0

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!

1
  • I do not see why you would do it like this. If your data in "array" is as simple as it looks like an named template with an choose when would do far better. Commented Jul 2, 2013 at 13:19

3 Answers 3

1

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>
Sign up to request clarification or add additional context in comments.

Comments

1
<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.

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.