Question about create pivot table in xslt

L

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.
 
J

Joe Kesselman

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

Break it down into parts. Express it in English -- or whatever language
you're most comfortable in -- and then use that as a guide to writing
the XSLT.

1) Pick one line of the table. How do you retrieve all the values needed
for that line? (Hint: You'll need to use a predicate.) What do you want
to surround those values with in order to display them in that line? (If
you're producing an HTML table, that would be <td> elements.) What does
the line have to be surrounded with to make it display as a line? (If
you're producing HTML, that would be a <tr> element.)

2) Repeat for the other lines of the table.

3) What do you need to surround the lines with in order to display the
table? (If you're producing HTML, that would be a <table> element.)
 
L

Lee

Joe,

Thanks a lot for your reply. I am pretty new to xslt, so I don't quite
understand what you mean. do you have any sample codes for that?
Thanks again.
 
J

Joe Kesselman

Lee said:
Thanks a lot for your reply. I am pretty new to xslt, so I don't quite
understand what you mean. do you have any sample codes for that?

If you're asking that general a question, it sounds like you really need
to start by reading some XSLT tutorials. See my 5/29 post to the
"Transforming xhtml with xslt" thread for a list of good places to look
for information. In particular, IBM's site has many good tutorials and
articles, and the XSLT FAQ website has many examples worth studying.

See also http://www.catb.org/~esr/faqs/smart-questions.html -- I'm sure
you didn't intend it, but your question reads entirely too much like
"please do my homework assignment for me."
 
L

Lee

If you're asking that general a question, it sounds like you really need
to start by reading some XSLT tutorials. See my 5/29 post to the
"Transforming xhtml with xslt" thread for a list of good places to look
for information. In particular, IBM's site has many good tutorials and
articles, and the XSLT FAQ website has many examples worth studying.

See alsohttp://www.catb.org/~esr/faqs/smart-questions.html-- I'm sure
you didn't intend it, but your question reads entirely too much like
"please do my homework assignment for me."

Joe,

Thanks for the reply.
 
L

Lee

If you're asking that general a question, it sounds like you really need
to start by reading some XSLT tutorials. See my 5/29 post to the
"Transforming xhtml with xslt" thread for a list of good places to look
for information. In particular, IBM's site has many good tutorials and
articles, and the XSLT FAQ website has many examples worth studying.

See alsohttp://www.catb.org/~esr/faqs/smart-questions.html-- I'm sure
you didn't intend it, but your question reads entirely too much like
"please do my homework assignment for me."

Anybody else can help me out with this question? I know my knowledge
about xsl are really poor, but it is imposible for me to learn all
those stuff in one day. I will take some time to learn it step by
step. but for this particular question, I really need people with
experience give me some clue on how to deal with it. Thanks a lot!
 
L

Lee

Anybody else can help me out with this question? I know my knowledge
about xsl are really poor, but it is imposible for me to learn all
those stuff in one day. I will take some time to learn it step by
step. but for this particular question, I really need people with
experience give me some clue on how to deal with it. Thanks a lot!- Hide quoted text -

- Show quoted text -

or anybody knows there are already some discussion on the similar
question? Please let me know what keywords I should use to search this
question. Thanks a lot.
 
J

Joe Kesselman

or anybody knows there are already some discussion on the similar
question?

I already mentioned the Filk FAQ website, among other resources. Google
Is Your Friend. Your homework is YOUR homework.
 
J

Joe Kesselman

Sorry, crossed threads; let me corrct that...
or anybody knows there are already some discussion on the similar
question?

I already mentioned the XSLT FAQ website, among other resources. Google
Is Your Friend. You have a homework assignment.
 
L

Lee

Dimitre,

I found you are using msxsl namespace, is that gonna be a problem for
none-microsoft browsers? Thanks.- Hide quoted text -

- Show quoted text -

Thank you guys, I finially figured out how to do it. at the very
beginning I thought I can't use param in for-each, so I don't event
try to use variable, acutually, variable is OK to use in for-each
loop. so my xslt file is here:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/
Transform">
<xsl:eek:utput method="html" />
<xsl:template match="/">
<html>
<body>
<table border="1">
<xsl:for-each select="/ProductList/Product[position()=1]/SpecList/
Spec">
<xsl:variable name="label" select="SpecLabel"></xsl:variable>
<tr>
<td>
<xsl:value-of select="$label"/>
</td>

<xsl:for-each select="/ProductList/Product">
<td>
<xsl:apply-templates select="SpecList/Spec/SpecLabel[.=
$label]"/>
</td>
</xsl:for-each>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>

<xsl:template match="SpecLabel">
<xsl:value-of select="../SpecValue"/>
</xsl:template>

</xsl:stylesheet>
 
D

Dimitre Novatchev

Dimitre,
I found you are using msxsl namespace, is that gonna be a problem for
none-microsoft browsers? Thanks.

No, but for another processor one would normally use the exslt:node-set()
extension function instead.

Just a few days ago Julian Reschke and David Carlisle discovered a
technique, which allows to specify exslt:node-set() in a way that it can be
used with the MSXML xslt processor:

http://dpcarlisle.blogspot.com/2007/05/exslt-node-set-function.html

Cheers,
Dimitre Novatchev
 
L

Lee

No, but for another processor one would normally use the exslt:node-set()
extension function instead.

Just a few days ago Julian Reschke and David Carlisle discovered a
technique, which allows to specify exslt:node-set() in a way that it can be
used with the MSXML xslt processor:

http://dpcarlisle.blogspot.com/2007/05/exslt-node-set-function.html

Cheers,
Dimitre Novatchev






- Show quoted text -

Dimitre,

Thanks a lot for the help. It is an interesting article. Thanks.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,534
Members
45,007
Latest member
obedient dusk

Latest Threads

Top