XSLT Delete whole tag if subtag with certain value

2019-08-11 19:55发布

问题:

I have following xml:

<root>
    <product>
        <some-another-tag>45</some-another-tag>
        <provider>1</provider>
    </product>
    <product>
        <some-another-tag>45</some-another-tag>
        <provider>2</provider>
    </product>
    <product>
        <some-another-tag>45</some-another-tag>
        <provider>3</provider>
    </product>
    <product>
        <some-another-tag>45</some-another-tag>
        <provider>1</provider>
    </product>
    <product>
        <some-another-tag>45</some-another-tag>
        <provider>8</provider>
    </product>
</root>

Using xsl transform I want to remove whole <product> tag if there is <provider>1</provider> in it.

How can I build such template?

回答1:

In general, if you want to remove certain elements then you start with the identity transformation template and add empty templates matching those elements you want to remove, see http://xsltransform.net/pPzifpk which does:

<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">

    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>

    <xsl:template match="product[provider = 1]"/>

</xsl:transform>


标签: xml xslt