7

Please consider my "A/B" xPath expression returns the following node

  <Q ID="12345">
  ----
  ----
  </Q>

This is my variable

This is how I’m trying to assign a value to my tempVariable variable

  <xsl:for-each select="A/B">
  <xsl:variable name="tempVariable"><xsl:value-of select="@ID"/></xsl:variable>
  </xsl:for-each>

And after all I’m trying to use this variable

  <xsl:if test="$tempVariable='12345'">
  ....
  ....
  </xsl:if>

but here as I understand I’m getting $tempVariable ="" which is not correct.

Can someone please tell me where I’m doing wrong or how can I do this in proper way. Thank you.

2
  • 1
    So Q is a child of B? ... then it should be Q/@ID ... Commented May 13, 2013 at 16:26
  • Seee the answer to similar quetion posted here stackoverflow.com/questions/17066455/… Commented Aug 16, 2014 at 8:51

1 Answer 1

8

Why would a path like A/B select a Q element? If you want to use a variable you need to make sure it is in scope. The variable you show in your sample is in scope inside the xsl:for-each, after the xsl:variable element.

If you want to use the variable outside the for-each you would need to declare it outside the for-each.

However I think you can simply do

<xsl:variable name="v1" select="A/B/@ID"/>
<xsl:if test="$v1 = '12345'">..</xsl:if>

there is no need for the for-each.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you for the answer Martin. How can I reassign a value to variable "v1" there, seems like XSLT does not permit me to reassign a values to a variable ??
XSLT is a declarative programming language while assignment and in particular reassignment to variables is a concept of procedural programming assuming some sequential execution order of statements. That is not how XSLT works and if you need help solving a problem in XSLT and are not yet familiar with it then consider to post an input sample and the corresponding result sample you want to create, then people here can show you an XSLT way of solving the problem.

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.