开发者

How to find an element in XML using Xquery in XSLT

In my XSLT using Xquery I need to find the Value for the given Key :

<ExtendedProperties>
    <ExtendedProperty>
        <Key>AvailabilityStatus</Key>
        <Value>test</Value>
    </ExtendedProperty>
    <ExtendedProperty>
        <Key>HomeDelivery</Key>
        <Value>1</Value>
    </ExtendedProperty&g开发者_JAVA百科t;
    <ExtendedProperty>
        <Key>LogisticNature</Key>
        <Value>1</Value>
    </ExtendedProperty>
</ExtendedProperties>

Example : If I give the key AvailabilityStatus I should get the result : test


Here's an example of what I think you are trying to accomplish. I'm using XSLT 2.0 and passing the search string as a parameter to the stylesheet. If you need XSLT 1.0, we can easily modify the example.

Using your sample XML and passing 'AvailabilityStatus' as the searchString parameter, this XSLT 2.0:

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="text"/>
  <xsl:strip-space elements="*"/>

  <xsl:param name="searchString"/>

  <xsl:template match="/">
    <xsl:value-of select="ExtendedProperties/ExtendedProperty[contains(lower-case(Key),lower-case($searchString))]/Value"/>
  </xsl:template>

</xsl:stylesheet>

produces the wanted(??) output:

test

If you want a more exact match, use = instead of the contains() function and remove the lower-case() processing.

EDIT

After reading the comments for a third time, it seems like what you really want is the XPath. The XPath in my XSLT example probably isn't what you're looking for. Maybe something like this:

/ExtendedProperties/ExtendedProperty[Key='AvailabilityStatus']/Value

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜