XSLT Question

J

Just Curious

Hello,

I have a following situation..
XML Document contains
<PurchaseOrder>
<LineItems>
<LineItem>
<Description></Description>
</LineItem>
<LineItem>
<Description></Description>
</LineItem>
</LineItems>
<Comments>
<Comment>
<NumberInCollection>1</NumberInCollection>
</Comment>
<Comment>
<NumberInCollection>2</NumberInCollection>
</Comment>
</Comments>
</PurchaseOrder>


When I am looping through Each Line Item using XSLT - I need to also
get corresponding comment for that line item - I have
NumberInCollection which ties with appropriate LineItem but I dont
know how to get this number when I am looping through Each LineItem.

Any Help will be very much apprecieated!

Thanks

AZXML
 
A

Andy Dingley

I have
NumberInCollection which ties with appropriate LineItem but I dont
know how to get this number when I am looping through Each LineItem.

This is a piece of poor DTD / Schema design - no wonder your XSLT is a
problem.


What connects these two nodes ? In your example, then there's nothing
robust that does it.


For a simple life with XML, then do it by containment:

<PurchaseOrder>
<LineItems>
<LineItem>
<Description></Description>
<Comment></Comment>
</LineItem>
<LineItem>
<Description></Description>
<Comment></Comment>
</LineItem>
</LineItems>
</PurchaseOrder>


If they _must_ be separated, then look at an Ordinal_Name / Reference
scheme. XML does this (badly) with ID and IDREF type attributes. RDF
does it in a more thought-through manner. Look at the RSS 1.0
protocol for a simple example.


In your particular case, then the XSLT is easy. However it's not
robust, because the ordering of the <LineItems> collection is not
reliable against being re-ordered. It would be much improved by some
form of "item number" attribute on each, then referring to that.

If you're stuck with the DTD as it stands, then investigate the
count() and position() functions within XSLT.
 
A

A. Bolmarcich

Hello,

I have a following situation..
XML Document contains
<PurchaseOrder>
<LineItems>
<LineItem>
<Description></Description>
</LineItem>
<LineItem>
<Description></Description>
</LineItem>
</LineItems>
<Comments>
<Comment>
<NumberInCollection>1</NumberInCollection>
</Comment>
<Comment>
<NumberInCollection>2</NumberInCollection>
</Comment>
</Comments>
</PurchaseOrder>


When I am looping through Each Line Item using XSLT - I need to also
get corresponding comment for that line item - I have
NumberInCollection which ties with appropriate LineItem but I dont
know how to get this number when I am looping through Each LineItem.

If I correctly understand the description, in the for-each on LineItem
elements you can use

<xsl:variable name="pos" select="position()"/>

to get the position of the current LineItem element being processed
by the for-each and use the XPATH expression

../following-sibling::Comments/Comment[NumberInCollection=$pos]

to get the corresponding Comment element.
 
J

Just Curious

Thank you that did it!

I am hoping that xslt does processing of XML file in a linear fashion
- meaning - while looping for:each - LineItem 1 gets processeed first,
LineItem 2 gets processed second and so forth from top down

Thank you again!
Hello,

I have a following situation..
XML Document contains
<PurchaseOrder>
<LineItems>
<LineItem>
<Description></Description>
</LineItem>
<LineItem>
<Description></Description>
</LineItem>
</LineItems>
<Comments>
<Comment>
<NumberInCollection>1</NumberInCollection>
</Comment>
<Comment>
<NumberInCollection>2</NumberInCollection>
</Comment>
</Comments>
</PurchaseOrder>


When I am looping through Each Line Item using XSLT - I need to also
get corresponding comment for that line item - I have
NumberInCollection which ties with appropriate LineItem but I dont
know how to get this number when I am looping through Each LineItem.

If I correctly understand the description, in the for-each on LineItem
elements you can use

<xsl:variable name="pos" select="position()"/>

to get the position of the current LineItem element being processed
by the for-each and use the XPATH expression

../following-sibling::Comments/Comment[NumberInCollection=$pos]

to get the corresponding Comment element.
 

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,997
Messages
2,570,239
Members
46,827
Latest member
DMUK_Beginner

Latest Threads

Top