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

  2. Brad

    Marrow Guest

    Marrow, Sep 1, 2004
    #2
    1. Advertisements

  3. 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. % 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, Sep 1, 2004
    #4
  5. % 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, Sep 2, 2004
    #5
  6. Brad

    tahire72 Guest

    Detailed XPath tutorial here; http://www.liquid-technologies.com/xpath-tutorial.aspx
     
    tahire72, Jan 15, 2013
    #6
  7. 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. Advertisements

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 (here). After that, you can post your question and our members will help you out.