Leo
Leo

Reputation: 1934

Adding HTML tags with XSLT on the fly

I have an XML document containing this:

<d1/>
<p1>...</p1>
<p2>...</p2>
<d2/>
<p3>...</p3>
<d3/>

Where pn are elements with possibly subelements and other stuff, and dn indicates where an HTML DIV tag wrapping the p tags should begin, but without a corresponding closing tag, this is only indicated implicitly by the next dn tag. The desired HTML output is this:

<div>
<p1>...</p1>
<p2>...</p2>
</div>
<div>
<p3>...</p3>
</div>

I have written an XSLT to introduce the <div> and </div> tags on the fly, using the following:

<xsl:text disable-output-escaping="yes">&lt;div&gt;</xsl:text>

and

<xsl:text disable-output-escaping="yes">&lt;/div&gt;</xsl:text>

and this works on Safari, but it fails on FireFox, which makes me suspect that it's not the right way to do it. Do you have a better idea that will work on every browser?

Thanks a lot in advance.

Upvotes: 0

Views: 1277

Answers (2)

michael.hor257k
michael.hor257k

Reputation: 116992

You could use a technique known as "sibling recursion".

Given a well-formed input such as:

XML

<root>
    <d1/>
    <p1>a</p1>
    <p2>b</p2>
    <d2/>
    <p3>c</p3>
    <d3/>
</root>

the following stylesheet:

XSLT 1.0

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html"/>

<xsl:template match="/root">
    <body>
        <xsl:apply-templates select="*[starts-with(name(), 'd')][position()!=last()]"/>
    </body>
</xsl:template>

<xsl:template match="*[starts-with(name(), 'd')]">
    <div>
        <xsl:apply-templates select="following-sibling::*[1][not(starts-with(name(), 'd'))]"/>
    </div>
</xsl:template>

<xsl:template match="/root/*[not(starts-with(name(), 'd'))]">
    <xsl:copy-of select="."/>
    <xsl:apply-templates select="following-sibling::*[1][not(starts-with(name(), 'd'))]"/>
</xsl:template>

</xsl:stylesheet>

will return:

<body>
   <div>
      <p>a</p>
      <p>b</p>
   </div>
   <div>
      <p>c</p>
   </div>
</body>

Upvotes: 2

Martin Honnen
Martin Honnen

Reputation: 167506

Firefox does not support disable-output-escaping because it does not serialize the result tree. The problem is a grouping problem, one way to solve it is to use a key:

<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">

<xsl:output indent="yes"/>

<xsl:key name="group" match="body/*[not(starts-with(local-name(), 'd'))]" use="generate-id(preceding-sibling::*[starts-with(local-name(), 'd')][1])"/>

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

<xsl:template match="*[starts-with(local-name(), 'd')]">
    <div>
        <xsl:copy-of select="key('group', generate-id())"/>
    </div>
</xsl:template>
</xsl:transform>

That would create an empty div however at the end of your sample, so you might want to change the last template to

<xsl:template match="*[starts-with(local-name(), 'd')]">
    <xsl:if test="key('group', generate-id())">
      <div>
        <xsl:copy-of select="key('group', generate-id())"/>
      </div>
    </xsl:if>
</xsl:template>

Upvotes: 2

Related Questions