Help with XPATH "position" of element

M

Mark Bordner

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>
 
D

Dimitre Novatchev [MVP XML]

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
 
M

Mark Bordner

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

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,074
Latest member
StanleyFra

Latest Threads

Top