hazymat
hazymat

Reputation: 424

Select attribute depending on node's value

It's possible I'm not asking the question correctly, so please bear with me. I need to search for a node with a given value, and return one of its attributes.

The below is nonsense data, but it illustrates my point:

<Period type="Day" value="2016-01-18Z">
 <Rep D="ESE" F="-1" G="20" H="71" Pp="6" S="9" T="2" V="VG" W="7" U="0">0</Rep>
 <Rep D="SE" F="-1" G="18" H="77" Pp="9" S="9" T="3" V="VG" W="8" U="0">180</Rep>
 <Rep D="SE" F="-1" G="20" H="76" Pp="9" S="9" T="3" V="GO" W="8" U="0">360</Rep>
</Period>
<Period type="Day" value="2016-01-19Z">
 <Rep D="E" F="-2" G="9" H="77" Pp="3" S="4" T="0" V="GO" W="2" U="0">540</Rep>
 <Rep D="E" F="-3" G="9" H="80" Pp="5" S="4" T="0" V="GO" W="7" U="0">720</Rep>
 <Rep D="ENE" F="-3" G="9" H="85" Pp="4" S="4" T="-1" V="GO" W="2" U="0">900</Rep>
</Period>

I understand that if I wish to select e.g. the attribute W from the second Rep value from the second Period, my xsl would look like this:

<xsl:value-of select="Period[2]/Rep[2]/@W">

This would return: 7

This is the value I need to return, however the XML contents change depending on the time of day and I need to select it according to the Rep's own value of 720 instead of its position in the tree.

I've messed around trying the following, clearly I'm barking up the wrong tree:

<xsl:value-of select="Period[2]/Rep['180']/@W" />
<xsl:value-of select="Period[2]/Rep[Rep='180']/@W" />
<xsl:value-of select="Period[2]/'180'/@W" />

Upvotes: 1

Views: 45

Answers (2)

kjhughes
kjhughes

Reputation: 111726

If this works for you to select 7 based upon position,

<xsl:value-of select="Period[2]/Rep[2]/@W">

then use this to select 7 based upon content:

<xsl:value-of select="Period[@value = '2016-01-19Z']/Rep[. = 720]/@W">

Or, if Period is irrelevant:

<xsl:value-of select=".//Rep[. = 720]/@W">

Upvotes: 2

har07
har07

Reputation: 89325

"I need to select it according to the Rep's own value of 720 instead of its position in the tree"

Basically, you can use . to reference current context node. So this is one possible way :

<xsl:value-of select="Period[2]/Rep[.=720]/@W" />

Upvotes: 2

Related Questions