jai
jai

Reputation: 21897

XSLT : Check for elements in sequence ignore case - if present node has to be removed

Input XML structure:

<ITEMS>
   <ITEM>
      <ITEMTYPE>FOO</ITEMTYPE>
   </ITEM>
   <ITEM>
      <ITEMTYPE>BAR</ITEMTYPE>
   </ITEM>
</ITEMS>

I'll be passing sequence as parameter to the XSLT. If the ITEMTYPE is present in the sequence, the node has to be removed. And it has to be processed in case-insensitive.

I'm newbie to XSLT. I've coded like below but not able to crack the case-insensitive.

<xsl:stylesheet version="2.0"
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:fn="http://www.w3.org/2005/xpath-functions"
                exclude-result-prefixes="#all">

    <xsl:output method="xml" indent="yes" omit-xml-declaration="yes"/>

    <xsl:param name="itemsToRemove"/>


    <xsl:output omit-xml-declaration="yes"/>

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

    <xsl:template match="ITEM">
        <xsl:variable name="currentItemType">
            <xsl:value-of select="itemType"/>
        </xsl:variable>
        <xsl:if test="not(fn:index-of($itemsToRemove, $currentItemType))">
            <xsl:copy>
                <xsl:apply-templates select="node()|@*"/>
            </xsl:copy>
        </xsl:if>
    </xsl:template>

</xsl:stylesheet>

Upvotes: 0

Views: 66

Answers (1)

Martin Honnen
Martin Honnen

Reputation: 167706

If you want to use case-insensitive value comparison then one way is using the matches function with the i flag:

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

    <xsl:param name="itemsToRemove" select="'foo'"/>

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

    <xsl:template match="ITEM[$itemsToRemove[matches(., current()/ITEMTYPE, 'i')]]"/>
</xsl:transform>

With http://xsltransform.net/3NSSEvW the output then is

<ITEMS>

   <ITEM>
      <ITEMTYPE>BAR</ITEMTYPE>
   </ITEM>
</ITEMS>

Upvotes: 2

Related Questions