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

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Izhaki
    New Member
    • Feb 2008
    • 1

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

    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:appl y-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|s trong|b">
    <xsl:copy><xsl: apply-templates/></xsl:copy>
    </xsl:template>

    rather than writing each separately.

    Thanks in advance,
    Izhaki
  • jkmyoung
    Recognized Expert Top Contributor
    • Mar 2006
    • 2057

    #2
    Are you missing copying the attributes?
    Guessing something like:
    [code=xml]
    <xsl:template match="h1|h2|h3 |h4|h5">
    <h1>
    <xsl:copy-of select="@*"/>
    <xsl:apply-templates/>
    </h1>
    </xsl:template>
    <xsl:template match="*">
    <xsl:copy>
    <xsl:copy-of select="@*"/>
    <xsl:apply-templates/>
    </xsl:copy>
    </xsl:template>
    [/code]

    Comment

    Working...