XPath: Using the outside context inside a square bracket predicate

Discussion in 'XML' started by Wabiloo, Jan 25, 2008.

  1. Wabiloo

    Wabiloo Guest

    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!
     
    Wabiloo, Jan 25, 2008
    #1
    1. Advertising

  2. Wabiloo wrote:

    > //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.

    --

    Martin Honnen
    http://JavaScript.FAQTs.com/
     
    Martin Honnen, Jan 25, 2008
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    1
    Views:
    1,125
    Victor Bazarov
    May 4, 2007
  2. Replies:
    1
    Views:
    2,259
    Pavel Lepin
    Mar 14, 2008
  3. winterTTr
    Replies:
    2
    Views:
    363
    winterTTr
    Apr 29, 2009
  4. Asen Bozhilov

    Square bracket and dot notations?

    Asen Bozhilov, Jun 11, 2011, in forum: Python
    Replies:
    4
    Views:
    1,034
    Terry Reedy
    Jun 11, 2011
  5. Nalpo

    Object names with square bracket

    Nalpo, Aug 11, 2004, in forum: Javascript
    Replies:
    1
    Views:
    118
    lallous
    Aug 11, 2004
Loading...

Share This Page