XSL: Exclude element with start-with condition

advertisements

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>


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>