By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,313 Members | 2,798 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,313 IT Pros & Developers. It's quick & easy.

Converting HTML tags within an XML document back to HTML using XSL

P: 1
Hi,

I'm creating a system where my XML includes HTML tags (<h1></h1>) in addition to other XML elements (<book></book>).

I would like to render the HTML tags back to HTML using XSL. Considering I want to replace all headings, I could do for each heading level (i.e. repeat the following code for h2, h3, h4, h5, etc.):

<xsl:template match="h1">
<h1><xsl:apply-templates/></h1>
</xsl:template>

or I can just write once:

<xsl:template match="*[starts-with(local-name(), 'h')]">
<xsl:copy><xsl:apply-templates/></xsl:copy>
</xsl:template>

The problem is that with xsl:copy, I'm loosing the 'native' HTML appearance of the tags. For example, the headings will be displayed inline rather than block. To solve this I can just edit the stylesheet and add display:block to all headings - meaning I'm doing extra job anyway, but I was wondering if there is any smart way of doing this.

Ideally, I would like to do something like this:

<xsl:template match="p|ul|li|dl|dt|dd|i|em|strong|b">
<xsl:copy><xsl:apply-templates/></xsl:copy>
</xsl:template>

rather than writing each separately.

Thanks in advance,
Izhaki
Feb 8 '08 #1
Share this Question
Share on Google+
1 Reply


jkmyoung
Expert 100+
P: 2,057
Are you missing copying the attributes?
Guessing something like:
Expand|Select|Wrap|Line Numbers
  1. <xsl:template match="h1|h2|h3|h4|h5">
  2.   <h1>
  3.     <xsl:copy-of select="@*"/>
  4.     <xsl:apply-templates/>
  5.   </h1>
  6. </xsl:template>
  7. <xsl:template match="*"> 
  8.   <xsl:copy>
  9.     <xsl:copy-of select="@*"/>
  10.     <xsl:apply-templates/>
  11.   </xsl:copy>
  12. </xsl:template>
  13.  
Feb 11 '08 #2

Post your reply

Sign in to post your reply or Sign up for a free account.