C
CI
I have the following XML file
<Book>
<Chapters>
<Chapter number="1"/>
<Chapter number="2"/>
</Chapters>
<Colors>
<Color RGB="255"/>
<Color RGB="211" name="Red"/>
<Color name="Yellow"/>
<Color RGB="127"/>
</Colors>
</Book>
I want to get a tree with the <Color> elements that don't have RGB
attribute:
So I came up with this XSLT sheet.
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/
Transform">
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="Color">
<xsl:choose>
<xsl:when test="@RGB">
</xsl:when>
<xsl
therwise>
<xsl:copy-of select="."/>
</xsl
therwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
It works correctly, but if in my XML file all the <Color> elements
have RGB attribute the resulted tree
contains empty <Colors></Colors> node.
Anyone had an idea how to conditionally remove it?
Regards,
Michael
<Book>
<Chapters>
<Chapter number="1"/>
<Chapter number="2"/>
</Chapters>
<Colors>
<Color RGB="255"/>
<Color RGB="211" name="Red"/>
<Color name="Yellow"/>
<Color RGB="127"/>
</Colors>
</Book>
I want to get a tree with the <Color> elements that don't have RGB
attribute:
So I came up with this XSLT sheet.
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/
Transform">
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="Color">
<xsl:choose>
<xsl:when test="@RGB">
</xsl:when>
<xsl
<xsl:copy-of select="."/>
</xsl
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
It works correctly, but if in my XML file all the <Color> elements
have RGB attribute the resulted tree
contains empty <Colors></Colors> node.
Anyone had an idea how to conditionally remove it?
Regards,
Michael