0

If I had a lookup table in XML like this:

<lookup>
    <element key='abc'>Hello</element>
</lookup>

And another XML file like this:

<root>
    <child key='abc'>Goodbye</child>
</root>

And I do this XSL transformation after loading the first file into a variable:

<xsl:variable name="myvar" select="document('lookupfile.xml')/lookup" />
<xsl:value-of select="$myvar/element[@key=/root/child/@key]" />

What I want is the value 'Hello' to come up, but instead I get nothing. Am I not allowed to compare the value of two nodes directly? Every example I see always compares [@key='hardCodedValue'] and never to the value of another node.

If I hardcode the value like so: select="$myvar/element[@key='abc'] it returns 'Hello'. If I directly output the value of the root/child key with select="/root/child/@key" I get the correct value 'abc'. It's just when I try to do the comparison above that it returns nothing.

3
  • What's the definition of $myvar? Commented Nov 5, 2014 at 18:35
  • I edited it to include the definition. Commented Nov 5, 2014 at 18:42
  • Please post a complete example that can be used to reproduce the issue. Your code is taken out of context - and context is crucial in XPath/XSLT. Commented Nov 5, 2014 at 18:48

1 Answer 1

1

Inside the predicate, the context is the element element of the lookupfile.xml document, so /root/child/@key is going to be evaluated within that document.

You can either do this, with current():

<xsl:variable name="myvar" select="document('lookupfile.xml')/lookup" />
<xsl:value-of select="$myvar/element[@key = current()/root/child/@key]" />

or store the value in a variable and use that:

<xsl:variable name="myvar" select="document('lookupfile.xml')/lookup" />
<xsl:variable name="mykey" select="/root/child/@key" />
<xsl:value-of select="$myvar/element[@key = $mykey]" />
Sign up to request clarification or add additional context in comments.

1 Comment

Using current() solved the problem in my simple test example! It didn't work in my actual XML document, though dumping the value of the '/root/child/@key' equivalent in a variable did. I'll try and figure out why current() isn't working in my real file, but I think I'm on the right track. Thanks!

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.