从已知子值中获取父属性

getting parent attribute from a known child's value

尝试根据一个或多个子元素的值 select 元素的属性,无论元素有多深。

想到了以下但没有用,感谢任何帮助,谢谢!

"//*[node()[contains(text(), 'FirstInfo')] and node()[contains(text(), 'SecondInfo')]/::parent]"

这是 XML:

<root>
<a ID="GetThisText">
  <b>
   <c>FirstInfo</c>

   <d>SecondInfo</d>
</b>
</a>
</root>

试试这个:

'//*[contains(., "FirstInfo")][contains(., "SecondInfo")]/ancestor::*/@ID'

或更好:

'//*[c="FirstInfo" and d="SecondInfo"]/ancestor::a/@ID'

使用以下路径表达式

//a[descendant::c = 'FirstInfo' and descendant::d = 'SecondInfo']/@ID

转换为

//a                                  Find all elements `a` no matter where in the XML
                                     tree
[descendant::c = 'FirstInfo'         But only return them if they have a descendant
                                     element 'c' with the value "FirstInfo"
and descendant::d = 'SecondInfo']    and a descendant element "d" with the value
                                     "SecondInfo"
/@ID                                 of those elements `a` return the element `ID`.

并导致

ID="GetThisText"