Reputation: 1336
I have this XML File :
<ID>
<T1>
<T2>XXXXX</T2>
<T2>XXXXX</T2>
</T1>
<T3>
<T4>
<TxId>AAAXXXXXXXXXXX</TxId>
</T4>
<T4>
<TxId>BBBXXXXXXXXXXX</TxId>
</T4>
<T4>
<TxId>BBXXXXXXXXXXX</TxId>
</T4> ....
I need to exclude T4 element when 3 first char of TxId = AAA
I have tried that :
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<!--Identity template,
provides default behavior that copies all content into the output -->
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<!--More specific template for Node766 that provides custom behavior -->
<xsl:template match="T4">
<xsl:copy>
<xsl:apply-templates select="TxId[not(starts-with(.,'AAA'))]"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
Upvotes: 0
Views: 366
Reputation: 117140
I need to exclude T4 element when 3 first char of TxId = AAA
If you want to exclude certain <T4>
elements, then you should apply the predicate when matching "T4" - not when selecting their children.
For example:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="utf-8" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="T4[not(starts-with(TxId,'AAA'))]">
<xsl:copy>
<xsl:apply-templates/>
</xsl:copy>
</xsl:template>
<xsl:template match="T4"/>
</xsl:stylesheet>
Upvotes: 3
Reputation: 28004
Instead of
<xsl:template match="T4">
<xsl:copy>
<xsl:apply-templates select="TxId[not(starts-with(.,'AAA'))]"/>
</xsl:copy>
</xsl:template>
You need something like
<xsl:template match="T4[TxId[starts-with(.,'AAA')]]" priority="2">
</xsl:template>
In other words, for any T4 element, you want the stylesheet to use the identity template and copy the T4 recursively, unless it matches this second template. In the latter case, output nothing (the template is empty).
This second template has a higher default priority than the identity template, because of its match pattern; but I like to make the priority explicit so we can easily see that the second template is intended to override, when it matches.
Upvotes: 4