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

August 30th, 2009, 08:58 PM
|
|
Authorized User
|
|
Join Date: Aug 2009
Posts: 31
Thanks: 16
Thanked 1 Time in 1 Post
|
|
Processing in groups of attribute type
Code:
<records>
<plan planid="1111" plantype="Type1">
</plan>
<plan planid="3333" plantype="Type1">
</plan>
<plan planid="2222" plantype="Type3">
</plan>
<plan planid="6666" plantype="Type2">
</plan>
<plan planid="4444" plantype="Type1">
</plan>
<plan planid="5555" plantype="Type2">
</plan>
</records>
What I would want to do is process the plan in groups of plantype, without knowing the distinct values of plantype.
Something like:
1. PROCESS(SELECT * where plantype='Type1')
2. PROCESS(SELECT * where plantype='Type2')
3. PROCESS(SELECT * where plantype='Type3')
How would I solve this kind of problem?
I've tried using the "key" clause but I'm failing miserably and having an error "This file is not valid: Unexepected child"
Thank you very much.
Last edited by kokoness; August 30th, 2009 at 09:14 PM..
Reason: Additional comments
|
|

August 31st, 2009, 02:05 AM
|
|
Friend of Wrox
|
|
Join Date: Jun 2008
Posts: 291
Thanks: 9
Thanked 29 Times in 29 Posts
|
|
Try this:
Code:
<xsl:key name="type" match="plan" use="@plantype"/>
<xsl:template match="records">
<xsl:copy>
<xsl:for-each select="plan[generate-id() = generate-id(key('type', @plantype)[1])]">
<xsl:sort select="@plantype"/>
<xsl:copy-of select="key('type', @plantype)"/>
</xsl:for-each>
</xsl:copy>
</xsl:template>
__________________
Rummy
|
|
The Following User Says Thank You to mrame For This Useful Post:
|
|
|

August 31st, 2009, 02:35 AM
|
|
Authorized User
|
|
Join Date: Aug 2009
Posts: 31
Thanks: 16
Thanked 1 Time in 1 Post
|
|
Code:
<?xml version="1.0" encoding="UTF-8"?>
<html xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xsl:version="1.0">
<xsl:key name="type" match="plan" use="@plantype"/>
<xsl:template match="records">
<xsl:copy>
<xsl:for-each select="plan[generate-id() = generate-id(key('type', @plantype)[1])]">
<xsl:sort select="@plantype"/>
<xsl:copy-of select="key('type', @plantype)"/>
</xsl:for-each>
</xsl:copy>
</xsl:template>
</html>
Hi Sir mrame,
I'm still unable to integrate it into my schema. I'm always getting "This file is not valid: Unexpected child" error when transforming.
My guess is that the key is not properly placed. It should not be inside a <html xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xsl:version="1.0"> tag. And it should rather be inside this kind of tag <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format">.
All of these are just guesses. I don't even know what the term is for those tags.
What I would really want is to display field contents of each plan seperated by tables in html format.
like:
Type1
Planid 1111
Planid 3333
Planid 4444
Type2
Planid 6666
Type3
Planid 2222
Planid 5555
You'll notice that I'm still learning xsl and haven't yet grasp the its fundamentals. It beats the crap out of me.
Thanks.
I'm reading XSLT for noobies right now :)
Last edited by kokoness; August 31st, 2009 at 02:48 AM..
Reason: Additional comments
|
|

August 31st, 2009, 03:03 AM
|
|
Friend of Wrox
|
|
Join Date: Jun 2008
Posts: 291
Thanks: 9
Thanked 29 Times in 29 Posts
|
|
You should get a note on basics of XSLT file structure.
Try the below:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format">
<xsl:key name="type" match="plan" use="@plantype"/>
<xsl:template match="records">
<html>
<xsl:copy>
<xsl:for-each select="plan[generate-id() = generate-id(key('type', @plantype)[1])]">
<xsl:sort select="@plantype"/>
<xsl:value-of select="@plantype"/>
<xsl:apply-templates select="key('type', @plantype)"></xsl:apply-templates>
<xsl:text> </xsl:text>
</xsl:for-each>
</xsl:copy>
</html>
</xsl:template>
<xsl:template match="plan">
Planid <xsl:value-of select="@planid"/>
</xsl:template>
</xsl:stylesheet>
This is how your xslt file should be.
Check it.
__________________
Rummy
|
|
The Following User Says Thank You to mrame For This Useful Post:
|
|
|

August 31st, 2009, 03:12 AM
|
|
Authorized User
|
|
Join Date: Aug 2009
Posts: 31
Thanks: 16
Thanked 1 Time in 1 Post
|
|
Yes sir! I'm now reading XSLT basics.
The code worked great!
Thank you again for your help. It's very much appreciated.
+888 points for you 
|
|

August 31st, 2009, 03:34 AM
|
|
Authorized User
|
|
Join Date: Aug 2009
Posts: 31
Thanks: 16
Thanked 1 Time in 1 Post
|
|
Sir, please bear with me for a little longer. I've been reading a lot of stuff but I just can't think how I could integrate the previous solution with what I have so far.
So far, I am able to produce what I need. Except for one thing, the grouping by type. You'll notice in my xsl, that regardless of plantype, all plans would be inside one table. I must now integrate the grouping by plan type, so that for each plan type, it must be in a different table.
In this case I have plantypes HCFAS and CAR. The expected output would be two tables instead of one. one table for each plan type.
Minimized XML File
Code:
<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" encoding='utf-8' href="mini.xsl"?>
<records>
<plan planid="AAAA" plantype="HCFAS" breadcrumb="Jan > Health Care > FAS 106 > 06-Jul-2009" plantypedescription="HC FAS">
<field>
<fieldId>A</fieldId>
<fieldtext>Health Care Fas 158</fieldtext>
<fieldvalue/>
<fieldvalue2>[NULL]</fieldvalue2>
<type>SectionTitle</type>
<sequence>1</sequence>
<stylemarker>0</stylemarker>
</field>
<field>
<fieldId>PlanName</fieldId>
<fieldtext>Name of Plan</fieldtext>
<fieldvalue>Battle Plan</fieldvalue>
<fieldvalue2>[NULL]</fieldvalue2>
<type>NormalField</type>
<sequence>2</sequence>
<stylemarker>1</stylemarker>
</field>
<field>
<fieldId>ResultsAsOf</fieldId>
<fieldtext>Result as of</fieldtext>
<!--fieldvalue><b>BATTLE PLAN<b></fieldvalue-->
<fieldvalue></fieldvalue>
<fieldvalue2>[NULL]</fieldvalue2>
<type>NormalField</type>
<sequence>3</sequence>
<stylemarker>0</stylemarker>
</field>
</plan>
<plan planid="BBBB" plantype="HCFAS" breadcrumb="Jan > Health Care > FAS 106 > 06-Jul-2009" plantypedescription="HC FAS">
<field>
<fieldId>A</fieldId>
<fieldtext>Health Care Fas 158</fieldtext>
<fieldvalue/>
<fieldvalue2>[NULL]</fieldvalue2>
<type>SectionTitle</type>
<sequence>1</sequence>
<stylemarker>0</stylemarker>
</field>
<field>
<fieldId>PlanName</fieldId>
<fieldtext>Name of Plan</fieldtext>
<fieldvalue>Battle Plan</fieldvalue>
<fieldvalue2>[NULL]</fieldvalue2>
<type>NormalField</type>
<sequence>2</sequence>
<stylemarker>1</stylemarker>
</field>
<field>
<fieldId>ResultsAsOf</fieldId>
<fieldtext>Result as of</fieldtext>
<!--fieldvalue><b>BATTLE PLAN<b></fieldvalue-->
<fieldvalue></fieldvalue>
<fieldvalue2>[NULL]</fieldvalue2>
<type>NormalField</type>
<sequence>3</sequence>
<stylemarker>0</stylemarker>
</field>
</plan>
<plan planid="AAAA" plantype="CAR" breadcrumb="Jan > Health Care > FAS 106 > 06-Jul-2009" plantypedescription="HC FAS">
<field>
<fieldId>A</fieldId>
<fieldtext>Health Care Fas 158</fieldtext>
<fieldvalue/>
<fieldvalue2>[NULL]</fieldvalue2>
<type>SectionTitle</type>
<sequence>1</sequence>
<stylemarker>0</stylemarker>
</field>
<field>
<fieldId>PlanName</fieldId>
<fieldtext>Name of Plan</fieldtext>
<fieldvalue>Battle Plan</fieldvalue>
<fieldvalue2>[NULL]</fieldvalue2>
<type>NormalField</type>
<sequence>2</sequence>
<stylemarker>1</stylemarker>
</field>
<field>
<fieldId>ResultsAsOf</fieldId>
<fieldtext>Result as of</fieldtext>
<!--fieldvalue><b>BATTLE PLAN<b></fieldvalue-->
<fieldvalue></fieldvalue>
<fieldvalue2>[NULL]</fieldvalue2>
<type>NormalField</type>
<sequence>3</sequence>
<stylemarker>0</stylemarker>
</field>
</plan>
<plan planid="BBBB" plantype="CAR" breadcrumb="Jan > Health Care > FAS 106 > 06-Jul-2009" plantypedescription="HC FAS">
<field>
<fieldId>A</fieldId>
<fieldtext>Health Care Fas 158</fieldtext>
<fieldvalue/>
<fieldvalue2>[NULL]</fieldvalue2>
<type>SectionTitle</type>
<sequence>1</sequence>
<stylemarker>0</stylemarker>
</field>
<field>
<fieldId>PlanName</fieldId>
<fieldtext>Name of Plan</fieldtext>
<fieldvalue>Battle Plan</fieldvalue>
<fieldvalue2>[NULL]</fieldvalue2>
<type>NormalField</type>
<sequence>2</sequence>
<stylemarker>1</stylemarker>
</field>
<field>
<fieldId>ResultsAsOf</fieldId>
<fieldtext>Result as of</fieldtext>
<!--fieldvalue><b>BATTLE PLAN<b></fieldvalue-->
<fieldvalue></fieldvalue>
<fieldvalue2>[NULL]</fieldvalue2>
<type>NormalField</type>
<sequence>3</sequence>
<stylemarker>0</stylemarker>
</field>
</plan>
</records>
Minimized XSL File
Code:
<?xml version="1.0"?>
<html xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xsl:version="1.0">
<head><title>Test</title></head>
<body>
<xsl:variable name="r" select="records"/>
<table border="1" width="100%">
<tr>
<td></td>
<xsl:for-each select="records/plan">
<td><xsl:value-of select="@breadcrumb"/></td>
</xsl:for-each>
<xsl:for-each select="records/plan">
<td><xsl:value-of select="@breadcrumb"/></td>
</xsl:for-each>
</tr>
<xsl:for-each select="records/plan[1]/field">
<xsl:variable name="p" select="position()"/>
<xsl:choose>
<xsl:when test="type = 'SectionTitle'">
<tr>
<td colspan='100%' bgcolor='#00BFFF'><xsl:value-of select="fieldtext"/></td>
</tr>
</xsl:when>
<xsl:when test="type = 'NormalField'">
<tr>
<!--td width='20%'><xsl:value-of select="fieldtext"/></td-->
<td width='20%' class="rowstyle1"><xsl:value-of select="fieldtext"/></td>
<!--td width='20%' bgcolor="{$style1}"><xsl:value-of select="fieldtext"/></td-->
<xsl:for-each select="$r/plan">
<td><xsl:value-of select="field[$p]/fieldvalue" disable-output-escaping="yes"/></td>
</xsl:for-each>
<xsl:for-each select="$r/plan">
<td><xsl:value-of select="'N/A'"/></td>
</xsl:for-each>
</tr>
</xsl:when>
</xsl:choose>
</xsl:for-each>
</table>
</body>
</html>
I know I should research thoroughly first before asking for help. It's just that I don't have much luxury of time right now because our client is really rushing things for us, and we have a very aggressive timeline. I'm really grateful for all the help I've been getting.
Last edited by kokoness; August 31st, 2009 at 04:42 AM..
Reason: Additional comments
|
|

August 31st, 2009, 04:57 AM
|
|
Friend of Wrox
|
|
Join Date: Jun 2008
Posts: 291
Thanks: 9
Thanked 29 Times in 29 Posts
|
|
As I have already posted change the above to
<?xml version='1.0'?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="/">
<html>
<head><title>Test</title></head>
<body>
<xsl:variable name="r" select="records"/>
...
and try.
__________________
Rummy
|
|

August 31st, 2009, 05:09 AM
|
|
Friend of Wrox
|
|
Join Date: Jun 2008
Posts: 291
Thanks: 9
Thanked 29 Times in 29 Posts
|
|
To get the desired output, please post the desired output xml, so that the round of questions and answers would be short.
__________________
Rummy
|
|

August 31st, 2009, 05:16 AM
|
|
Authorized User
|
|
Join Date: Aug 2009
Posts: 31
Thanks: 16
Thanked 1 Time in 1 Post
|
|
Yes, I've already tried your previous posted change and it worked given the sample html output it produced.
What I meant to say was given the code I have right now, I don't know where in my code I will need to insert this:
CODE 1
Code:
<xsl:copy>
<xsl:for-each select="plan[generate-id() = generate-id(key('type', @plantype)[1])]">
<xsl:sort select="@plantype"/>
<xsl:value-of select="@plantype"/>
<xsl:apply-templates select="key('type', @plantype)"></xsl:apply-templates>
<xsl:text> </xsl:text>
</xsl:for-each>
</xsl:copy>
From what I understand, it is this code which generates the html:
CODE 2
Code:
<xsl:template match="plan">
Planid <xsl:value-of select="@planid"/>
</xsl:template>
Given the html part of the code I made in my xsl, I'm trying to find a way how I could incorporate CODE 1.
Sorry if my question looks easy and basic enough. I'm really overwhelmed with the schema/structure of XSL.
|
|

August 31st, 2009, 05:20 AM
|
|
Authorized User
|
|
Join Date: Aug 2009
Posts: 31
Thanks: 16
Thanked 1 Time in 1 Post
|
|
This will be my expected html output
Code:
<html><head> <META http-equiv="Content-Type" content="text/html; charset=UTF-16"><title>Test</title></head><body><style>
.rowstyle1 { background-color: #D6F2EE;min-height: 50px;font-size: 10px;vertical-align: middle;padding-left: 4px;font-family: Arial, Helvetica, sans-serif;border-top: solid 1px black;border-bottom: solid 1px black;border-left: solid 1px black;border-right: solid 1px black;}
.rowstyle2 { background-color: #F5F5F5;min-height: 50px;font-size: 10px;vertical-align: middle;padding-left: 4px;font-family: Arial, Helvetica, sans-serif;border-top: solid 1px black;border-bottom: solid 1px black;border-left: solid 1px black;border-right: solid 1px black;' }
</style><table border="1" width="100%">
<tr>
<td /><td>Jan > Health Care > FAS 106 > 06-Jul-2009</td><td>Jan > Health Care > FAS 106 > 06-Jul-2009</td><td>Jan > Health Care > FAS 106 > 06-Jul-2009</td><td>Jan > Health Care > FAS 106 > 06-Jul-2009</td>
</tr>
<tr><td colspan="100%" bgcolor="#00BFFF">Health Care Fas 158</td>
</tr>
<tr><td width="20%" class="rowstyle1">Name of Plan</td><td>Battle Plan</td><td>Battle Plan</td><td>N/A</td><td>N/A</td>
</tr>
<tr><td width="20%" class="rowstyle1">Result as of</td><td></td><td></td><td>N/A</td><td>N/A</td>
</tr>
</table>
<table border="1" width="100%">
<tr>
<td /><td>Jan > Health Care > FAS 106 > 06-Jul-2009</td><td>Jan > Health Care > FAS 106 > 06-Jul-2009</td><td>Jan > Health Care > FAS 106 > 06-Jul-2009</td><td>Jan > Health Care > FAS 106 > 06-Jul-2009</td>
</tr>
<tr><td colspan="100%" bgcolor="#00BFFF">Car</td>
</tr>
<tr><td width="20%" class="rowstyle1">Name of Plan</td><td>Battle Plan</td><td>Battle Plan</td><td>N/A</td><td>N/A</td>
</tr>
<tr><td width="20%" class="rowstyle1">Result as of</td><td></td><td></td><td>N/A</td><td>N/A</td>
</tr>
</table> </body></html>
|
|
 |