[xsl] Getting info from certain nodes only

Subject: [xsl] Getting info from certain nodes only
From: "Jorge A. Salido" <jorgesalido@xxxxxxxxx>
Date: 24 Oct 2002 16:12:09 -0600

I've been going circles around this code for a while, so I thought to
give the list a try. I intend to display only some info from an xml
file, with a previously given configuration. In this case, the <header>
part defines the columns from the <records> that should be displayed. In
the example, field1 and field3 are the only ones that I want to see. 

I actually got this to work, but then the xsl sort was broken, and since
I need the sort, I've been looking for an alternate solution. The
closest I have come so far is to get the <columns> in a key, but then
I'm having problem getting the values back, since I have not found a way
to successfully reference them back. I used position() as the key value,
but it seems to be getting only the first column, but not the second
one, therefor I had to use an artificial "order" value, which I
currently do not like.

Also, since I have to go back to check out how many columns are there in
the table (kind of an iteration - I miss the while's and for's...), I
completely lose the sorting part. Is there also a way to count how many
items you have in a key element? Or perhaps a way to use a key in a
for-each statement? Any hints?


partial xml data file 

<column order="1" field="field1" align="left"> 
<title align="left">First</title> 
<column order="2" field="field3" align="left"> 
<title align="left">Second</title> 
<cell src="field1">John</cell> 
<cell src="field2">A</cell> 
<cell src="field3">Smith</cell> 
<cell src="field4"></cell> 
<cell src="field5"></cell> 
<cell src="field1">William</cell> 
<cell src="field2"></cell> 
<cell src="field3">Thompson</cell> 
<cell src="field4"></cell> 
<cell src="field5"></cell> 
<cell src="field1">Mark</cell> 
<cell src="field2"></cell> 
<cell src="field3">Martinez</cell> 
<cell src="field4"></cell> 
<cell src="field5"></cell> 
<cell src="field1">George</cell> 
<cell src="field2">S</cell> 
<cell src="field3">Wilson</cell> 
<cell src="field4"></cell> 
<cell src="field5"></cell> 
<cell src="field1">Charles</cell> 
<cell src="field2"></cell> 
<cell src="field3">Jackson</cell> 
<cell src="field4"></cell> 
<cell src="field5"></cell> 

partial xslt stylesheet 

<xsl:variable name="numberOfColumns"
<xsl:key name="showColumns" match="/data/header/column" use="@order" /> 

<xsl:template name="createRow"> 
<xsl:value-of select="count(records/row)"></xsl:value-of> 
<xsl:for-each select="records/row"> 
<xsl:sort select="cell[1]"/> 
<xsl:variable name="currentRow" select="position()"/> 
<tr class="fondo{position() mod 2}"> 
<xsl:for-each select="//column"> 
<xsl:variable name="currentCol" select="position()"/> 
<xsl:variable name="currentField" select="key('showColumns',
<b><xsl:value-of select="$currentField"/></b> 
<xsl:value-of select="//row[$currentRow]/cell[@src=$currentField]"/> 

current output
field1	John		field3Smith 
field1	William		field3Thompson 
field1	Mark		field3Martinez 
field1	George		field3Wilson 
field1	Charles		field3Jackson

 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list

Current Thread