Easy xml/xsl question (not easy for me though...)
I am working with an XML file, and trying to apply an XSL to it, so that I can get the formatting into tables, nice and neat looking... HOWEVER! There are parts of the XML that go like this..
<item_detail_records>
<product_id>12345</product_id>
<upc>00112233</upc>
<quantity>50</quantity>
<description>Bears</description>
<product_id>22223</product_id>
<upc>5544332211</upc>
<quantity>45</quantity>
<description>Tigers</description>
</item_detail_records>
Is there ANY way that I can display Product_ID, UPC, Quantity, and Description in a table, not knowing how many product_ids I will have??
Once I came upon this situation, I decided it might be better for me to use some forward only parser, but I really would like to use XSL because the XSL file is already half way created...
[973 byte] By [
bjswift] at [2007-11-19 9:34:43]

# 1 Re: Easy xml/xsl question (not easy for me though...)
So what you are saying is that item_detail_records may contain any number of product_id's and that the upc, quantity and description associated with each product_id is the one immidiaetly after the productid ?
First I think you should launch a manhunt for the guy who designed this, and have him thrown off a very tall building, or let him off by only breaking all the bones in his body and then force him to fix the XML format.
Is there ANY way that I can display Product_ID, UPC, Quantity, and Description in a table, not knowing how many product_ids I will have??
Sure you could run a for-each on the productid's, you can then use the position() function to get the position of the current productid and get the upc, quantity and description associated with it.
khp at 2007-11-10 3:27:29 >

# 2 Re: Easy xml/xsl question (not easy for me though...)
I agree with launching a manhunt. It has been a pain working with this data structure.
I will give the position function a try, thanks for your input!
# 3 Re: Easy xml/xsl question (not easy for me though...)
Ok. Maybe a little help here, I believe I understand the concept of having to use position(), let me know if I am way off...
I need to do:
<xsl:for-each select = "item_detail_records/product_id">
<tr><td><xsl:value-of select = "item_detail_records/product_id[position()]"/></td>
<tr><td><xsl:value-of select = "item_detail_records/upc[position()]"/></td>
<tr><td><xsl:value-of select = "item_detail_records/quantity[position()]"/></td>
</tr>
</xsl:for-each>
If I run this:
<xsl:for-each select = "item_detail_records/product_id">
<tr><td><xsl:value-of select = "position()"/></td></tr>
</xsl:for-each>
the results are
1
2
3
4
Which, makes me believe I am on track, but I am still unable to get any values associated with the nodes product_id, upc, quantity, or description.. I've tried select = "item_detail_records/product_id[1.....2.....3... and even 4] and that didn't work. HOWEVER, if I select "item_detail_records/product_id[1......2.....3......or 4] outside the for-each, the respected values show up!! I must not be navigating the data properly...
# 4 Re: Easy xml/xsl question (not easy for me though...)
Sigh, my linux box is acting up, and since I use that to run my xsl transforms I can't verify the code that I write atm, so maybe there will be a bug or two, you will have to weed out.
<xsl:value-of select = "item_detail_records/product_id[position()]"/>
This won't quite do, Functions called in an xpath expression are evaluated relative to their context in the xpath expression. and node[1] is really just short hand for node[position()=1] so your code would be evaluated as <xsl:value-of select = "item_detail_records/product_id[position()=position()]"/> which is of course a tautology.
You will need a variable to hold the position of the current product_id in the foreach and then refer to this in the other select statements.
something like <xsl:variable name="position" select="position()"/>
In addition the context inside the for-each changes to the current product_id node which means that relative xpaths like "item_detail_records/upc" won't work. You need a globally quallyfied path like "/item_detail_records/upc[$position]" asuming that item_detail_records is your document root, or you can use the parent axis to get to the other nodes, something like parent::item_detail_records/upc[$position].
khp at 2007-11-10 3:30:37 >

# 5 Re: Easy xml/xsl question (not easy for me though...)
Thank you thank you thank you!!
Your advice has helped me understand XML/XSL much better than I ever did before, and now I am able to transform this poorly designed xml scheme and display it the way I need to.
Again, thank you very much!