[XQuery Talk Mailing List Archive Home] [By Date] [By Thread] [By Subject] [By Author] [Recent Entries] [Reply To This Message]

Getting node position in a for statement

Vallone, Philip Mr CTR USA AMC Philip.Vallone at us.army.mil
Mon Jun 16 09:05:27 PDT 2008


  Getting node position in a for statement
Hi List,

I am trying to use xquery to print out details about <tables> in my xml.


Take the following XML.

<root>
	<table>
		<title>Table 1 - Index</title>
		<tbody>
			<tr>
				<th>Index Number</th>
			</tr>
			<tr>
				<th>1</th>
			</tr>
		</tbody>
	</table>
	<table>
		<title>Table 2 - Dictionary</title>
		<tbody>
			<tr>
				<th>Word</th>
				<th>Definition</th>
			</tr>
			<tr>
				<th>Some word</th>
				<th>some definition</th>
			</tr>
			<tr>
				<th>Some word</th>
				<th>some definition</th>
			</tr>
			<tr>
				<th>Some word</th>
				<th>some definition</th>
			</tr>
		</tbody>
	</table>
</root>

My desired output would be to count the <tr> position within each
table/tbody:

<row position="1" cellcount="1">Table 1 - Index</row>
<row position="2" cellcount="1">Table 1 - Index</row>

<row position="1" cellcount="2">Table 2 - Dictionary</row>
<row position="2" cellcount="2">Table 2 - Dictionary</row>
<row position="3" cellcount="2">Table 2 - Dictionary</row>
<row position="4" cellcount="2">Table 2 - Dictionary</row>

Here is my xq expression so far:

for $rows in //table/tbody/tr
	let $cells := count($rows/th)
	let $title := $rows/ancestor::table/title
	let $pos := $rows/position()
return
<row position="{$pos}" cellcount="{$cells}" >{data($title)}</row>

It currently returns "1" for every position.

Thanks for the help.

Phil




PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.