I am trying to remove nodes from an xml if certain attribute values are not matching. Here is my XML:
<CONFIGURATIONS>
<CONFIG1 DOMAIN="CAE" FOCUS="IN" STATUS="ACTIVE" >
<ATTR>1<ATTR>
</CONFIG1>
<CONFIG1 DOMAIN="CAE" FOCUS="OUT" STATUS="INACTIVE" >
<ATTR>2<ATTR>
</CONFIG1>
<CONFIG1 DOMAIN="CAE" FOCUS="OUT" STATUS="ACTIVE" >
<ATTR>2<ATTR>
</CONFIG1>
<CONFIG1 DOMAIN="MFG" FOCUS="OUT" STATUS="ACTIVE" >
<ATTR>3<ATTR>
</CONFIG1>
</CONFIGURATIONS>
I want to remove the nodes not do not have the DOMAIN value as CAE and the FOCUS is not OUT and the STATUS is not ACTIVE.
<CONFIGURATIONS>
<CONFIG1 DOMAIN="CAE" FOCUS="OUT" STATUS="ACTIVE" >
<ATTR>2<ATTR>
</CONFIG1>
I am using the following xslt:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes"/>
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="SM_CONFIG_ITEM [@DOMAIN !='CAE' and @FOCUS !='OUT' and @STATUS != 'INACTIVE']"/>
</xsl:stylesheet>
But it is not producing the desired output. What am I doing wrong?
You want the element to be matched when one of the
!=conditions is satisfied — but the above match pattern specifies that all of the!=conditions must be satisfied.Solution:
This transformation:
when applied on the provided XML document:
produces the wanted, correct result: