Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/blackberry/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Html XPath表达式:选择A HREF=";之间的元素;expr";标签_Html_Select_Xpath_Tags - Fatal编程技术网

Html XPath表达式:选择A HREF=";之间的元素;expr";标签

Html XPath表达式:选择A HREF=";之间的元素;expr";标签,html,select,xpath,tags,Html,Select,Xpath,Tags,我没有找到一种明确的方法来选择存在于两个锚之间的所有节点( 第一节点 第一节点 X 第一节点 ... 拆除两个锚固件、三个p(当然包括内跨) 有办法吗 我不知道XPath2.0是否提供了更好的方法来实现这一点 *编辑(特殊情况!)* 我还应处理以下情况: “选择X和X'之间的标记,其中X是” 因此,不是: <a href="file://START..."></a> <!-- xhtml to be extracted --> <a href="fi

我没有找到一种明确的方法来选择存在于两个锚之间的所有节点(

第一节点

第一节点 X

第一节点

...
拆除两个锚固件、三个p(当然包括内跨)

有办法吗

我不知道XPath2.0是否提供了更好的方法来实现这一点

*编辑(特殊情况!)*

我还应处理以下情况:

“选择X和X'之间的标记,其中X是

因此,不是:

<a href="file://START..."></a>
<!-- xhtml to be extracted -->
<a href="file://END..."></a>

我还应处理:

<p>
  <a href="file://START..."></a>
</p>
<!-- xhtml to be extracted -->

<p>
  <a href="file://END..."></a>
</p>


再次感谢您。

使用此XPath 1.0表达式:

//a[starts-with(@href,'file://START')]/following-sibling::node()
     [count(.| //a[starts-with(@href,'file://END')]/preceding-sibling::node())
     =
      count(//a[starts-with(@href,'file://END')]/preceding-sibling::node())
     ]
    //a[starts-with(@href,'file://START')]/following-sibling::node()
  intersect
    //a[starts-with(@href,'file://END')]/preceding-sibling::node()
<html>...
    <a href="file://START0"></a>
    <p>First nodes</p>
    <p>First nodes    
        <span>X</span>
    </p>
    <p>First nodes</p>
    <a href="file://END0"></a>...
</html>
<p>First nodes</p>
<p>First nodes    
        <span>X</span>
</p>
<p>First nodes</p>
<xsl:stylesheet version="2.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>

 <xsl:template match="/">
  <xsl:copy-of select=
  " //a[starts-with(@href,'file://START')]/following-sibling::node()
   intersect
    //a[starts-with(@href,'file://END')]/preceding-sibling::node()
  "/>
 </xsl:template>
</xsl:stylesheet>
或者,使用此XPath 2.0表达式

//a[starts-with(@href,'file://START')]/following-sibling::node()
     [count(.| //a[starts-with(@href,'file://END')]/preceding-sibling::node())
     =
      count(//a[starts-with(@href,'file://END')]/preceding-sibling::node())
     ]
    //a[starts-with(@href,'file://START')]/following-sibling::node()
  intersect
    //a[starts-with(@href,'file://END')]/preceding-sibling::node()
<html>...
    <a href="file://START0"></a>
    <p>First nodes</p>
    <p>First nodes    
        <span>X</span>
    </p>
    <p>First nodes</p>
    <a href="file://END0"></a>...
</html>
<p>First nodes</p>
<p>First nodes    
        <span>X</span>
</p>
<p>First nodes</p>
<xsl:stylesheet version="2.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>

 <xsl:template match="/">
  <xsl:copy-of select=
  " //a[starts-with(@href,'file://START')]/following-sibling::node()
   intersect
    //a[starts-with(@href,'file://END')]/preceding-sibling::node()
  "/>
 </xsl:template>
</xsl:stylesheet>
XPath 2.0表达式使用XPath 2.0
intersect
运算符

XPath 1.0表达式使用Kayessian(在@Michael Kay之后)公式表示两个节点集的相交:

$ns1[count(.|$ns2) = count($ns2)]
使用XSLT进行验证

//a[starts-with(@href,'file://START')]/following-sibling::node()
     [count(.| //a[starts-with(@href,'file://END')]/preceding-sibling::node())
     =
      count(//a[starts-with(@href,'file://END')]/preceding-sibling::node())
     ]
    //a[starts-with(@href,'file://START')]/following-sibling::node()
  intersect
    //a[starts-with(@href,'file://END')]/preceding-sibling::node()
<html>...
    <a href="file://START0"></a>
    <p>First nodes</p>
    <p>First nodes    
        <span>X</span>
    </p>
    <p>First nodes</p>
    <a href="file://END0"></a>...
</html>
<p>First nodes</p>
<p>First nodes    
        <span>X</span>
</p>
<p>First nodes</p>
<xsl:stylesheet version="2.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>

 <xsl:template match="/">
  <xsl:copy-of select=
  " //a[starts-with(@href,'file://START')]/following-sibling::node()
   intersect
    //a[starts-with(@href,'file://END')]/preceding-sibling::node()
  "/>
 </xsl:template>
</xsl:stylesheet>
此XSLT 1.0转换:

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

 <xsl:template match="/">
  <xsl:copy-of select=
  "    //a[starts-with(@href,'file://START')]/following-sibling::node()
         [count(.| //a[starts-with(@href,'file://END')]/preceding-sibling::node())
         =
          count(//a[starts-with(@href,'file://END')]/preceding-sibling::node())
         ]
  "/>
 </xsl:template>
</xsl:stylesheet>

应用于提供的XML文档时

//a[starts-with(@href,'file://START')]/following-sibling::node()
     [count(.| //a[starts-with(@href,'file://END')]/preceding-sibling::node())
     =
      count(//a[starts-with(@href,'file://END')]/preceding-sibling::node())
     ]
    //a[starts-with(@href,'file://START')]/following-sibling::node()
  intersect
    //a[starts-with(@href,'file://END')]/preceding-sibling::node()
<html>...
    <a href="file://START0"></a>
    <p>First nodes</p>
    <p>First nodes    
        <span>X</span>
    </p>
    <p>First nodes</p>
    <a href="file://END0"></a>...
</html>
<p>First nodes</p>
<p>First nodes    
        <span>X</span>
</p>
<p>First nodes</p>
<xsl:stylesheet version="2.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>

 <xsl:template match="/">
  <xsl:copy-of select=
  " //a[starts-with(@href,'file://START')]/following-sibling::node()
   intersect
    //a[starts-with(@href,'file://END')]/preceding-sibling::node()
  "/>
 </xsl:template>
</xsl:stylesheet>
。。。
第一节点

第一节点 X

第一节点

...
生成所需的正确结果

//a[starts-with(@href,'file://START')]/following-sibling::node()
     [count(.| //a[starts-with(@href,'file://END')]/preceding-sibling::node())
     =
      count(//a[starts-with(@href,'file://END')]/preceding-sibling::node())
     ]
    //a[starts-with(@href,'file://START')]/following-sibling::node()
  intersect
    //a[starts-with(@href,'file://END')]/preceding-sibling::node()
<html>...
    <a href="file://START0"></a>
    <p>First nodes</p>
    <p>First nodes    
        <span>X</span>
    </p>
    <p>First nodes</p>
    <a href="file://END0"></a>...
</html>
<p>First nodes</p>
<p>First nodes    
        <span>X</span>
</p>
<p>First nodes</p>
<xsl:stylesheet version="2.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>

 <xsl:template match="/">
  <xsl:copy-of select=
  " //a[starts-with(@href,'file://START')]/following-sibling::node()
   intersect
    //a[starts-with(@href,'file://END')]/preceding-sibling::node()
  "/>
 </xsl:template>
</xsl:stylesheet>
第一个节点

第一节点 X

第一节点

此XSLT 2.0转换

//a[starts-with(@href,'file://START')]/following-sibling::node()
     [count(.| //a[starts-with(@href,'file://END')]/preceding-sibling::node())
     =
      count(//a[starts-with(@href,'file://END')]/preceding-sibling::node())
     ]
    //a[starts-with(@href,'file://START')]/following-sibling::node()
  intersect
    //a[starts-with(@href,'file://END')]/preceding-sibling::node()
<html>...
    <a href="file://START0"></a>
    <p>First nodes</p>
    <p>First nodes    
        <span>X</span>
    </p>
    <p>First nodes</p>
    <a href="file://END0"></a>...
</html>
<p>First nodes</p>
<p>First nodes    
        <span>X</span>
</p>
<p>First nodes</p>
<xsl:stylesheet version="2.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>

 <xsl:template match="/">
  <xsl:copy-of select=
  " //a[starts-with(@href,'file://START')]/following-sibling::node()
   intersect
    //a[starts-with(@href,'file://END')]/preceding-sibling::node()
  "/>
 </xsl:template>
</xsl:stylesheet>

当应用于同一个XML文档(如上)时,再次准确地生成所需的结果

我增加了一个我应该处理的特殊情况

为了处理这种特殊情况,您可以使用相同的方法,我的意思是使用Kayessian(也可以使用XPath可视化工具;-)。相交节点集的更改如下所示:

相交节点集C

    "//p[.//a[starts-with(@href,'file://START')]]
         /following-sibling::node()"
包含
a
START的
p
的以下所有兄弟姐妹

相交节点集D

"./following-sibling::p[.//a[starts-with(@href,'file://END')]]
    /preceding-sibling::node())"
包含
a
p
的所有先前同级和当前
p


现在,您可以按以下方式执行交叉:

C∩ D 就是

    "//p[.//a[starts-with(@href,'file://START')]]
            /following-sibling::node()[
            count(.| ./following-sibling::p
                     [.//a[starts-with(@href,'file://END')]]
                       /preceding-sibling::node())
            =
            count(./following-sibling::p
                   [.//a[starts-with(@href,'file://END')]]
                     /preceding-sibling::node())
            ]"

如果需要同时管理这两种情况,则可以按照以下步骤继续合并相交节点集:

(A)∩ (B)∪ (C)∩ D) 其中:

  • 必须使用XPath联合运算符
    |
  • 节点集A e B已在@Dimitre'答案中显示
  • 节点集C e D是我的答案中显示的那些

+1。即使当XPath 1.0 Kayessian公式起到帮助作用时我很清楚,但我总是很难意识到如何在各种情况下应用它;虽然我可以轻松阅读XPath2.0交叉点。不过,这个示例非常有用。@empo:您可以使用XPath Visualizer:)将两者可视化。我很惊讶HtmlAgilityPack SelectNodes方法不支持XPath 2.0。@Hernan:XPath 2.0实现很少,它们通常作为XSLT 2.0处理器或XQuery处理器的一部分出现——几乎从来都不是独立的。我们在这方面存在技术差距,随着XPath 3.0逐渐成为官方标准,iti将进一步扩大。Dimitry,我添加了一个我应该处理的特殊情况:PGood问题,+1。关于两种解决方案(XPath 1.0和XPath 2.0),请参见我的答案、解释以及使用XSLT作为XPath宿主进行验证。非常感谢!XPath2.0似乎使这些集合操作变得更加容易,不幸的是.NET3中没有2.0支持!似乎是这样。您可以编写
$n1 intersect$n2
来代替
$n1[count(.|$n2)=count($n2)]
。不管怎样,节点集的选择是很棘手的。@Dimitre:嗯,这意味着我做得非常好!:D谢谢