【问题标题】:Get an attribute from a node using another "multi-values"attribute XML/XSL使用另一个“多值”属性 XML/XSL 从节点获取属性
【发布时间】:2018-10-30 15:58:47
【问题描述】:

我不明白如何找到一个搜索“多值”属性的属性。

我会写一个来自 W3school 的例子

[链接:https://www.w3schools.com/xml/xpath_examples.asp]

<bookstore>
<book category="web">
  <title lang="en">XQuery Kick Start</title>
  <author>James McGovern</author>
  <author>Per Bothner</author>
  <author>Kurt Cagle</author>
  <author>James Linn</author>
  <author>Vaidyanathan Nagarajan</author>
  <year>2003</year>
  <price>49.99</price>
</book>

</bookstore>

例如,我怎样才能得到使用字符串&lt;xsl:value-of "/bookstore/book[author = James McGovern]/title"/&gt; 搜索的标题。

有人可以帮助我吗?我是使用 XML 和 XSL 的新手。

谢谢。

【问题讨论】:

    标签: html xml xslt


    【解决方案1】:

    首先,请注意您的术语。在您的 XML 中,只有 categorylang 是属性。另一方面,author 是一个元素。所以,你真正要问的是当多个子元素同名时如何匹配。

    答案是完全按照您正在尝试做的事情做,希望您只需将您的作者姓名放在撇号中以表明它是一个字符串文字

    <xsl:value-of select="/bookstore/book[author = 'James McGovern']/title"/>
    

    当你有一个像author = 'James McGovern' 这样的表达式时,它实际上会检查所有author 元素,如果至少有一个匹配则返回true。因此,即使“James McGovern”是最后列出的作者,它仍然会返回 true。

    (如果您想了解更多信息,可以阅读官方 W3C 规范中的布尔表达式https://www.w3.org/TR/xpath-10/#booleans。特别是,这种情况由“如果要比较的对象是node-set 和 other 是一个字符串,那么当且仅当 node-set 中有一个节点使得对节点的 string-value 和另一个字符串执行比较的结果为真的")

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-09-27
      • 1970-01-01
      • 1970-01-01
      • 2021-02-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多