Hopefully simple XPath question

Discussion in 'XML' started by Jon Thackray, Nov 10, 2004.

  1. Jon Thackray

    Jon Thackray Guest

    I'm trying to use XPath to enable me to number items within sections
    in a paper. I have so far tried about ten things, all of which it
    seemed to me should work, and none of which do. Essentially what I
    want is a count of the number of sections preceding the section which
    is (the unique section) ancestor of the current node. Functionally
    this would be

    #PrecedingSiblings(SectionAncestorOf(CurrentNode))

    But XPath doesn't seem to allow me to express this . I've no doubt it
    can, I just can't currently see how. Can somebody please enlighten me.
     
    Jon Thackray, Nov 10, 2004
    #1
    1. Advertising

  2. Jon Thackray

    Joris Gillis Guest

    Hi,

    > Essentially what I want is a count of the number ofsections preceding the section which is (the uniquesection) ancestor of the current node.


    In Xpath, it would be: count(preceding::section)

    but, it is more advisable to use the xslt approach, viz. 'xsl:number'
    so you should use something like: <xsl:number level="any" count="section"/>
    for full details, see http://www.w3.org/TR/xslt.html#number

    regards,
    --
    Joris Gillis (http://www.ticalc.org/cgi-bin/acct-view.cgi?userid=38041)
    Ceterum censeo XML omnibus esse utendum
     
    Joris Gillis, Nov 11, 2004
    #2
    1. Advertising

  3. Jon Thackray

    Jon Thackray Guest

    (Jon Thackray) wrote in message news:<>...
    Snip

    No answer required any more, I've figured it out.
     
    Jon Thackray, Nov 11, 2004
    #3
  4. Jon Thackray

    Jeff Kish Guest

    On 11 Nov 2004 13:48:55 -0800, (Jon Thackray) wrote:

    > (Jon Thackray) wrote in message news:<>...
    >Snip
    >
    >No answer required any more, I've figured it out.

    And the answer is... ?
     
    Jeff Kish, Nov 12, 2004
    #4
  5. Jon Thackray

    Jeff Kish Guest

    On Thu, 11 Nov 2004 18:56:29 GMT, "Joris Gillis" <> wrote:

    >Hi,
    >
    >> Essentially what I want is a count of the number ofsections preceding the section which is (the uniquesection) ancestor of the current node.

    >
    >In Xpath, it would be: count(preceding::section)
    >
    >but, it is more advisable to use the xslt approach, viz. 'xsl:number'
    >so you should use something like: <xsl:number level="any" count="section"/>
    >for full details, see http://www.w3.org/TR/xslt.html#number
    >
    >regards,

    Just out of curiosity, why is it more advisable? Just trying to learn here.
    Thanks
     
    Jeff Kish, Nov 12, 2004
    #5
  6. Jon Thackray

    Joris Gillis Guest

    >> but, it is more advisable to use the xslt approach, viz. 'xsl:number'
    > Just out of curiosity, why is it more advisable? Just trying to learn here.
    > Thanks
    >

    Hi,

    It was only my own humble opinion :) , I should have mentioned that...
    Since xslt defines a (flexible) numbering system (with quite advanced features), I see no need to invent a similar function (bound to very complicated when mimicing some features of 'xsl:number') by using Xpath. So, I prefer 'xsl:number' just to keep my code neat, flexible, extensible and comprehensible.

    You don't have to agree off course. I'm just learning too.

    regards,

    --
    Joris Gillis (http://www.ticalc.org/cgi-bin/acct-view.cgi?userid=38041)
    Ceterum censeo XML omnibus esse utendum
     
    Joris Gillis, Nov 12, 2004
    #6
  7. Jon Thackray

    Jon Thackray Guest

    "Joris Gillis" <> wrote in message news:<>...
    > Hi,
    >
    > > Essentially what I want is a count of the number ofsections preceding the section which is (the uniquesection) ancestor of the current node.

    >
    > In Xpath, it would be: count(preceding::section)


    That doesn't quite do it. I ended up with this

    <xsl:value-of select="1+count(ancestor::SECTION/preceding-sibling::SECTION)"/>

    > but, it is more advisable to use the xslt approach, viz. 'xsl:number'
    > so you should use something like: <xsl:number level="any" count="section"/>
    > for full details, see http://www.w3.org/TR/xslt.html#number


    I'll look into that.
     
    Jon Thackray, Nov 12, 2004
    #7
  8. Jon Thackray

    Joris Gillis Guest

    >> In Xpath, it would be: count(preceding::section)
    >
    > That doesn't quite do it. I ended up with this
    >
    > <xsl:value-of select="1+count(ancestor::SECTION/preceding-sibling::SECTION)"/>


    That's strange, I would have sworn that "count(ancestor::SECTION/preceding-sibling::SECTION)" gave the same result as "count(preceding::section)" in your xml (or what I understood of it).
    The '+1' change is logic off course.

    regards,
    --
    Joris Gillis (http://www.ticalc.org/cgi-bin/acct-view.cgi?userid=38041)
    Ceterum censeo XML omnibus esse utendum
     
    Joris Gillis, Nov 12, 2004
    #8
  9. Jon Thackray

    Jon Thackray Guest

    "Joris Gillis" <> wrote in message news:<>...
    > >> In Xpath, it would be: count(preceding::section)

    > >
    > > That doesn't quite do it. I ended up with this
    > >
    > > <xsl:value-of select="1+count(ancestor::SECTION/preceding-sibling::SECTION)"/>

    >
    > That's strange, I would have sworn that "count(ancestor::SECTION/preceding-sibling::SECTION)" gave the same result as "count(preceding::section)" in your xml (or what I understood of it).


    It went wrong in the second CHAPTER. Given document structure below,
    1+count(preceding::SECTION) gave

    CHAPTER
    SECTION Items numbered 1.1.x
    SECTION Items numbered 1.2.x
    SECTION Items numbered 1.3.x
    CHAPTER
    SECTION Items numbered 2.4.x

    Where I wanted

    CHAPTER
    SECTION Items numbered 1.1.x
    SECTION Items numbered 1.2.x
    SECTION Items numbered 1.3.x
    CHAPTER
    SECTION Items numbered 2.1.x

    > The '+1' change is logic off course.


    Just making it 1 based rather then 0 based
     
    Jon Thackray, Nov 13, 2004
    #9
  10. Jon Thackray

    Joris Gillis Guest

    > I wanted
    >
    > CHAPTER
    > SECTION Items numbered 1.1.x
    > SECTION Items numbered 1.2.x
    > SECTION Items numbered 1.3.x
    > CHAPTER
    > SECTION Items numbered 2.1.x


    OK, I see.

    with 'xsl:number', it would be something like:

    <xsl:number level="any" count="CHAPTER"/><xsl:text>.</xsl:text>
    <xsl:number level="any" from="CHAPTER" count="SECTION"/>

    regards,
    --
    Joris Gillis (http://www.ticalc.org/cgi-bin/acct-view.cgi?userid=38041)
    Ceterum censeo XML omnibus esse utendum
     
    Joris Gillis, Nov 13, 2004
    #10
    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. Chris Gardner
    Replies:
    0
    Views:
    363
    Chris Gardner
    Oct 29, 2003
  2. Replies:
    4
    Views:
    273
  3. Replies:
    1
    Views:
    251
  4. Chuft Captain

    A hopefully simple question

    Chuft Captain, Apr 9, 2009, in forum: Ruby
    Replies:
    5
    Views:
    165
    Chuft Captain
    Apr 9, 2009
  5. Harold Pritchett

    A hopefully simple question

    Harold Pritchett, Feb 16, 2005, in forum: Perl Misc
    Replies:
    6
    Views:
    136
    Chris Mattern
    Feb 16, 2005
Loading...

Share This Page