XPath or XSL Transform Help

Discussion in 'XML' started by Candle, Dec 11, 2006.

  1. Candle

    Candle Guest

    I am new to XML Programming and I am hoping someone can help me out
    with this.

    Assuming I have the following XML:

    <root>
    <E C="P_E1" O="E1" Y='True'>
    <A C="P_E1A1" V="7" Y='True'/>
    <A C="P_E1A2" V="8" />
    </E>
    <E C="P_E2" O="E1">
    <A C="P_E1A1" V="7" />
    <A C="P_E1A2" V="8" />
    </E>
    <E C="P_E3" O="3" Y='True'>
    <A C="P_E3A1" V="15" Y='True'/>
    <E C="P_E3E1" O="E1" Y='True'>
    <A C="P_E3E1A1" V="15" Y='True'/>
    <A C="P_E3E1A2" V="16" />
    <A C="P_E3E1A3" V="17" />
    <A C="P_E3E1A4" V="18" />
    <E C="P_E3E1E1" O="E1" Y='True'>
    <A C="P_E3E1E1A1" V="15" Y='True'/>
    </E>
    </E>
    </E>
    <E C="P_E4" O="4" >
    <A C="P_E4A1" V="19" />
    <A C="P_E4A2" V="20" />
    <E C="P_E4E1" O="E1" Y='True'>
    <A C="P_E4E1A1" V="15" Y='True'/>
    </E>
    </E>
    </root>

    Basically, I want an xPath that will select all of the top most E
    elements where Y='True'.
    When I write top most, I mean only the followng:
    - the parent, not children
    - single node, if there are no children

    So, for the example above, the following nodes would be selected:
    - <E C="P_E1" O="E1" Y='True'>
    - <E C="P_E3" O="3" Y='True'>
    - <E C="P_E4E1" O="E1" Y='True'>

    I assume this can be done; I just cannot get the syntax down.

    Here is what I have so far: //E [@Y='True']. However, this selects all
    of the nodes including the children.

    If this helps, the Y attribute will either be equal to True on not
    exist. As shown in the example above.

    After those nodes are selected, I need to transform the nodes using
    XSLT. I am mentioning this just in case the xPath cannot be written
    and XSLT needs to be used to select the nodes. Once the nodes are
    selected, I think I can take it from there.

    Thanks for your help.

    Regards,

    R
     
    Candle, Dec 11, 2006
    #1
    1. Advertising

  2. Candle wrote:
    > Basically, I want an xPath that will select all of the top most E
    > elements where Y='True'.


    Your only difficulty here is making "top most" something that's
    sufficiently explicit that you can express it in XSLT.

    One way to say this: You want all E elements whose Y attribute is
    "True", but whose parent's Y attribute is either absent or not "True".

    //E[@Y="True" and not(../@Y="True")]

    It's probably more efficient to rephrase this as: You want all E
    elements whose Y attribute is "True", that are children of an element
    whose Y attribute is either absent or not "True".

    //*[not(@Y="True")]/E[@Y="True"]

    If you know the parent is always going to be an E, it's probably even
    more efficient to say:

    //E[not(@Y="True")]/E[@Y="True"]

    This is assuming that once Y becomes True, it is True for all descendant
    E's below that point ... otherwise, this simple parent/child test
    applied to all available E's won't work, and you'll need a more explicit
    description of what "top most" means.



    --
    Joe Kesselman / Beware the fury of a patient man. -- John Dryden
     
    Joseph Kesselman, Dec 11, 2006
    #2
    1. Advertising

  3. Candle

    Candle Guest

    Joseph,

    Worked like a charm.

    Thx,

    R

    Joseph Kesselman wrote:
    > Candle wrote:
    > > Basically, I want an xPath that will select all of the top most E
    > > elements where Y='True'.

    >
    > Your only difficulty here is making "top most" something that's
    > sufficiently explicit that you can express it in XSLT.
    >
    > One way to say this: You want all E elements whose Y attribute is
    > "True", but whose parent's Y attribute is either absent or not "True".
    >
    > //E[@Y="True" and not(../@Y="True")]
    >
    > It's probably more efficient to rephrase this as: You want all E
    > elements whose Y attribute is "True", that are children of an element
    > whose Y attribute is either absent or not "True".
    >
    > //*[not(@Y="True")]/E[@Y="True"]
    >
    > If you know the parent is always going to be an E, it's probably even
    > more efficient to say:
    >
    > //E[not(@Y="True")]/E[@Y="True"]
    >
    > This is assuming that once Y becomes True, it is True for all descendant
    > E's below that point ... otherwise, this simple parent/child test
    > applied to all available E's won't work, and you'll need a more explicit
    > description of what "top most" means.
    >
    >
    >
    > --
    > Joe Kesselman / Beware the fury of a patient man. -- John Dryden
     
    Candle, Dec 12, 2006
    #3
    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. Geir S Eidissen
    Replies:
    0
    Views:
    1,203
    Geir S Eidissen
    Dec 27, 2004
  2. Replies:
    1
    Views:
    3,613
    A. Bolmarcich
    May 27, 2005
  3. jjouett
    Replies:
    7
    Views:
    1,138
    Harrie
    Oct 2, 2005
  4. Candle

    XSL Transform Help Please

    Candle, Dec 8, 2006, in forum: XML
    Replies:
    1
    Views:
    445
    Joseph Kesselman
    Dec 8, 2006
  5. Joe Fawcett
    Replies:
    0
    Views:
    362
    Joe Fawcett
    Nov 21, 2007
Loading...

Share This Page