使用xpath / xslt确定XML节点是否包含文本,后跟特定的子元素

时间:2015-07-10 22:38:40

标签: xml xslt xpath

我需要确定一个元素包含随机文本字符串,后跟一个也包含随机字符串的特定子元素的情况。例如:

<paragraph>Here's some text and here's a <word>child</word></paragraph>

这是样式表的一部分,因此应该使用xpath。

2 个答案:

答案 0 :(得分:1)

您正在寻找following-sibling::轴。

使用此XML文档:

<root>
  <paragraph>Here's some text and here's a <word>child</word>.</paragraph>
  <paragraph>Here's some text with no child.</paragraph>
  <paragraph>Here's some text with another <word>child</word>.</paragraph>
  <paragraph/>
  <paragraph>Here's some text with any empty <word/>.</paragraph>
</root>

以下XPath表达式选择第一,第三和第五段:

//paragraph[text()[following-sibling::word]]

如果您想约束word标记也有直接文本子项,请使用此选项仅选择第一个和第三个:

//paragraph[text()[following-sibling::word[text()]]]

答案 1 :(得分:0)

尝试:

 //*[text()/following-sibling::*/text()]