7

I have some xml that I want to process using xslt. A good amount of the data comes through in key value pairs (see below). I am struggling with how to extract the value base on the key into a variable. I would like to be able to do something like this:

<xsl:variable name="foo" select="/root/entry[key = 'foo']/value"/>

but that doesn't seem to work. Here is sample xml.

<?xml version="1.0" encoding="ISO-8859-1"?>
<root>
  <entry>
    <key>
      foo
    </key>
    <value>
      bar
    </value>
  </entry>
</root>

What would the correct xpath be for this?

TahoeWolverine
  • 1,749
  • 2
  • 23
  • 31
  • Good Question (+1). Did you know that the answer you selected is not generally correct? See my answer for an explanation and for two true solutions. :) – Dimitre Novatchev Apr 29 '10 at 16:48

3 Answers3

6

The following transformation shows two ways to achieve this -- with and without the use of <xsl:key> and the key() function:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes"/>

 <xsl:key name="kValueByKey"
   match="value" use="normalize-space(../key)"/>

 <xsl:template match="/">
   1. By key: <xsl:text/>

   <xsl:copy-of select="key('kValueByKey', 'foo')"/>

   2. Not using key:  <xsl:text/>

   <xsl:copy-of select="/*/*[normalize-space(key)='foo']/value"/>
 </xsl:template>
</xsl:stylesheet>

Do note the use of the normalize-space() function to strip any leading or trailing whitespace characters from the value of <key>.

Dimitre Novatchev
  • 240,661
  • 26
  • 293
  • 431
  • seems to be the best of both worlds, correct? – TahoeWolverine Apr 29 '10 at 17:20
  • @TahoeWolverine: In pure XPath one *has* to use this expression. Whenever XPath is hosted by XSLT I'd always prefer using keys -- isn't it noticeable how the key-expression is simpler than the "pure" XPath one? Another very strong argument in favor of keys is that in most cases using keys is many factors of magnitude faster. – Dimitre Novatchev Apr 29 '10 at 17:29
  • Yes I do see that now. I was a bit confused because I did not see where you were defining variables. Once I started to create the variables this way, things became quite a bit cleaner. Thanks. – TahoeWolverine Apr 29 '10 at 17:47
5

Your XPath works fine for the following (equivalent) document:

<?xml version="1.0" encoding="ISO-8859-1"?>
<root>
 <entry>
  <key>foo</key>
  <value>
   bar
  </value>
 </entry>
</root>

You can use the xpath contains function to get the same result without restructuring your XML:

<xsl:variable name="foo" select="/root/entry[contains(key,'foo')]/value"/>
Oded
  • 489,969
  • 99
  • 883
  • 1,009
  • I should have known it would have been something like that. Unfortunately explaining the xml change to the people creating the xml would be overly difficult. I will use the xpath. Thanks a lot. – TahoeWolverine Apr 29 '10 at 15:10
  • Very straight forward solution but if in your XML was 'fooz' then it still contains 'foo', and you will have a wrong match. – derloopkat Dec 12 '13 at 10:14
3
<xsl:variable name="foo" select="/root/entry[key='foo']/value" />

This is an exact match so make sure there are no spare spaces or new line characters around foo in your XML. Otherwise use replace function and a regular expression to trim them.

Community
  • 1
  • 1
derloopkat
  • 6,232
  • 16
  • 38
  • 45