1

I am trying to use contains() where i want to display the node value only when it does not contain number but I get all the node values in result. Below is my code and sample XML. My code is as below: Please let me know where I am doing wrong.

my code:

     <xsl:variable name="restricted_characters" select="1234567890"/>
        <xsl:for-each select="test/a">
        <xsl:if test="not(contains(. , $restricted_characters))">
         <xsl:value-of select="position()" />:
         <xsl:value-of select="." />
        </xsl:if>
        </xsl:for-each>

XML:

    <test>
    <a>abc11</a>
    <a>123</a>
    <a>abc</a>
    <a>abc2</a>
    <a>abc3</a>
    <a>abc</a>
    </test>  
har07
  • 88,338
  • 12
  • 84
  • 137
Shweta
  • 27
  • 5

1 Answers1

1

You can't do it that way. contains() checks if the 1st argument contains the 2nd argument entirely. For example, your test expression will return true for abc1234567890 but false for abc123.

Instead of using contains(), you can use translate() to replace number characters in your input string with empty, and then compare the result against the original input string. If both are the same, then that means the input string doesn't contain any number characters.

<xsl:if test="translate(. , $restricted_characters, '') != .">
har07
  • 88,338
  • 12
  • 84
  • 137