如何使用另一个给定的属性值访问元素上的命名属性

时间:2021-02-21 22:17:34

I am trying to come up with an XPath to the following XML:

我试图想出一个XPath到以下XML:

<A>
   <Target attr1="foo" attr2="bar"/>
   <B>
      <Target attr1="forest" attr2="gump"/>
      <C>
          <Target attr1="foo" attr2="aDifferentKindOfBar"/>    
      </C>
      <C/>
   </B>
</A>

It should give me the value of attribute named "attr2" for all elements Target that have an attribute "attr1" with value "foo".

它应该为所有元素Target提供名为“attr2”的属性值,该属性具有值为“foo”的属性“attr1”。

So in my example I would like to get "bar" and "aDifferentKindOfBar" but not "gump".

所以在我的例子中我想得到“bar”和“aDfereferentKindOfBar”而不是“gump”。

I came as far as:

我来到:

//Target[@attr1='foo']

But I can't figure out how to get the navigation to the attribute "attr2".

但我无法弄清楚如何导航到属性“attr2”。

1 个解决方案

#1


Just add the next step to the path:

只需在路径中添加下一步:

//Target[@attr1='foo']/@attr2

#1


Just add the next step to the path:

只需在路径中添加下一步:

//Target[@attr1='foo']/@attr2