Evgeny Makarov
Evgeny Makarov

Reputation: 1448

XSLT Delete whole tag if subtag with certain value presents

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?

Upvotes: 1

Views: 246

Answers (1)

Martin Honnen
Martin Honnen

Reputation: 167716

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>

Upvotes: 1

Related Questions