Spons
Spons

Reputation: 1591

XSLT recursive html clean up

I'm working with a xslt to cleanup a html code that is generated by an editor.

The code contains the following structure:

<span name="x">1<b>test text</b>
    <span name="y">2</span>
</span>
<span name="y">3
    <span name="y">4</span>
    <span name="x">1</span>
</span>
<span name="y">5<i>test</i>
    <span name="y"><u>6</u>7</span>
</span>

The thing is that xslt needs to preserve the HTML structure. And needs to complete 2 actions. if it finds a span with the name x. It needs to remove it. (This is not an problem)

But when it finds a y that's not inside a x. It needs to take the child nodes (value of node()), and place them in the output. The child nodes need to be checked for any other spans.

A.t.m i did have some xslt's that find the first level spans (number 1 and 3). But it copies the inner html, and doesn't apply the templates for the innerHTML.

Did anyone have this problem, or know a solution?

EDIT: Needed output as explained above.

345<i>test</i><u>6</u>7

EDIT2: XSLT

<xsl:template match="@* | node()">
    <xsl:copy>
        <xsl:apply-templates select="@* | node()"/>
    </xsl:copy>
</xsl:template>
<xsl:template match="span[@name='x']">
 //This deletes the node.. But only one level?
</xsl:template>
<xsl:template  match="span[@name='y']">
 //This needs to play all templates again to remove or get inner nodes() see case x & y
</xsl:template>

Upvotes: 1

Views: 316

Answers (1)

Navin Rawat
Navin Rawat

Reputation: 3138

You just need to include apply-templates in your last template to get desired output.

Upvotes: 1

Related Questions