繁体   English   中英

使用 XPath select 包含具有属性值的子元素的所有元素

[英]Using XPath select all elements that contains a child element with an attribute value

我正在尝试使用 xpath 到 select 所有包含子元素的节点元素,该子元素具有除空字符串之外的任何限制属性值。

这是我的 xml:

<nodes code="C">
   <node code="A">
      <child restriction="" />
      <child restriction="value" />
      <child restriction="" />
      <child restriction="" />
      <child restriction="" />
      <child restriction="" />
      <child restriction="" />
   </node>
   <node code="B">
      <child restriction="value" />
      <child restriction="" />
      <child restriction="" />
      <child restriction="" />
      <child restriction="" />
      <child restriction="" />
      <child restriction="" />
   </node>
   <node code="C">
      <child restriction="" />
      <child restriction="" />
      <child restriction="" />
      <child restriction="" />
      <child restriction="" />
      <child restriction="" />
      <child restriction="" />
   </node>
</nodes>

我的工作 xpath 是:

nodes/node[(child/@restriction and string-length(child/@restriction) > 0)]

这只返回节点 B,大概是因为 B 是唯一首先出现限制值的元素。 我的目标是返回 B 和 A,但不返回 C。

有人能指出我哪里出错了吗?

怎么样:

nodes/node[child/@restriction[string()]]

这将测试每个restriction属性是否存在字符串内容。

在您的版本中,您尝试将child/@restriction的节点集转换为用作string-length()参数的字符串。 在 XSLT 1.0 中,这将返回集合中第一个节点的值; 在 XSLT 2.0 中,这会产生错误。

暂无
暂无

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

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