zoukankan      html  css  js  c++  java
  • XPath 获取两个node中间的HTML Nodes

    XPath 获取两个node中间的HTML Nodes


    //div[@id="Recipe"]//h5[contains(text(),"Ingredients")]/following-sibling::p[count(.|//div[@id="Recipe"]//h5[contains(text(),"Method")]/preceding-sibling::p) = count(//div[@id="Recipe"]//h5[contains(text(),"Method")]/preceding-sibling::p)]

    In XPath 1.0 one way to do this is by using the Kayessian method for node-set intersection:

    $ns1[count(.|$ns2) = count($ns2)]

    The above expression selects exactly the nodes that are part both of the node-set $ns1 and the node-set $ns2.

    To apply this to the specific question -- let's say we need to select all nodes between the 2nd and 3rd h3 element in the following XML document:

    <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>

    We have to substitute $ns1 with:

    /*/h3[2]/following-sibling::node()

    and to substitute $ns2 with:

    /*/h3[3]/preceding-sibling::node()

    Thus, the complete XPath expression is:

    /*/h3[2]/following-sibling::node()
                 [count(.|/*/h3[3]/preceding-sibling::node())
                 =
                  count(/*/h3[3]/preceding-sibling::node())
                 ]

    We can verify that this is the correct XPath expression:

    <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>

    When this transformation is applied on the XML document presented above, the wanted, correct result is produced:

    <a32/>
    
    <b32/>

    II. XPath 2.0 solution:

    Use the intersect operator:

       /*/h3[2]/following-sibling::node()
    intersect
       /*/h3[3]/preceding-sibling::node()

     

  • 相关阅读:
    [转][LeetCode]Longest Common Prefix ——求字符串的最长公共前缀
    [转]最长回文子串——4种解法
    [转]通过金矿模型介绍动态规划
    一句话说清楚什么是闭包函数
    [转]as3事件流机制彻底理解
    Eclipse 快捷键
    文件打包与解压缩
    第5节 环境变量与文件查找
    vim的多标签
    java思维导图
  • 原文地址:https://www.cnblogs.com/timssd/p/6562743.html
Copyright © 2011-2022 走看看