XPath: Using the outside context inside a square bracket predicate

W

Wabiloo

Hi,

Although I've been using XPath for a long time, I'm stuck on what
seems to be a simple problem.

In the following document, I want to count the number of times a set
of nodes (identified through an attribute) is reused in the rest of
the document.

<PlayerConfiguration>
<Type name='Portrait'>
<Description>Portrait screens in PIPs</Description>
<PublicVariables>
<Var name='_ContentDiagnostics'>Off</Var>
<Var name='Show_TOC_ES'>0</Var>
<Var name='Show_TOC_EM'>0</Var>
<Var name='Show_TOC_FC'>0</Var>
<Var name='Show_TOC_SE'>0</Var>
</PublicVariables>
</Type>

<Player id='TEST-P' ip='192.168.86.60' type='Portrait'>
<PublicVariables>
<Var name='Show_TOC_ES'>1</Var>
<Var name='Show_TOC_EM'>1</Var>
<Var name='Show_TOC_FC'>1</Var>
<Var name='Show_TOC_SE'>1</Var>
</PublicVariables>
</Player>
<Player id='PIP1-P' ip='' type='Portrait'>
<PublicVariables>
<Var name='Show_Area_ARCD'>1</Var>
<Var name='Show_TravelInfo_Train'>1</Var>
<Var name='Show_Weather_National'>1</Var>
<Var name='Show_TravelInfo_NRE'>1</Var>
</PublicVariables>
</Player>
<Player id='PIP2a-P' ip='' ignore='0' type='Portrait'>
<PublicVariables>
<Var name='Show_Area_ARCD'>1</Var>
<Var name='Show_TOC_ES'>1</Var>
</PublicVariables>
</Player>
</PlayerConfiguration>

The following XPath is incorrect, as the ./@name is evaluated within
the context of the square bracket, and not in the outside context

//Type[1]/PublicVariables/Var/count(//Player/PublicVariables/
Var[@name=./@name])

The following XPath is also incorrect, as it is only valid in the
context of XSL (which is not the case for me):

//Type[1]/PublicVariables/Var/count(//Player/PublicVariables/
Var[@name=current()/@name])

Is there a way of getting this with a single XPath expression without
using XSL?

The output I'd expect is for each of the variables in the Type node at
the top (there's 4 of them), how many times they appear in the Player
nodes below (eg. 'Show_TOC_ES' appears twice).

Help much appreciated!
 
M

Martin Honnen

Wabiloo said:
//Type[1]/PublicVariables/Var/count(//Player/PublicVariables/
Var[@name=current()/@name])

Is there a way of getting this with a single XPath expression without
using XSL?

You seem to want to use XPath 2.0, right?
In that case
for $v in //Type[1]/PublicVariables/Var
return count(//Player/PublicVariables/Var[@name = $v/@name]
should do.

If you want to use XPath 1.0 then I don't understand the XPath
expressions you have posted.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top