繁体   English   中英

如何在XSLT中使用Xquery在XML中查找元素

[英]How to find an element in XML using Xquery in XSLT

在使用Xquery的XSLT中,我需要找到给定Key的值:

<ExtendedProperties>
    <ExtendedProperty>
        <Key>AvailabilityStatus</Key>
        <Value>test</Value>
    </ExtendedProperty>
    <ExtendedProperty>
        <Key>HomeDelivery</Key>
        <Value>1</Value>
    </ExtendedProperty>
    <ExtendedProperty>
        <Key>LogisticNature</Key>
        <Value>1</Value>
    </ExtendedProperty>
</ExtendedProperties>

示例:如果我给出密钥AvailabilityStatus,我应该得到结果:test

这是我认为您要实现的目标的示例。 我正在使用XSLT 2.0,并将搜索字符串作为参数传递给样式表。 如果您需要XSLT 1.0,我们可以轻松地修改示例。

使用示例XML并将'AvailabilityStatus'作为searchString参数,此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>

产生通缉的(??)输出:

test

如果需要更精确的匹配,请使用=代替contains()函数,并删除lower-case()处理。

编辑

在第三次阅读注释后,似乎您真正想要的是XPath。 我的XSLT示例中的XPath可能不是您想要的。 也许是这样的:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM