[xquery-talk] 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


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




More information about the talk mailing list