如何找到两个H3';谁在使用XPATH?

如何找到两个H3';谁在使用XPATH?,xpath,Xpath,如何使用XPATH查找两个H3之间的所有节点 在XPath 1.0中,实现这一点的一种方法是对节点集交集使用Kayessian方法: $ns1[count(.|$ns2) = count($ns2)] /*/h3[2]/following-sibling::node() /*/h3[3]/preceding-sibling::node() /*/h3[2]/following-sibling::node() [count(.|/*/h3[3]/preceding-

如何使用XPATH查找两个H3之间的所有节点

在XPath 1.0中,实现这一点的一种方法是对节点集交集使用Kayessian方法:

$ns1[count(.|$ns2) = count($ns2)]
/*/h3[2]/following-sibling::node()
/*/h3[3]/preceding-sibling::node()
/*/h3[2]/following-sibling::node()
             [count(.|/*/h3[3]/preceding-sibling::node())
             =
              count(/*/h3[3]/preceding-sibling::node())
             ]
<a32/>

<b32/>
   /*/h3[2]/following-sibling::node()
intersect
   /*/h3[3]/preceding-sibling::node()
上面的表达式精确地选择属于节点集
$ns1
和节点集
$ns2
的节点

要将此应用于特定问题——假设我们需要在以下XML文档中选择第二个和第三个
h3
元素之间的所有节点:

<html>
  <h3>Title T31</h3>
    <a31/>
    <b31/>
  <h3>Title T32</h3>
    <a32/>
    <b32/>
  <h3>Title T33</h3>
    <a33/>
    <b33/>
  <h3>Title T34</h3>
    <a34/>
    <b34/>
  <h3>Title T35</h3>
</html>
并用替换
$ns2

$ns1[count(.|$ns2) = count($ns2)]
/*/h3[2]/following-sibling::node()
/*/h3[3]/preceding-sibling::node()
/*/h3[2]/following-sibling::node()
             [count(.|/*/h3[3]/preceding-sibling::node())
             =
              count(/*/h3[3]/preceding-sibling::node())
             ]
<a32/>

<b32/>
   /*/h3[2]/following-sibling::node()
intersect
   /*/h3[3]/preceding-sibling::node()
因此,完整的XPath表达式是

$ns1[count(.|$ns2) = count($ns2)]
/*/h3[2]/following-sibling::node()
/*/h3[3]/preceding-sibling::node()
/*/h3[2]/following-sibling::node()
             [count(.|/*/h3[3]/preceding-sibling::node())
             =
              count(/*/h3[3]/preceding-sibling::node())
             ]
<a32/>

<b32/>
   /*/h3[2]/following-sibling::node()
intersect
   /*/h3[3]/preceding-sibling::node()
我们可以验证这是正确的XPath表达式:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>

 <xsl:template match="/">
  <xsl:copy-of select=
   "/*/h3[2]/following-sibling::node()
             [count(.|/*/h3[3]/preceding-sibling::node())
             =
              count(/*/h3[3]/preceding-sibling::node())
             ]
   "/>
 </xsl:template>
</xsl:stylesheet>

在XPath2.0中,假设您希望两个h3元素之间的所有树深度都有节点,而这两个元素不一定是同级的

/path/to/first/h3/following::node()[. << /path/to/second/h3]

/path/to/first/h3/following::node()[.当您知道两个标记是同一元素时的其他XPath 1.0解决方案(本例为
h3
):

基于出色的回答,我可以提出以下解决方案,即我只给出第一项标题的内容,而不是针对不同的H3对[2]和[3]进行硬编码

//h3[text()="Main Page Section Heading"]/following-sibling::node()
 [  count(.|//h3[text()="Main Page Section Heading"]/following-sibling::h3[1]/preceding-sibling::node()) =  
    count(//h3[text()="Main Page Section Heading"]/following-sibling::h3[1]/preceding-sibling::node())  ]

但我想更进一步的是,当我查看最后一个H3时,能够处理这个场景,并获取它之后的所有内容,在上述情况下,我无法获取最后一个H3之后的内容。

有另一个使用键的伟大通用解决方案,假设您的
标记具有唯一属性(例如,其文本或
id
属性):



它根据前面的

对所有标记进行分组,因此,这没有解决的一个用例是最后一个H3之后的内容。我很好奇,需要做哪些修改才能将其显示出来。@klumsy:只需在现有表达式前面加上
“/*/H3[2]/下面的兄弟::节点()[不是(/*/H3[3])]|
如何在更多的“切片”上使用此表达式循环?如何在所有h3的循环中用变量替换2和3?@zypro,很简单:让变量
$startInd
$endInd
,并在“循环”中用必要的值声明它们。此外,在表达式中替换“
2
“使用
$startInd
和“
3
”使用
$endInd
。XPath 2.0表达式甚至可以是:
对于$i in 1进行计数(/*/h3)-1返回/*/h3[$i]/后面的同级节点::node()相交/*/h3[$i+1]/前面的同级节点::node()