XSL select only nodes which contain a specific child node

Discussion in 'XML' started by William Krick, Jul 8, 2009.

  1. Given the following extremely simplified XML...

    <AA>
    <BB></BB>
    <BB><CC>foo</CC></BB>
    <BB></BB>
    <BB><CC>bar</CC></BB>
    </AA>

    ....is there an easy way to use and XSL select to get only the <BB>
    nodes which have a <CC> child node?

    <xsl:for-each select="AA/BB">

    Gives me 4 nodes:

    <BB></BB>
    <BB><CC>foo</CC></BB>
    <BB></BB>
    <BB><CC>bar</CC></BB>


    <xsl:for-each select="AA/BB/CC">

    Gives me 2 nodes, but it's at the CC level and I need it to be at the
    BB level because there's a bunch of other nodes there that need to be
    processed as well.

    <CC>foo</CC>
    <CC>bar</CC>


    I'm thinking that there must be some sort of conditional select XPATH
    that will get me what I need:

    <BB><CC>foo</CC></BB>
    <BB><CC>bar</CC></BB>


    Any tips?
     
    William Krick, Jul 8, 2009
    #1
    1. Advertising

  2. William Krick wrote:
    > Given the following extremely simplified XML...
    >
    > <AA>
    > <BB></BB>
    > <BB><CC>foo</CC></BB>
    > <BB></BB>
    > <BB><CC>bar</CC></BB>
    > </AA>
    >
    > ...is there an easy way to use and XSL select to get only the <BB>
    > nodes which have a <CC> child node?
    >
    > <xsl:for-each select="AA/BB">


    XSLT uses XPath to select nodes and with XPath you can do
    /AA/BB[CC]
    to select those 'BB' child elements of the 'AA' root element which have
    at least one 'CC' child element.

    With XPath 2.0 you could also write
    /AA/BB[exists(CC)]
    which might be clearer and more intuitive for beginners.

    --

    Martin Honnen
    http://msmvps.com/blogs/martin_honnen/
     
    Martin Honnen, Jul 8, 2009
    #2
    1. Advertising

  3. On Jul 8, 10:37 am, Martin Honnen <> wrote:

    > XSLT uses XPath to select nodes and with XPath you can do
    >    /AA/BB[CC]
    > to select those 'BB' child elements of the 'AA' root element which have
    > at least one 'CC' child element.


    Thank you, that's exactly what I needed.
    I knew what I wanted to do, I just didn't know the XPath syntax. :)
     
    William Krick, Jul 8, 2009
    #3
  4. William  Krick

    anntenna

    Joined:
    Mar 18, 2011
    Messages:
    2
    How to select a node in this case?

    I have an xml file:
    <AA>
    <BB>fgh</BB>
    <BB>
    <CC>asd</CC>
    <DD>KEY</DD>
    </BB>
    <BB>
    <CC>dsgf</CC>
    </AA>

    I need to select the BB nodes that have a child node CC and also that have a child node which has value "KEY", i.e. i should be able to select only the second BB node. Would this work?:

    <xsl:for-each select="AA/BB[CC and DD='KEY']">
    ...
    </xsl:for-each>
     
    anntenna, Mar 18, 2011
    #4
  5. William  Krick

    anntenna

    Joined:
    Mar 18, 2011
    Messages:
    2
    It works, never mind!
     
    anntenna, Mar 18, 2011
    #5
    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. Bryan Ax
    Replies:
    6
    Views:
    560
    Peter Flynn
    May 17, 2006
  2. Tjerk Wolterink
    Replies:
    2
    Views:
    1,478
    Dimitre Novatchev
    Aug 24, 2006
  3. Replies:
    3
    Views:
    3,909
    A. Bolmarcich
    Oct 20, 2006
  4. Replies:
    1
    Views:
    1,223
    Joe Kesselman
    Nov 1, 2006
  5. Luqman
    Replies:
    2
    Views:
    904
    Luqman
    Jun 5, 2007
Loading...

Share This Page