Re: [xsl] Problem to place data into a specific <TD>

Subject: Re: [xsl] Problem to place data into a specific <TD>
From: Dimitre Novatchev <dnovatchev@xxxxxxxxx>
Date: Thu, 21 Dec 2000 08:27:09 -0800 (PST)
Hi Roel,

Sometimes this summer I had a similar problem.

Given the following xml document(pivot.xml):
------------------------------------------
<root>
	<data>
		<item id='1' name='age' value='25' />
		<item id='2' name='age' value='27' />
		<item id='1' name='cool' value='yes' />
		<item id='2' name='cool' value='no' />
	</data>
</root>

The following output had to be produced:
---------------------------------------
id,age,cool
1,25,yes
2,27,no

Below is the stylesheet that specifies this transformation. This is
very similar to your problem and I hope it can be of some help to you.

Dimitre.

The stylesheet (pivot.xsl):
---------------------------
<xsl:stylesheet version="1.0"
	xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
	xmlns:msxsl="urn:schemas-microsoft-com:xslt"
	>

	<xsl:output method="text"/>

	<xsl:variable name="Root" select="/"/> 
	
	<!-- Obtain all different "name"s -->
	<xsl:variable name="sortedNames">
		<xsl:for-each select="/root/data/item">
			<xsl:sort select="@name"/>
				<xsl:element name="name">
					<xsl:value-of select="@name"/>
				</xsl:element>
		</xsl:for-each>
	</xsl:variable>
	
	<xsl:variable name="uniqueNames">
		<xsl:for-each select="msxsl:node-set($sortedNames)/name[position()=1 
or . != preceding-sibling::name[1]]">
			<xsl:element name="name">
				<xsl:value-of select="."/>
			</xsl:element>
		</xsl:for-each>
	</xsl:variable>
	
	<!-- Obtain all different "id"s -->
	<xsl:variable name="sortedIds">
		<xsl:for-each select="/root/data/item">
			<xsl:sort select="@id"/>
				<xsl:element name="id">
					<xsl:value-of select="@id"/>
				</xsl:element>
		</xsl:for-each>
	</xsl:variable>
	
	<xsl:variable name="uniqueIds">
		<xsl:for-each select="msxsl:node-set($sortedIds)/id[position()=1 
or . != preceding-sibling::id[1]]">
			<xsl:element name="id">
				<xsl:value-of select="."/>
			</xsl:element>
		</xsl:for-each>
	</xsl:variable>
	
	
 

<xsl:template match="/">
	<!-- Output the "title" line -->
	<xsl:text>id,</xsl:text>
	<xsl:for-each select="msxsl:node-set($uniqueNames)/*">
		<xsl:value-of select="."/>
		<xsl:if test="position() != last()">
			<xsl:text>,</xsl:text>
		</xsl:if>
	</xsl:for-each>
	
	<!-- Output every "data" line -->
	<xsl:for-each select="msxsl:node-set($uniqueIds)/id">
		<xsl:variable name="thisId" select="."/>
		
		<!-- Output "NL" + "id" -->
		<xsl:text>&#xA;</xsl:text>
		<xsl:value-of select="."/>
		<xsl:text>,</xsl:text>
		
		<!-- Output the corresponding "value" for every "id" -->
		<xsl:for-each select="msxsl:node-set($uniqueNames)/name">
			<xsl:variable name="thisName" select="."/>
			<xsl:value-of select="$Root/root/data/item
[@id=$thisId][@name=$thisName][1]/@value"/>
			<xsl:if test="position() != last()">
				<xsl:text>,</xsl:text>
			</xsl:if>
		</xsl:for-each>
	</xsl:for-each>
	
</xsl:template>
</xsl:stylesheet>


Roel Mertens wrote:

I have the following problem.
Here my XML and my wanted output:


XML:
 
<Root>
<Article></Article>
<ID>1</ID>
<code type="A">1</code>
<code type="B">2</code>
<code type="C">3</code>
<Article></Article>
<ID>2</ID>
<code type="A">1</code>
<code type="C">3</code>
</Root>

I want this as output (with xsl)

        A    B    C
1      1     2    3
2      1           3

with a for-each the problem is the last code is placed under B like:

        A    B    C
1      1     2    3
2      1     3

What can be a solution ??
The code type is vaiable, I don't know whatr it is when I get the XML.
So it can also be E F G or something






__________________________________________________
Do You Yahoo!?
Yahoo! Shopping - Thousands of Stores. Millions of Products.
http://shopping.yahoo.com/

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


Current Thread