moravas8051
moravas8051

Reputation: 23

Match nodes based on attribute refernce with XPath

given the following XML snippet:

<TypeB id="FD2020Q1YTD">
  <identifier>num1</identifier>
</TypeB>

<TypeA ref="FD2020Q1YTD">num1</TypeA>
<TypeA ref="FD2020Q1YTD_dei_LegalEntityAxis_pnw_ArizonaPublicServiceCompanyMember">num2</TypeA>

How can I query using XPath the following: I want to select all "TypeA" elements, where element's ref attribute points such a node where intensifier's inner text matchs to TypeA's inner text.

"num1" in "TypeA" should match to "num1" in "TypeB/identifier"

Update:

wp78de's suggestion seems to be the best:

//TypeA[.=//TypeB/identifier and @ref=//TypeB/@id]

but in this case, how can decide, that the first condition: .=//TypeB/identifier refers to the same TypeB instance, as the second condition: @ref=//TypeB/@id

Upvotes: 1

Views: 56

Answers (3)

wp78de
wp78de

Reputation: 18950

Try it like this:

//TypeA[.=//TypeB/identifier and @ref=//TypeB/@id]

Demo

Upvotes: 1

Alexandra Dudkina
Alexandra Dudkina

Reputation: 4462

Solution using XPath 2.0:

for $a in //TypeA, $b in //TypeB
    return if ($b/@id=$a/@ref and $a/text()=$b/identifier/text()) then $a else ()

Don't think that it's possible to solve with XPath 1.0.

Upvotes: 1

Jack Fleeting
Jack Fleeting

Reputation: 24930

I'm not sure I understand you correctly, but I believe you are looking for something like this:

//TypeA[text()=//TypeB/identifier/text()]

Output should be:

<TypeA ref="FD2020Q1YTD">num1</TypeA>

Upvotes: 1

Related Questions