Reputation: 511
I have an xml like
<Books>
<Book Name="ABC">
<Line No="43"/>
</Book>
<Book Name="XYZ">
<Line No="44"/>
</Book>
</Books>
I have to remove where Name is "ABC" only when where Name is "XYZ" is also present (or where Name is "ABC" is not the only element in the nodeset)
The xslt i prepared is like :
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="xml" indent="yes" />
<xsl:if test="count(Books/Book) > '1'">
<xsl:strip-space elements="*" />
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()" />
</xsl:copy>
</xsl:template>
<xsl:template match="Book[@Name='ABC']" />
</xsl:if>
</xsl:stylesheet>
This does not seem to work. What is it that I am doing wrong here.
Upvotes: 1
Views: 405
Reputation: 122394
You're on the right lines with the identity template, but you need to put the condition in the matching pattern of the overriding empty template rather than trying to use an if
(which is not allowed at the top level anyway, only inside a template).
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="xml" indent="yes" />
<xsl:strip-space elements="*" />
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()" />
</xsl:copy>
</xsl:template>
<xsl:template match="Book[@Name='ABC'][count(../Book) > 1]" />
</xsl:stylesheet>
That match
pattern will match the "ABC" book, but only when it has at least one other Book
sibling. If ABC is the only Book
that pattern won't match, and the matcher will fall back to the identity template.
Upvotes: 1