Help with XPATH "position" of element

Mark Bordner

Usenet Archive
Joined
Feb 14, 2004
Messages
2
Reaction score
0
Hi All,

Given the following XML, I've been trying to figure out what XPATH expression will give me the "position" of a <lineitem> within the <shipto> that is its grandparent.

To give you an idea of what I'm trying to do...I'm transforming this XML into XSL-FO; which I will process with FOP to produce a PDF file. Each <shipto> element with the XML correlates to a new "section" within the target PDF document. I need to know when I've reached the last <lineitem> within a <shipto> so I can output some summary information.

The way I'm approaching this in the XSLT stylesheet is

1. Within the template for <lineitem>, I can use
"count(../..//lineitem)" to give me the total number of <lineitem>'s within this <shipto>. Then, I want to get the "position" of the current <lineitem> within the current <shipto>, which I can them compare against the total to see if this is the last <lineitem>.

I know the XPATH will be "count({something}) + 1", but I've just not had any luck determining what "{something}" should be.

Obviously, there could be (and probably is :) a better (or at least more elegant) solution to this; but it seems like this will work.

Thanks in advance for any assistance.

Regards,
Mark

<?xml version="1.0" encoding="UTF-8"?>
<shipment_coo>
<shipto code="1">
<invoice id="1">
<lineitem id="1"/>
<lineitem id="2"/>
<lineitem id="3"/>
<lineitem id="4"/>
</invoice>
<invoice id="2">
<lineitem id="1"/>
<lineitem id="2"/>
<lineitem id="3"/>
<lineitem id="4"/>
</invoice>
<invoice id="3">
<lineitem id="1"/>
<lineitem id="2"/>
<lineitem id="3"/>
<lineitem id="4"/>
</invoice>
</shipto>
<shipto code="2">
<invoice id="4">
<lineitem id="1"/>
<lineitem id="2"/>
<lineitem id="3"/>
<lineitem id="4"/>
</invoice>
<invoice id="5">
<lineitem id="1"/>
<lineitem id="2"/>
<lineitem id="3"/>
<lineitem id="4"/>
</invoice>
</shipto>
</shipment_coo>
 

Dimitre Novatchev MVP XML

Usenet Archive
Joined
Feb 11, 2004
Messages
43
Reaction score
0
Mark Bordner said:
Hi All,

Given the following XML, I've been trying to figure out what XPATH expression will give me the "position" of a <lineitem> within the <shipto> that is its grandparent.

To give you an idea of what I'm trying to do...I'm transforming this XML into XSL-FO; which I will process with FOP to produce a PDF file. Each <shipto> element with the XML correlates to a new "section" within the target PDF document. I need to know when I've reached the last <lineitem> within a <shipto> so I can output some summary information.

Use:

not(following-sibling::lineitem) and not(../following-sibling::invoice)

When the current node is a "lineitem" the above XPath expression will evaluate to true only when this "lineitem" element is the last "grandson" of its "grandfather".

Cheers,

Dimitre Novatchev [XML MVP],
FXSL developer, XML Insider,

http://fxsl.sourceforge.net/ -- the home of FXSL
Resume: http://fxsl.sf.net/DNovatchev/Resume/Res.html
 

Mark Bordner

Usenet Archive
Joined
Feb 14, 2004
Messages
2
Reaction score
0
Outstanding! I was thinking that there was a different way to look at this problem. Thank you very much for the solution and the insight.
Hi All,

Given the following XML, I've been trying to figure out what XPATH expression will give me the "position" of a <lineitem> within the <shipto> that is its grandparent.

To give you an idea of what I'm trying to do...I'm transforming this XML into XSL-FO; which I will process with FOP to produce a PDF file. Each <shipto> element with the XML correlates to a new "section" within the target PDF document. I need to know when I've reached the last <lineitem> within a <shipto> so I can output some summary information.

Use:

not(following-sibling::lineitem) and not(../following-sibling::invoice)

When the current node is a "lineitem" the above XPath expression will evaluate to true only when this "lineitem" element is the last "grandson" of its "grandfather".

Cheers,

Dimitre Novatchev [XML MVP],
FXSL developer, XML Insider,

http://fxsl.sourceforge.net/ -- the home of FXSL
Resume: http://fxsl.sf.net/DNovatchev/Resume/Res.html
 

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
548,208
Messages
3,104,798
Members
249,594
Latest member
henrywalsh

Latest Threads

Top