user2051823
user2051823

Reputation: 177

Matching a value in an array (XSLT)

I will get a value from the XML file, e.g.

<WORD>Men</WORD>

Then, I want to create an array in XSLT 1.0, e.g.

<array>
<Item>Men</Item>
<Item>Women</Item>
</array>

If the value from the XML file matches one of the items in the array, it will return true. Can anyone tell me how can I do it?? Thank you!

Upvotes: 0

Views: 3848

Answers (3)

C. M. Sperberg-McQueen
C. M. Sperberg-McQueen

Reputation: 25034

The condition $doc//WORD = $table//item is true when you want it to be, and false when you want it to be, if $doc and $table are bound appropriately.

Upvotes: 0

Tomalak
Tomalak

Reputation: 338316

<xsl:stylesheet 
  version="1.0" 
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:exsl="http://exslt.org/common" 
  xmlns:internal="http://tempuri.org/config"
  exclude-result-prefixes="internal"
  extension-element-prefixes="exsl"
>
  <internal:config>
    <array>
      <Item>Men</Item>
      <Item>Women</Item>
    </array>
  </internal:config>

  <xsl:variable name="config" select="document('')/*/internal:config" />

  <xsl:template match="WORD">
    <xsl:if test="$config/array/Item[. = current()]">
      <xsl:value-of select="concat('The current value ', ., ' was found.')" />
    </xsl:if>
  </xsl:template>

</xsl:stylesheet>

Note:

  • the use of a temporary namespace to store configuration in the XSLT program.
  • the use of exclude-result-prefixes to prevent leaking the temporary namespace into the result document
  • the use of document('') to access the contents stylesheet from within itself.

Upvotes: 1

Navin Rawat
Navin Rawat

Reputation: 3138

It seems you are looking for extension function of exsl:node-set. Have a look:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:exsl="http://exslt.org/common" extension-element-prefixes="exsl">

  <xsl:param name="InputArray">
    <array>
      <Item>Men</Item>
      <Item>Women</Item>
    </array>
  </xsl:param>

  <xsl:param name="InputItem">
    <WORD>Men</WORD>
  </xsl:param>

  <xsl:template match="/">
    <xsl:choose>
      <xsl:when test="exsl:node-set($InputArray)//Item[text()=exsl:node-set($InputItem)//text()]">
        <xsl:text>Yes</xsl:text>
      </xsl:when>
      <xsl:otherwise>
        <xsl:text>No</xsl:text>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

</xsl:stylesheet>

Upvotes: 1

Related Questions