Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
|
XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old January 23rd, 2009, 04:19 PM
Registered User
 
Join Date: Jan 2009
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Question XML/XSLT Matrix

I have to create an xml file based on data I have in an oracle DB. I'm creating temp versions of the xml, xslt files so that I don't have to connect to the db.

I need to create this with XML & XSLT
Quote:
2000 2001 2002 2003
Went Fishing? W P
Went Hunting? D B
All my spaces are stripped out so the years should start in the cell after the questions.
W should be directly under 2000
P should be directly under 2002
D should be directly under 2001
B should be directly under 2003

This is just an example. The W & P stand for a type of fish and D & B stand for a hunted animal. Sorry if this is a bad example.

Does anyone know how I can do this?

Thank You for any help

Jerry

Last edited by jerry8989; January 23rd, 2009 at 04:39 PM..
 
Old January 23rd, 2009, 04:28 PM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

You need to show us what the input and output look like in XML terms.
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference
 
Old January 23rd, 2009, 04:45 PM
Registered User
 
Join Date: Jan 2009
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default

mhkay,
I'm kind of stuck at that point. I have this so far but I'm not sure how to transform it to get what I need.


Quote:

<?xmlversion="1.0"encoding="utf-8" ?>
<
DATA>
<
Years>
<
Year>All</Year>
</
Years>
<
Years>
<
Year>2000</Year>
</
Years>
<
Years>
<
Year>2001</Year>
</
Years>
<
Years>
<
Year>2002</Year>
</
Years>
<
Years>
<
Year>2003</Year>
</
Years><Row>
<
Question>
<
Path>Went Fishing?
</Path>
<
RowData>
<
Code>W</Code>
<
CodeYear>2000</CodeYear>
<Code>P</Code>
<
CodeYear>2002</CodeYear>
</
RowData>
</
Question>

<Question>
<
Path>Went Hunting?
</Path>
<
RowData>
<
Code>D</Code>
<
CodeYear>2001</CodeYear>
<Code>B</Code>
<
CodeYear>2003</CodeYear>
</
RowData>
</
Question>
</Row>

</
DATA>
This is the xslt so far

Quote:
<tableborder="1"bordercolor="black"style="width:900px;"align="center">
<
tr>
<
td></td>
<
xsl:for-eachselect="DATA/Years">
<
tdstyle="width:10px;" >
<
xsl:value-ofselect="Year"/>
</
td>
</
xsl:for-each>
</
tr>
<
xsl:for-eachselect="DATA/Row/Question">
<
tr>
<
td>
<
xsl:value-ofselect="Path"/>
</
td>
<
xsl:for-eachselect="RowData">
<
td>
<
xsl:value-ofselect="Code"/>
</
td>
</
xsl:for-each>
</
tr>
</
xsl:for-each>
</
table>
</
div>
</
xsl:template>
</
xsl:stylesheet>

Thank you for your help


 
Old January 23rd, 2009, 04:58 PM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

You need to generate output (table cells) that don't correspond to any input, so the stylesheet needs to be output-driven, not input-driven.

Something like

Code:
<xsl:variable name="DATA" select="."/>
<table>
  <tr>
    <xsl:for-each select="Years/Year">
      <td><xsl:value-of select="."/></td>
    </
  </

<xsl:for-each select="Question">
  <tr>
     <td><xsl:value-of select="Path"/></td>
     <xsl:variable name="row" select="."/>
     <xsl:for-each select="$DATA/Years/Year">
        <td><xsl:value-of select="$row/Code[following-sibling::CodeYear[1]=current()]"/></td>
     </
  </
</
</table>
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference

Last edited by mhkay; January 23rd, 2009 at 04:59 PM.. Reason: fix typos
 
Old January 23rd, 2009, 05:12 PM
Registered User
 
Join Date: Jan 2009
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default

mhkay,
Thank you for helping me.

I tried the code and it didn't display anything. It generates

<div>
<table>
<tr></tr>
</table>
</div>

XML
Quote:
<?xmlversion="1.0"encoding="utf-8" ?>
<
DATA>
<
Years>
<
Year>All</Year>
</
Years>
<
Years>
<
Year>2000</Year>
</
Years>
<
Years>
<
Year>2001</Year>
</
Years>
<
Years>
<
Year>2002</Year>
</
Years>
<
Years>
<
Year>2003</Year>
</
Years>
<
Row>
<
Question>
<
Path>
Went Fishing?
</Path>
<
RowData>
<
Code>W</Code>
<
CodeYear>2000</CodeYear>
<
Code>P</Code>
<
CodeYear>2002</CodeYear>
</
RowData>
</
Question>
<
Question>
<
Path>
Went Hunting?
</Path>
<
RowData>
<
Code>D</Code>
<
CodeYear>2001</CodeYear>
<
Code>B</Code>
<
CodeYear>2003</CodeYear>
</
RowData>
</
Question>
</
Row>
</
DATA>

XSLT
Quote:
<?xmlversion="1.0"encoding="utf-8"?>
<
xsl:stylesheetversion="1.0"xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<
xsl:outputmethod="html" />
<
xsl:templatematch="/">
<
div>
<
xsl:variablename="DATA"select="."/>
<
table>
<
tr>
<
xsl:for-eachselect="Years/Year">
<
td>
<
xsl:value-ofselect="."/>
</
td>
</
xsl:for-each>
</
tr>
<
xsl:for-eachselect="Question">
<
tr>
<
td>
<
xsl:value-ofselect="Path"/>
</
td>
<
xsl:variablename="row"select="."/>
<
xsl:for-eachselect="$DATA/Years/Year">
<
td>
<
xsl:value-ofselect="$row/Code[following-sibling::CodeYear[1]=current()]"/>
</
td>
</
xsl:for-each>
</
tr>
</
xsl:for-each>
</
table>
</
div>
</
xsl:template>
</
xsl:stylesheet>
Thank you again for all your help
Jerry








 
Old January 23rd, 2009, 05:22 PM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

Sorry, I should have spelt it out. The code is intended to run with DATA as the context node, so it goes within

<xsl:template match="DATA">

Otherwise

<xsl:variable name="DATA" select="."/>

isn't going to select the DATA element.
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference
 
Old January 23rd, 2009, 05:35 PM
Registered User
 
Join Date: Jan 2009
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default

mhkay,
Thank you for your reply. I changed it a bit and not I get the Years and the Path but I still can't get the correct code under the correct year. I'm looking at this chunk of code

Code:
<xsl:for-eachselect="DATA/Row/Question">
<tr>
<td>
<xsl:value-ofselect="Path"/>
</td>
<xsl:variablename="row"select="."/>
<xsl:for-eachselect="$DATA/Years/Year">
<td>
<xsl:value-ofselect="$row/Code[following-sibling::CodeYear[1]=current()]"/>
</td>
</xsl:for-each>
</tr>
</xsl:for-each>
Since Path is working I was looking at the "row" variable. Does the select="." set the variable equal to the row tag? or should it be RowData?

I really appreciate your help and patients working with a newbie

Last edited by jerry8989; January 23rd, 2009 at 05:37 PM.. Reason: incorrect word
 
Old January 23rd, 2009, 05:44 PM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

$row is the Question element. I missed a level in the path - it should be $row/RowData/Code[...]
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference
 
Old January 23rd, 2009, 05:50 PM
Registered User
 
Join Date: Jan 2009
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Still not displaying

What does this variable hold?

<xsl:variablename="row"select="."/>

I'm sure the code is right I just have to figure out the paths. I don't understand the select=".".

Thank You
 
Old January 26th, 2009, 10:34 AM
Registered User
 
Join Date: Jan 2009
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Talking Thanks

mhkay,
Thank you for everything. You helped so much. I truely appreciate all of your help.
I posted on another site and I it help as well. Here is the link for anyone else looking at this post.

http://social.msdn.microsoft.com/Forums/en-US/xmlandnetfx/thread/26b89b5f-4411-4ed5-962a-7c31e50b0b58


Thank You
Jerry







Similar Threads
Thread Thread Starter Forum Replies Last Post
Regarding xml-html transformation of an xml string using xslt and javascript suprakash444 XSLT 1 January 12th, 2009 01:23 AM
XSLT - MATRIX JohnnyP XSLT 0 March 12th, 2007 02:24 PM
xml and xsl templates as input to xslt gives xml rameshnarayan XSLT 5 August 3rd, 2005 01:58 AM
merge two xml file and make new xml using xslt ketan XSLT 0 September 21st, 2004 08:48 AM
Merge XML files into a xml file using xslt lxu XML 4 November 6th, 2003 06:01 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.