开发者

How can I get a value from an xml key/value pair with xpath in my xslt?

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
    </val开发者_运维知识库ue>
  </entry>
</root>

What would the correct xpath be for this?


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


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"/>


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜