English 中文(简体)
XPath “追随先的兄弟姐妹”
原标题:XPath "following siblings before"

我正在尝试使用XPath 1.0(或可能是正则表达式)选择作为特定元素(b)的后续兄弟元素(a),但仅在另一个b元素之前。

<img><b>First</b><br>&nbsp;&nbsp;
<img>&nbsp;&nbsp;<a href="/first-href">First Href</a> - 19:30<br>
<img><b>Second</b><br>&nbsp;&nbsp;
<img>&nbsp;&nbsp;<a href="/second-href">Second Href</a> - 19:30<br>
<img>&nbsp;&nbsp;<a href="/third-href">Third Href</a> - 19:30<br>

我试图使样品尽可能接近现实世界。因此,在这种情况下,当我在元素中时

<b>First</b>

我需要选择

<a href="/first-href">First Href</a> 

当我在

<b>Second</b> 

我需要选择

<a href="/second-href">Second Href</a> 
<a href="/third-href">Third Href</a>

任何如何实现这一目标的想法? 谢谢!

最佳回答

积极创建这一疾病:

following-sibling::a[preceding-sibling::b[1][.= xxxx ]]

将xxxx替换为当前<b>的文本。

假设所有要素实际上都是aresiblings。 如果没有,你可以尝试使用<>teding和following axes,或撰写更具体、更类似于文件结构的XPath。

在XSLT中,你也可以使用:

following-sibling::a[
  generate-id(preceding-sibling::b[1]) = generate-id(current())
]
问题回答

这里有一个仅由一个XPath表达式构成的解决方案

采用两个分题的Kaysian公式ns1ns2:

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

我们只需要将$ns1替换为跟随当前<b>节点的<a>同级节点集合,并将$ns2替换为紧随下一个<b>节点的<a>同级节点集合。

这里是一个完整的变换,使用了这个。

<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:apply-templates select="*/b"/>
  </xsl:template>

  <xsl:template match="b">
    At: <xsl:value-of select="."/>

    <xsl:variable name="vNextB" select="following-sibling::b[1]"/>

    <xsl:variable name="vA-sAfterCurrentB" select="following-sibling::a"/>

    <xsl:variable name="vA-sBeforeNextB" select=
    "$vNextB/preceding-sibling::a
    |
     $vA-sAfterCurrentB[not($vNextB)]
    "/>

    <xsl:copy-of select=
     "$vA-sAfterCurrentB
              [count(.| $vA-sBeforeNextB)
              =
               count($vA-sBeforeNextB)
               ]
    "/>
  </xsl:template>
</xsl:stylesheet>

当这种转换应用于以下XML文档时:

<t>
    <img/>
    <b>First</b>
    <br />&#xA0;&#xA0;
    <img/>&#xA0;&#xA0;
    <a href="/first-href">First Href</a> - 19:30
    <br />
    <img/>
    <b>Second</b>
    <br />
    <img/>&#xA0;&#xA0;
    <a href="/second-href">Second Href</a> - 19:30
    <br />
    <img/>&#xA0;
    <a href="/third-href">Third Href</a> - 19:30
    <br />
</t>

正确的结果产生了。

   At: First <a href="/first-href">First Href</a>
    At: Second <a href="/second-href">Second Href</a>
<a href="/third-href">Third Href</a>




相关问题
VTD XML inner XPath Expressions

I have a xml file for example like this <root> <test> <bla>test1</bla> </test> <test> <bla>test2</bla> </test> <test> </test&...

C# XML adding/copying

With the following XML I am trying to copy and add to another XML but I haven t used the C# XML document objects before. So here is the XML <config> <configXML> <Connections&...

Filter SQL queries on the XML column using XPath/XQuery

I m having a table with one XML column. I d like to filter out the rows where a specific attribute in the XML match a string, essentially doing a WHERE or HAVING. The table looks something like this ...

Python XPath Result displaying only []

Hey I have just started to use Python recently and I want to use it with a bit of xPath, the thing is when I print the result of the query I only get [] and I don t know why =S import libxml2, ...

热门标签