Question

Here is my example document

<a >
 <b flag='foo'>
 <c/>
 <d/>
 </b>
</a>

I am looking to remove the "c" element only when the flag attribute on b is 'bar'. Ie if flag='foo' the "c" element should not be removed. I do not currently have an xsl debugging tool on my pc, and have been unable to find an online tool that shows xslt error information, and have been running the following test xsl transform on http://xslttest.appspot.com/ :

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    version="1.0">
<xsl:output method="xml" indent="yes" version="1.0" encoding="ISO-8859-1"/>
    <!--Identity template to copy all content by default-->
    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>
    <xsl:choose>
    <xsl:when test="/a/b[@flag='bar']">
    <xsl:template match="/a/b/c"/>
    </xsl:when>
    </xsl:choose>
</xsl:stylesheet>

When I run this I get Error: Failed to compile stylesheet. 3 errors detected. I'm looking for help (1) fixing the problems with the xsl code and (2) anything out there like jsfiddle for xsl that can debug/test fragements of xsl code.

Was it helpful?

Solution

You can't put a choose outside a template, but you don't need to - you can use predicates in match expressions so just declare your no-op template to match the elements you want to remove:

<xsl:template match="b[@flag='bar']/c" />

or more generally, if the parent of the c element might have various names

<xsl:template match="c[../@flag='bar']" />

or

<xsl:template match="*[@flag='bar']/c" />
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top