selecting a range of nodes

Discussion in 'XML' started by Andy Fish, Aug 3, 2007.

  1. Andy Fish

    Andy Fish Guest

    hi,

    I am trying to select a range of following siblings using xslt 1.0. consider
    the following XML file

    <top>
    <a1/>
    <a2/>
    <foo/>
    <a3/>
    <a4/>
    <a5/>
    <a6/>
    <bar/>
    <a7/>
    <a8/>
    </top>

    now, inside this template

    <xsl:template match="foo">

    I would like to select all the nodes up to the following <bar> (if there are
    several <bar>s it would be the next one)

    it seems quite simple on the face of it but I can't figure out any way to
    get close at the moment

    TIA

    Andy
     
    Andy Fish, Aug 3, 2007
    #1
    1. Advertising

  2. In article <joCsi.15392$>,
    Andy Fish <> wrote:

    >I am trying to select a range of following siblings using xslt 1.0. consider
    >the following XML file
    >
    ><top>
    > <a1/>
    > <a2/>
    > <foo/>
    > <a3/>
    > <a4/>
    > <a5/>
    > <a6/>
    > <bar/>
    > <a7/>
    > <a8/>
    ></top>
    >
    >now, inside this template
    >
    ><xsl:template match="foo">
    >
    >I would like to select all the nodes up to the following <bar> (if there are
    >several <bar>s it would be the next one)


    It's a bit messy. You want the following-siblings of the foo that
    have a following-sibling which is the first bar after the foo. So
    set a variable $bar to following-sibling::bar[1], and use

    following-sibling::*[generate-id(following-sibling::bar[1]) = generate-id($bar)]

    In this case generate-id works better than counting the union because some
    of the elements may not have a following bar element.

    For more complicated examples you might want to use "Muenchian grouping"
    (see Google).

    -- Richard
    --
    "Consideration shall be given to the need for as many as 32 characters
    in some alphabets" - X3.4, 1963.
     
    Richard Tobin, Aug 3, 2007
    #2
    1. Advertising

  3. Andy Fish

    Pavel Lepin Guest

    Andy Fish <> wrote in
    <joCsi.15392$>:
    > <top>
    > <a1/>
    > <a2/>
    > <foo/>
    > <a3/>
    > <a4/>
    > <a5/>
    > <a6/>
    > <bar/>
    > <a7/>
    > <a8/>
    > </top>
    >
    > now, inside this template
    >
    > <xsl:template match="foo">
    >
    > I would like to select all the nodes up to the following
    > <bar> (if there are several <bar>s it would be the next
    > one)


    The need to do that often indicates a problem with the
    design of your XML document.

    > it seems quite simple on the face of it but I can't figure
    > out any way to get close at the moment


    Read up on generate-id() and current():

    following-sibling::*
    [
    generate-id(following-sibling::bar)=
    generate-id(current()/following-sibling::bar)
    ]

    Note that using keys may result in a significant performance
    boost.

    --
    ....the pleasure of obedience is pretty thin compared with
    the pleasure of hearing a rotten tomato hit someone in the
    rear end. -- Garrison Keillor
     
    Pavel Lepin, Aug 3, 2007
    #3
  4. Andy Fish

    Andy Fish Guest

    Thanks to both for the replies

    Works a treat

    "Pavel Lepin" <> wrote in message
    news:f8uu90$m0f$...
    >
    > Andy Fish <> wrote in
    > <joCsi.15392$>:
    >> <top>
    >> <a1/>
    >> <a2/>
    >> <foo/>
    >> <a3/>
    >> <a4/>
    >> <a5/>
    >> <a6/>
    >> <bar/>
    >> <a7/>
    >> <a8/>
    >> </top>
    >>
    >> now, inside this template
    >>
    >> <xsl:template match="foo">
    >>
    >> I would like to select all the nodes up to the following
    >> <bar> (if there are several <bar>s it would be the next
    >> one)

    >
    > The need to do that often indicates a problem with the
    > design of your XML document.
    >
    >> it seems quite simple on the face of it but I can't figure
    >> out any way to get close at the moment

    >
    > Read up on generate-id() and current():
    >
    > following-sibling::*
    > [
    > generate-id(following-sibling::bar)=
    > generate-id(current()/following-sibling::bar)
    > ]
    >
    > Note that using keys may result in a significant performance
    > boost.
    >
    > --
    > ...the pleasure of obedience is pretty thin compared with
    > the pleasure of hearing a rotten tomato hit someone in the
    > rear end. -- Garrison Keillor
     
    Andy Fish, Aug 3, 2007
    #4
    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. Olivier Wulveryck
    Replies:
    1
    Views:
    676
    Andrew Thompson
    Sep 14, 2004
  2. Timo Nentwig

    selecting nodes between other nodes

    Timo Nentwig, Jun 16, 2004, in forum: XML
    Replies:
    1
    Views:
    406
    Patrick TJ McPhee
    Jun 17, 2004
  3. Tjerk Wolterink
    Replies:
    9
    Views:
    1,250
    Joris Gillis
    Feb 7, 2005
  4. Ryan  Adler
    Replies:
    2
    Views:
    392
  5. Replies:
    1
    Views:
    574
    Soren Kuula
    Aug 24, 2006
Loading...

Share This Page