XSLT Question

Just Curious

Usenet Archive
Joined
Nov 14, 2001
Messages
8
Reaction score
0
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
 

Andy Dingley

Usenet Archive
Joined
Feb 23, 2000
Messages
3,303
Reaction score
0
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. Bolmarcich

Usenet Archive
Joined
May 9, 2002
Messages
219
Reaction score
0
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.
 

Just Curious

Usenet Archive
Joined
Nov 14, 2001
Messages
8
Reaction score
0
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
548,042
Messages
3,104,223
Members
248,131
Latest member
Tonypshaw1

Latest Threads

Top