471,089 Members | 1,132 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,089 software developers and data experts.

question about create table by using xslt

Lee
I have a xml file, here is sample part:
<?xml version="1.0" encoding="UTF-8"?>
<ProducsList>
<Product id="1">
<SpecList>
<Spec>
<SpecLabel>Height</SpecLabel>
<SpecValue>10</SpecValue>
<SpecCat>Dimension</SpecCat>
</Spec>
<Spec>
<SpecLabel>Width</SpecLabel>
<SpecValue>6</SpecValue>
<SpecCat>Dimension</SpecCat>
</Spec>
<Spec>
<SpecLabel>Weight</SpecLabel>
<SpecValue>20.5</SpecValue>
<SpecCat>Weigth</SpecCat>
</Spec>
</SpecList>
</Prodcut>
<Product id="2">
<SpecList>
<Spec>
<SpecLabel>Height</SpecLabel>
<SpecValue>8</SpecValue>
<SpecCat>Dimension</SpecCat>
</Spec>
<Spec>
<SpecLabel>Width</SpecLabel>
<SpecValue>5</SpecValue>
<SpecCat>Dimension</SpecCat>
</Spec>
<Spec>
<SpecLabel>Weight</SpecLabel>
<SpecValue>18</SpecValue>
<SpecCat>Weigth</SpecCat>
</Spec>
</SpecList>
</Prodcut>
<Product id="3">
<SpecList>
<Spec>
<SpecLabel>Height</SpecLabel>
<SpecValue>5</SpecValue>
<SpecCat>Dimension</SpecCat>
</Spec>
<Spec>
<SpecLabel>Width</SpecLabel>
<SpecValue>2</SpecValue>
<SpecCat>Dimension</SpecCat>
</Spec>
<Spec>
<SpecLabel>Weight</SpecLabel>
<SpecValue>10</SpecValue>
<SpecCat>Weigth</SpecCat>
</Spec>
</SpecList>
</Prodcut>
</ProductsList>

I need to create a table look like that based on that xml file:

----------------------------------------------------
Label Product 1 product 2 product 3
----------------------------------------------------
Height 10 8 5
Width 6 5 2
Weight 20.5 18 10

I am totally losed, I don't know how to create that table. any one has
any idea about that? thank you very much.

Also how many Spec in SpecList is unkown.

Thanks for the help.

May 31 '07 #1
5 3623
here it is
<?xml version="1.0" encoding="utf-8"?>

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:template match="/">
<html>

<body>
<!--
This is an XSLT template file. Fill in this area with the
XSL elements which will transform your XML to XHTML.
-->
<table>
<tr>
<th>Label</th>
<xsl:for-each select="ProductsList/Product">
<th>
<xsl:value-of select="concat('Product',@id)"/>
</th>
</xsl:for-each>
</tr>
<xsl:for-each select="ProductsList/Product[1]/SpecList/Spec">
<xsl:variable name="spec" select="SpecLabel"></xsl:variable>
<tr>
<td>
<xsl:value-of select="SpecLabel"/>
</td>
<xsl:for-each select="/ProductsList/Product/SpecList/Spec[SpecLabel =
$spec]">
<td>
<xsl:value-of select="SpecValue"/>
</td>
</xsl:for-each>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>

</xsl:stylesheet>

Jun 6 '07 #2
On Jun 6, 5:50 pm, Luc Alquier <LucAlqu...@discussions.microsoft.com>
wrote:
here it is

<?xml version="1.0" encoding="utf-8"?>

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:template match="/">
<html>

<body>
<!--
This is an XSLT template file. Fill in this area with the
XSL elements which will transform your XML to XHTML.
-->
<table>
<tr>
<th>Label</th>
<xsl:for-each select="ProductsList/Product">
<th>
<xsl:value-of select="concat('Product',@id)"/>
</th>
</xsl:for-each>
</tr>
<xsl:for-each select="ProductsList/Product[1]/SpecList/Spec">
<xsl:variable name="spec" select="SpecLabel"></xsl:variable>
<tr>
<td>
<xsl:value-of select="SpecLabel"/>
</td>
<xsl:for-each select="/ProductsList/Product/SpecList/Spec[SpecLabel =
$spec]">
<td>
<xsl:value-of select="SpecValue"/>
</td>
</xsl:for-each>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>

</xsl:stylesheet>
I posted an earlier reply to the same problem in another group. You
may want to compare. I'm not a great advocate of intensive use of
xsl:for-each

<xsl:template match="ProducsList">
<table>
<tr>
<th>Label</th>
<xsl:apply-templates select="Product" mode="header"/>
</tr>
<xsl:apply-templates select="/ProducsList/Product/
SpecList/
Spec/SpecLabel[

not(

text()

=

ancestor::Product/preceding-sibling::Product//SpecLabel/text()
)

]"/

</table>
</xsl:template>

<xsl:template match="Product" mode="header">
<th>
<xsl:text>Product </xsl:text>
<xsl:value-of select="@id"/>
</th>
</xsl:template>

<xsl:template match="SpecLabel">
<xsl:variable name="text" select="text()"/>
<tr>
<th>
<xsl:value-of select="$text"/>
</th>
<xsl:apply-templates select="/ProducsList/Product">
<xsl:with-param name="label" select="$text"/>
</xsl:apply-templates>
</tr>
</xsl:template>

<xsl:template match="Product">
<xsl:param name="label"/>
<td>
<xsl:value-of select="SpecList/Spec[SpecLabel =
$label]/SpecValue"/>
</td>
</xsl:template>

outputs

<table>
<tr>
<th>Label</th><th>Product 1</th><th>Product 2</th><th>Product 3</th>
</tr>
<tr>
<th>Height</th><td>10</td><td>8</td><td>5</td>
</tr>
<tr>
<th>Width</th><td>6</td><td>5</td><td>2</td>
</tr>
<tr>
<th>Weight</th><td>20.5</td><td>18</td><td>10</td>
</tr>
</table>

Jun 6 '07 #3


<?xml version="1.0" encoding="utf-8"?>

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:template match="/">
<html>

<body>
<!--
This is an XSLT template file. Fill in this area with the
XSL elements which will transform your XML to XHTML.
-->
<table>
<tr>
<th>Label</th>
<xsl:for-each select="ProductsList/Product">
<th>
<xsl:value-of select="concat('Product',@id)"/>
</th>
</xsl:for-each>
</tr>
<xsl:for-each select="ProductsList/Product[1]/SpecList/Spec">
<xsl:variable name="spec" select="SpecLabel"></xsl:variable>
<tr>
<td>
<xsl:value-of select="SpecLabel"/>
</td>
<xsl:for-each select="/ProductsList/Product/SpecList/Spec[SpecLabel =
$spec]">
<td>
<xsl:value-of select="SpecValue"/>
</td>
</xsl:for-each>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>

</xsl:stylesheet>

Jun 7 '07 #4
<PRE>
<?xml version="1.0" encoding="utf-8"?>

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:template match="/">
<html>

<body>
<!--
This is an XSLT template file. Fill in this area with the
XSL elements which will transform your XML to XHTML.
-->
<table>
<tr>
<th>Label</th>
<xsl:for-each select="ProductsList/Product">
<th>
<xsl:value-of select="concat('Product',@id)"/>
</th>
</xsl:for-each>
</tr>
<xsl:for-each select="ProductsList/Product[1]/SpecList/Spec">
<xsl:variable name="spec" select="SpecLabel"></xsl:variable>
<tr>
<td>
<xsl:value-of select="SpecLabel"/>
</td>
<xsl:for-each select="/ProductsList/Product/SpecList/Spec[SpecLabel =
$spec]">
<td>
<xsl:value-of select="SpecValue"/>
</td>
</xsl:for-each>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>

</xsl:stylesheet>

</PRE>
Jun 7 '07 #5
Lee
On Jun 7, 1:18 am, Luc Alquier <LucAlqu...@discussions.microsoft.com>
wrote:
<PRE>
<?xml version="1.0" encoding="utf-8"?>

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:template match="/">
<html>

<body>
<!--
This is an XSLT template file. Fill in this area with the
XSL elements which will transform your XML to XHTML.
-->
<table>
<tr>
<th>Label</th>
<xsl:for-each select="ProductsList/Product">
<th>
<xsl:value-of select="concat('Product',@id)"/>
</th>
</xsl:for-each>
</tr>
<xsl:for-each select="ProductsList/Product[1]/SpecList/Spec">
<xsl:variable name="spec" select="SpecLabel"></xsl:variable>
<tr>
<td>
<xsl:value-of select="SpecLabel"/>
</td>
<xsl:for-each select="/ProductsList/Product/SpecList/Spec[SpecLabel =
$spec]">
<td>
<xsl:value-of select="SpecValue"/>
</td>
</xsl:for-each>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>

</xsl:stylesheet>

</PRE>
guys, thank you very much, all solutions work, I learned a lot from
you guys. Thanks.

Jun 7 '07 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Scott | last post: by
2 posts views Thread by gene.ellis | last post: by
4 posts views Thread by RJN | last post: by
2 posts views Thread by Adam dR. | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.