Complicated XPATH to return a nodeset based on a sibling node's value

Discussion in 'XML' started by Brad, Aug 31, 2004.

  1. Brad

    Brad Guest

    Help, I have a really complicated XPATH request I can't wrap my head around

    I have an XML nodeset like this:

    <a>
    <a1 attr="key">Use</a1>
    <a1 attr="val">Value1</a2>
    </a>
    <a>
    <a1 attr="key">DontUse</a1>
    <a1 attr="val">Value2</a1>
    </a>
    <a>
    <a1 attr="key">Use</a1>
    <a1 attr="val">Value3</a1>
    </a>

    How do I form an XPATH that will return me the nodeset a/a1[@attr="Val"]
    only when a/a1[@attr="key"] is equal to "Use"??? IE, I want a nodeset with
    ["Value1", "Value3"]

    Thanks for any insight! This is driving me nuts! :-(
     
    Brad, Aug 31, 2004
    #1
    1. Advertising

  2. Brad

    Marrow Guest

    Hi Brad,

    Try something like...

    a/a1[@attr = 'val' and ../a1[@attr = 'key' and . = 'Use']]


    HTH
    Marrow
    http://www.marrowsoft.com - home of Xselerator (XSLT IDE and debugger)
    http://www.topxml.com/Xselerator


    "Brad" <> wrote in message
    news:7G6Zc.15897$...
    > Help, I have a really complicated XPATH request I can't wrap my head

    around
    >
    > I have an XML nodeset like this:
    >
    > <a>
    > <a1 attr="key">Use</a1>
    > <a1 attr="val">Value1</a2>
    > </a>
    > <a>
    > <a1 attr="key">DontUse</a1>
    > <a1 attr="val">Value2</a1>
    > </a>
    > <a>
    > <a1 attr="key">Use</a1>
    > <a1 attr="val">Value3</a1>
    > </a>
    >
    > How do I form an XPATH that will return me the nodeset a/a1[@attr="Val"]
    > only when a/a1[@attr="key"] is equal to "Use"??? IE, I want a nodeset

    with
    > ["Value1", "Value3"]
    >
    > Thanks for any insight! This is driving me nuts! :-(
    >
    >
     
    Marrow, Sep 1, 2004
    #2
    1. Advertising

  3. In article <7G6Zc.15897$>, Brad wrote:
    > Help, I have a really complicated XPATH request I can't wrap my head around
    >
    > I have an XML nodeset like this:
    >
    > <a>
    > <a1 attr="key">Use</a1>
    > <a1 attr="val">Value1</a2>
    > </a>
    > <a>
    > <a1 attr="key">DontUse</a1>
    > <a1 attr="val">Value2</a1>
    > </a>
    > <a>
    > <a1 attr="key">Use</a1>
    > <a1 attr="val">Value3</a1>
    > </a>
    >
    > How do I form an XPATH that will return me the nodeset a/a1[@attr="Val"]
    > only when a/a1[@attr="key"] is equal to "Use"??? IE, I want a nodeset with
    > ["Value1", "Value3"]
    >
    > Thanks for any insight! This is driving me nuts! :-(


    One way is to work from the root of the tree structure inward:

    - You want the a1 nodes with the right attribute and text values

    a/a1[@attr="key" and .="Use"]

    - Within those nodes you want the a1 nodes of the same parent

    a/a1[@attr="key" and .="Use"]/../a1

    with the right attribute value

    a/a1[@attr="key" and .="Use"]/../a1[@attr="val"]

    - Within those nodes you (maybe) want only the text.

    a/a1[@attr="key" and .="Use"]/../a1[@attr="val"]/text()
     
    A. Bolmarcich, Sep 1, 2004
    #3
  4. In article <7G6Zc.15897$>, Brad <> wrote:

    % How do I form an XPATH that will return me the nodeset a/a1[@attr="Val"]
    % only when a/a1[@attr="key"] is equal to "Use"??? IE, I want a nodeset with
    % ["Value1", "Value3"]


    First pick a

    a[a1/@attr = "key" and a1 = "Use"]

    then get the correct a1 from that

    a[a1/@attr = "key" and a1 = "Use"]/a1[@attr = "val"]

    --

    Patrick TJ McPhee
    East York Canada
     
    Patrick TJ McPhee, Sep 1, 2004
    #4
  5. In article <>,
    Patrick TJ McPhee <> wrote:

    % a[a1/@attr = "key" and a1 = "Use"]/a1[@attr = "val"]

    There's a subtle error here. It should be

    a[a1[@attr = "key" and . = "Use"]]/a1[@attr = "val"]

    You could read this as `the set of a1 elements with attribute attr equal
    to "val", whose parent element is an a element which has a child a1
    element, which has attribute attr equal to "key" and whose text content
    is "Use"'.

    The incorrect version might read `the set of a1 elements with attribute
    attr equal to "val", whose parent element is an a element which has a
    child a1 element which has attribute attr equal to "key" and which also
    has a child a1 element whose text content is "Use"'.

    Sorry.
    --

    Patrick TJ McPhee
    East York Canada
     
    Patrick TJ McPhee, Sep 2, 2004
    #5
  6. Brad

    Guest

    On Tuesday, August 31, 2004 11:09:07 PM UTC+1, Brad wrote:
    > Help, I have a really complicated XPATH request I can't wrap my head around
    >
    > I have an XML nodeset like this:
    >
    > <a>
    > <a1 attr="key">Use</a1>
    > <a1 attr="val">Value1</a2>
    > </a>
    > <a>
    > <a1 attr="key">DontUse</a1>
    > <a1 attr="val">Value2</a1>
    > </a>
    > <a>
    > <a1 attr="key">Use</a1>
    > <a1 attr="val">Value3</a1>
    > </a>
    >
    > How do I form an XPATH that will return me the nodeset a/a1[@attr="Val"]
    > only when a/a1[@attr="key"] is equal to "Use"??? IE, I want a nodeset with
    > ["Value1", "Value3"]
    >
    > Thanks for any insight! This is driving me nuts! :-(


    Detailed XPath tutorial here; http://www.liquid-technologies.com/xpath-tutorial.aspx
     
    , Jan 15, 2013
    #6
  7. Re: Complicated XPATH to return a nodeset based on a sibling node'svalue

    On 1/15/2013 8:55 AM, wrote:
    > On Tuesday, August 31, 2004 11:09:07 PM UTC+1, Brad wrote:
    >> Help, I have a really complicated XPATH request I can't wrap my head around
    >>
    >> I have an XML nodeset like this:
    >>
    >> <a>
    >> <a1 attr="key">Use</a1>
    >> <a1 attr="val">Value1</a2>
    >> </a>
    >> <a>
    >> <a1 attr="key">DontUse</a1>
    >> <a1 attr="val">Value2</a1>
    >> </a>
    >> <a>
    >> <a1 attr="key">Use</a1>
    >> <a1 attr="val">Value3</a1>
    >> </a>
    >>
    >> How do I form an XPATH that will return me the nodeset a/a1[@attr="Val"]
    >> only when a/a1[@attr="key"] is equal to "Use"??? IE, I want a nodeset with
    >> ["Value1", "Value3"]


    Assuming you meant "val", not "Val"... Try expressing it in simplest
    English form. "Find the a's whose key is 'Use' and return their val
    elements."

    a[a1[@attr="key"]="Use"]/a1[@attr="Val"]


    Note that this is a perfect example of why documents that use attributes
    to name the values are a Bad Practice. Your document should almost
    certainly have been something more like:

    <entry>
    <key>Use</key>
    <val>Value1</key>
    </entry>

    which would simplify your XPath to
    entry[key="use"]/val

    Much easier to maintain, and in many implementations will run
    significantly faster.


    --
    Joe Kesselman,
    http://www.love-song-productions.com/people/keshlam/index.html

    {} ASCII Ribbon Campaign | "may'ron DaroQbe'chugh vaj bIrIQbej" --
    /\ Stamp out HTML mail! | "Put down the squeezebox & nobody gets hurt."
     
    Joe Kesselman, Jan 15, 2013
    #7
    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. Kathy Burke
    Replies:
    0
    Views:
    713
    Kathy Burke
    Aug 3, 2003
  2. Alastair Cameron
    Replies:
    1
    Views:
    7,536
    SQL Server Development Team [MSFT]
    Jul 8, 2003
  3. Bryan Galvin
    Replies:
    2
    Views:
    449
    Bryan Galvin
    Sep 29, 2004
  4. Replies:
    11
    Views:
    675
    Fredrik Lundh
    Jan 23, 2006
  5. john
    Replies:
    2
    Views:
    269
Loading...

Share This Page