XPath 1.0:使用当前节点父节点的属性值来查找另一个匹配节点

时间:2016-03-07 17:55:11

标签: xpath xpath-1.0

我在这个问题上找到了许多相似的帖子,但没有任何内容可以解答这个具体问题。我必须使用XPath 1.0。我没有可用的XSLT(或XQuery或其他任何东西),我不能使用XPath 2.0。我正在从一个软件(Arbortext Styler)中执行这个XPath,我可以使用XPath 1.0从其他节点中选择内容,但在这种情况下XSLT不可用。另外,我无法控制源XML的结构。

当我在<step>的上下文中时,我需要能够匹配前一个过程/任务/步骤,该步骤的父过程与当前过程的@ref和@seq匹配并且具有字母“ “作为@conf的值。

<document>
  <topic>
    <procedure ref="056" seq="01" conf="A">
      <task>
        <step>1. Blah Blah (056-01-A)</step>
      </task>
    </procedure>
    <procedure ref="057" seq="02" conf="A">
      <task>
        <step>2. Blah blah (057-02-A)</step>
      </task>
    </procedure>
    <procedure ref="057" seq="02" conf="B">
        <task>
            <step>2. Blah blah (057-02-B)</step>
        </task>
    </procedure>
    <procedure ref="057" seq="03" conf="A">
        <task>
            <step>3. Blah blah (057-02-A)</step>
        </task>
    </procedure>
  </topic>
</document>

我需要的是这样的东西,但没有current()函数,软件应用程序不支持:

//procedure[@ref=current()/ancestor::procedure/@ref and @seq=current()/ancestor::procedure/@seq and @conf='A']/task/step

或类似的东西,但没有for in return语句:

for $ref in ancestor::procedure/@ref, $seq in ancestor::procedure/@seq return //topic/procedure[@ref=$ref and @seq=$seq and @conf='A']/task/step/text()

有没有人对如何使用XPath 1.0完成此操作有任何建议?请注意,程序的位置不能硬编码。重复的refs可以多次出现在任何位置。此外,要求使用<step>的起始上下文完成此匹配。

我怀疑我的问题的答案是无法完成,但我知道如果可以做到,这就是找到答案的地方!提前感谢所有考虑过这个问题的人。

这篇文章很相似,但搜索的是寻找起始情境的孩子:Xpath Getting All Nodes that Have an Attribute that Matches Another Node

这也很有趣,但我的属性值不是ID:Xpath: find an element value from a match of id attribute to id anchor

有什么建议吗?

1 个答案:

答案 0 :(得分:0)

正如Tomalak和Honza Hejzl所建议的那样,XPath 1.0无法做到这一点。感谢您的反馈。