XPath:获取具有匹配属性值的元素

时间:2019-11-26 18:33:46

标签: xpath

我正在尝试从其@id属性与上下文节点的@idref匹配的元素获取内容。例如,给定以下xml(只是人为的示例)...

<doc>
   <toc>
      <entry idref="ch1"/>
      <entry idref="ch2"/>
   </toc>
   <body>
      <chapter id="ch1">
         <title>Chapter 1</title>
         <para/>
      </chapter>
      <chapter id="ch2">
         <title>Chapter 2</title>
         <para/>
      </chapter>
      <chapter id="ch3">
         <title>Chapter 3</title>
         <para/>
      </chapter>
   </body>
</doc>

如何从[entry]元素中获取[id]与当前@idref匹配的[chapter]中的[title]内容。

因此,基本上找到章节[其中@id =当前条目@idref] /标题

我尝试过

string(//chapter[@id = @idref]/title)
string(//chapter[@id = ./@idref]/title)
string(//chapter[@id = current()/@idref]/title)

一切都没有运气。

1 个答案:

答案 0 :(得分:1)

您可以在xml上尝试使用此表达式吗?

//chapter[@id=//toc/entry/@idref]/string-join((title,@id),' ')

输出:

Chapter 1 ch1
Chapter 2 ch2