Elias Giannopoulos
Elias Giannopoulos

Reputation: 144

XPath query node based on another node attribute

I have the following XML

<NETCASTING>
    <MATCHINFO>
        <TEAM Name="TEAM A" >
            <PLAYER Birth_date="1987.04.20" Height="1.98" Surname="Robinson" Number="4"/>
            <PLAYER Birth_date="1988.07.11" Height="1.84" Surname="Zhedik" Number="7"/>
            <PLAYER Birth_date="1986.01.27" Height="1.86" Surname="Kirillova" Number="9"/>
        </TEAM>
        <TEAM Name="TEAM B" >
            <PLAYER Birth_date="1986.12.28" Height="1.97" Surname="Lyttle" Number="2"/>
            <PLAYER Birth_date="1989.09.18" Height="1.94" Surname="Arteshina" Number="4"/>
            <PLAYER Birth_date="1987.05.11" Height="1.96" Surname="Belyakova" Number="5"/>
            <PLAYER Birth_date="1989.03.13" Height="1.99" Surname="Baric" Number="9"/>
        </TEAM>
    </MATCHINFO>
    <BOXSCORE>
        <TEAM>
            <PLAYER Number="4" Start="1" Points="11"/>
            <PLAYER Number="7" Start="0" Points="3"/>
            <PLAYER Number="9" Start="1" Points="0"/>
        </TEAM>
        <TEAM>
            <PLAYER Number="2" Start="0" Points="14"/>
            <PLAYER Number="4" Start="0" Points="2"/>
            <PLAYER Number="5" Start="1" Points="2"/>
            <PLAYER Number="9" Start="0" Points="0"/>
        </TEAM>
    </BOXSCORE>
</NETCASTING>

I need to get the Surname, Birth_date, Height, Number & Points of every player in 1st Team (/TEAM[1]) where the Start="1"

So I need to get:

<PLAYER Birth_date="1987.04.20" Height="1.98" Surname="Robinson" Number="4" Points="11"/>
<PLAYER Birth_date="1986.01.27" Height="1.86" Surname="Kirillova" Number="9" Points="0"/>

of for 2nd TEAM (/TEAM[2])

<PLAYER Birth_date="1987.05.11" Height="1.96" Surname="Belyakova" Number="5" Points="2"/>

Thanks in advance for your help

Upvotes: 2

Views: 61

Answers (2)

kjhughes
kjhughes

Reputation: 111541

XPath is for selection, not for transformation. (XSLT is for transformation.)

You can select only among PLAYER elements that exist in the input document. Since there are no PLAYER elements in the input XML with @Points attributes and the other attributes (@Birth_date etc), your request, including,

<PLAYER Birth_date="1987.04.20" 
        Height="1.98" 
        Surname="Robinson" 
        Number="4"
        Points="11"/>  <!-- Cannot add Points attribute -->

is impossible to do with XPath alone. You'll need help from the hosting language (XSLT, Python, Java, etc).

Upvotes: 1

har07
har07

Reputation: 89285

I don't think this can be done using single XPath expression. Assuming that a match info is always consists of 2 opposing teams, then you can use 2 XPath queries, one for each team, and join the result using union (|) operator :

/NETCASTING/MATCHINFO/TEAM[1]/PLAYER[
    @Number = /NETCASTING/BOXSCORE/TEAM[1]/PLAYER[@Start=1]/@Number
] |
/NETCASTING/MATCHINFO/TEAM[2]/PLAYER[
    @Number = /NETCASTING/BOXSCORE/TEAM[2]/PLAYER[@Start=1]/@Number
]

demo

output :

<PLAYER Birth_date="1987.04.20"
        Height="1.98"
        Surname="Robinson"
        Number="4"/>
<PLAYER Birth_date="1986.01.27"
        Height="1.86"
        Surname="Kirillova"
        Number="9"/>
<PLAYER Birth_date="1987.05.11"
        Height="1.96"
        Surname="Belyakova"
        Number="5"/>

Upvotes: 2

Related Questions