T
tschwartz
I'm trying to write a stylesheet which removes nodes which are empty
as a result of other template processing.
For example, given the following xml, I'd like to:
- remove all "b" elements
- remove all "a" elements which, as a result of "b" element
removal, now have no children
so, starting with:
<?xml version="1.0" encoding="UTF-8"?>
<doc>
<a>
<b/>
</a>
<a>
<b/>
<c/>
</a>
</doc>
I'd like to end up with:
<?xml version="1.0" encoding="UTF-8"?>
<doc>
<a>
<c/>
</a>
</doc>
I've written this stylesheet:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl
utput omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<!-- matches all nodes and attributes -->
<xsl:template match="node()|@*" name="identity">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<!-- rule for element "a", ought to reproduce "a" only if it has
children -->
<xsl:template match="//a">
<xsl:if test="count(./*) > 0">
<xsl:call-template name="identity"/>
</xsl:if>
</xsl:template>
<!-- remove all b nodes -->
<xsl:template match="//b" priority="3" name="b-removal"/>
</xsl:stylesheet>
but the resultant output has not removed the empty "a" element:
<doc>
<a/>
<a>
<c/>
</a>
</doc>
Can anyone suggest a way to do this?
Thanks,
Ted
as a result of other template processing.
For example, given the following xml, I'd like to:
- remove all "b" elements
- remove all "a" elements which, as a result of "b" element
removal, now have no children
so, starting with:
<?xml version="1.0" encoding="UTF-8"?>
<doc>
<a>
<b/>
</a>
<a>
<b/>
<c/>
</a>
</doc>
I'd like to end up with:
<?xml version="1.0" encoding="UTF-8"?>
<doc>
<a>
<c/>
</a>
</doc>
I've written this stylesheet:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl
<xsl:strip-space elements="*"/>
<!-- matches all nodes and attributes -->
<xsl:template match="node()|@*" name="identity">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<!-- rule for element "a", ought to reproduce "a" only if it has
children -->
<xsl:template match="//a">
<xsl:if test="count(./*) > 0">
<xsl:call-template name="identity"/>
</xsl:if>
</xsl:template>
<!-- remove all b nodes -->
<xsl:template match="//b" priority="3" name="b-removal"/>
</xsl:stylesheet>
but the resultant output has not removed the empty "a" element:
<doc>
<a/>
<a>
<c/>
</a>
</doc>
Can anyone suggest a way to do this?
Thanks,
Ted