Hopefully simple XPath question

J

Jon Thackray

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

Joris Gillis

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,
 
J

Jon Thackray

(e-mail address removed) (Jon Thackray) wrote in message Snip

No answer required any more, I've figured it out.
 
J

Jeff Kish

Hi,


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
 
J

Joris Gillis

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,
 
J

Joris Gillis

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,
 
J

Jon Thackray

Joris Gillis said:
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
 
J

Joris Gillis

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,
 

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

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,999
Messages
2,570,243
Members
46,835
Latest member
lila30

Latest Threads

Top