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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
February 26th, 2010, 05:40 AM
Authorized User
Join Date: Sep 2009
Posts: 30
Thanks: 9
Thanked 0 Times in 0 Posts
Grouping XML using XSLT1.0
Dear Martin,
I have following XML file and I have to create an XSL file for it to genearate the HTML output.
<?xml version="1.0" encoding="utf-8"?>
<SalesChanges xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Country>India</Country>
<State>MP</State>
<City>Indore</City>
<Zip>452369</Zip>
<Created>
<string>Sales Order</string>
<string>Market Research</string>
</Created>
<Updated>
<string>Trends</string>
<string>Costs</string>
<string>Ranges</string>
</Updated>
<Deleted>
<string>Old Product</string>
</Deleted>
</SalesChanges>
Expected output is like
===========================
Country Name : India
State : MP
City : Indore
Zip : 452369
<table border=1>
<tr>
<td>
Created
</td>
<td>
<ul>
<li>Order</li>
<li>Market Research</li>
</ul>
</td>
</tr>
<tr>
<td>
Updated
</td>
<td>
<ul>
<li>Trends</li>
<li>Costs</li>
<li>Ranges</li>
</ul>
</td>
</tr>
<tr>
<td>
Deleted
</td>
<td>
<ul>
<li>Old Product</li>
</ul>
</td>
</tr>
</table>
===========================
Plesae help.
Thanks in advance
February 26th, 2010, 07:52 AM
Friend of Wrox
Join Date: Nov 2007
Location: Germany
Posts: 1,151
Thanks: 0
Thanked 222 Times in 221 Posts
Here is a sample XSLT 1.0 stylesheet:
Code:
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:output method="html" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="SalesChanges">
<xsl:apply-templates select="Country | State | City | Zip"/>
<table border="1">
<xsl:apply-templates select="Created | Updated | Deleted"/>
</table>
</xsl:template>
<xsl:template match="Country | State | City | Zip">
<xsl:value-of select="concat(local-name(), ' : ', .)"/>
<br/>
</xsl:template>
<xsl:template match="Created | Updated | Deleted">
<tr>
<td><xsl:value-of select="local-name()"/></td>
<td>
<ul>
<xsl:apply-templates/>
</ul>
</td>
</tr>
</xsl:template>
<xsl:template match="string">
<li><xsl:value-of select="."/></li>
</xsl:template>
</xsl:stylesheet>
__________________
Martin Honnen
Microsoft MVP (XML, Data Platform Development) 2005/04 - 2013/03
My blog
The Following User Says Thank You to Martin Honnen For This Useful Post:
February 26th, 2010, 09:50 AM
Authorized User
Join Date: Sep 2009
Posts: 30
Thanks: 9
Thanked 0 Times in 0 Posts
You are Great... Thanks ...:)
February 26th, 2010, 10:05 AM
Authorized User
Join Date: Sep 2009
Posts: 30
Thanks: 9
Thanked 0 Times in 0 Posts
Dear Martin,
If any of the value is blank then table is not well formatted. e.g.
Created and Deleted nodes are blank. Then how to make the table well formatted with border??
<?xml version="1.0" encoding="utf-8"?>
<SalesChanges xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Country>India</Country>
<State>MP</State>
<City>Indore</City>
<Zip>452369</Zip>
<Created />
<Updated>
<string>Trends</string>
<string>Costs</string>
<string>Ranges</string>
</Updated>
<Deleted />
</SalesChanges>
Please help..
February 26th, 2010, 10:12 AM
Friend of Wrox
Join Date: Nov 2007
Location: Germany
Posts: 1,151
Thanks: 0
Thanked 222 Times in 221 Posts
If you use
Code:
<table style="emtpy-cells: show;" border="1">...</table>
on your table element, do you get to see the cell borders then? Or which browser(s) do you target?
__________________
Martin Honnen
Microsoft MVP (XML, Data Platform Development) 2005/04 - 2013/03
My blog
March 2nd, 2010, 01:09 AM
Authorized User
Join Date: Sep 2009
Posts: 30
Thanks: 9
Thanked 0 Times in 0 Posts
Dear Martin,
For testing I use IE8 but it is not working for IE8.
Actually I have to tranform the XML to HTML and send it as EmailBody thru application.
Let me know if there is any other option
OR
If there is any way so that I will not show the empty nodes. In our example I will show only Updated node.
Thanks in advance.
March 2nd, 2010, 08:41 AM
Friend of Wrox
Join Date: Nov 2007
Location: Germany
Posts: 1,151
Thanks: 0
Thanked 222 Times in 221 Posts
If you don't want to process the empty nodes then use e.g.
Code:
<xsl:template match="SalesChanges">
<xsl:apply-templates select="Country | State | City | Zip"/>
<table border="1">
<xsl:apply-templates select="Created[*] | Updated[*] | Deleted[*]"/>
</table>
</xsl:template>
to process only those elements having child elements.
__________________
Martin Honnen
Microsoft MVP (XML, Data Platform Development) 2005/04 - 2013/03
My blog
The Following User Says Thank You to Martin Honnen For This Useful Post:
February 24th, 2012, 06:50 PM
Authorized User
Join Date: Sep 2009
Posts: 30
Thanks: 9
Thanked 0 Times in 0 Posts
XML-XSLT accessing values of another node based on values of different node InnerJoin
Hello,
I have an XML with following sample;
<?xml version="1.0"?>
<!DOCTYPE CustomerSurvey []>
<CustomerSurvey xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<Country>India</Country>
<Area>South-India</Area>
<Surveys>
<Survey>
<CustomerList>
<CustomerInfo>
<GeneralInformation>
<CustomerNumber>I101</CustomerNumber>
<CustomerName>R A Msey </CustomerName>
<SurveyStartOn>2010-07-01T00:00:00Z</SurveyStartOn>
<SurveyEndOn>2012-06-30T00:00:00Z</SurveyEndOn>
</GeneralInformation>
</CustomerInfo>
<CustomerInfo>
<GeneralInformation>
<CustomerNumber>I102</CustomerNumber>
<CustomerName>D K Bose </CustomerName>
<SurveyStartOn>2010-07-01T00:00:00Z</SurveyStartOn>
<SurveyEndOn>2012-06-30T00:00:00Z</SurveyEndOn>
</GeneralInformation>
</CustomerInfo>
</CustomerList>
</Survey>
<SupportingInformation/>
<QuestionAnswerList>
<QuestionAnswer>
<QuestionTextID>22</QuestionTextID>
<QuestionText>What do you think about it?</QuestionText>
<CustomerName>R A Msey</CustomerName>
<Answer>
<TextValue>Great Product</TextValue>
<BitValue xsi:nil="true"/>
<DateTimeValue xsi:nil="true"/>
<IntergerValue xsi:nil="true"/>
<DecimalValue xsi:nil="true"/>
<PercentageValue xsi:nil="true"/>
<Comment>comment 1</Comment>
</Answer>
</QuestionAnswer>
<QuestionAnswer>
<QuestionTextID>23</QuestionTextID>
<QuestionText>How is it?</QuestionText>
<CustomerName>R A Msey </CustomerName>
<Answer>
<TextValue>Not good</TextValue>
<BitValue xsi:nil="true"/>
<DateTimeValue xsi:nil="true"/>
<IntergerValue xsi:nil="true"/>
<DecimalValue xsi:nil="true"/>
<PercentageValue xsi:nil="true"/>
</Answer>
</QuestionAnswer>
<QuestionAnswer>
<QuestionTextID>24</QuestionTextID>
<QuestionText>Will you recommend?</QuestionText>
<CustomerName>R A Msey</CustomerName>
<Answer>
<TextValue>Best</TextValue>
<BitValue xsi:nil="true"/>
<DateTimeValue xsi:nil="true"/>
<IntergerValue xsi:nil="true"/>
<DecimalValue xsi:nil="true"/>
<PercentageValue xsi:nil="true"/>
</Answer>
</QuestionAnswer>
<QuestionAnswer>
<QuestionTextID>25</QuestionTextID>
<QuestionText>Comments any?</QuestionText>
<CustomerName>R A Msey</CustomerName>
<Answer>
<TextValue>NA</TextValue>
<BitValue xsi:nil="true"/>
<DateTimeValue xsi:nil="true"/>
<IntergerValue xsi:nil="true"/>
<DecimalValue xsi:nil="true"/>
<PercentageValue xsi:nil="true"/>
</Answer>
</QuestionAnswer>
<QuestionAnswer>
<QuestionTextID>22</QuestionTextID>
<QuestionText>What do you think about it?</QuestionText>
<CustomerName>D K Bose</CustomerName>
<Answer>
<TextValue>y</TextValue>
<BitValue xsi:nil="true"/>
<DateTimeValue xsi:nil="true"/>
<IntergerValue xsi:nil="true"/>
<DecimalValue xsi:nil="true"/>
<PercentageValue xsi:nil="true"/>
<Comment>comment 3.2</Comment>
</Answer>
</QuestionAnswer>
<QuestionAnswer>
<QuestionTextID>23</QuestionTextID>
<QuestionText>How is it?</QuestionText>
<CustomerName>D K Bose</CustomerName>
<Answer>
<TextValue>n</TextValue>
<BitValue xsi:nil="true"/>
<DateTimeValue xsi:nil="true"/>
<IntergerValue xsi:nil="true"/>
<DecimalValue xsi:nil="true"/>
<PercentageValue xsi:nil="true"/>
</Answer>
</QuestionAnswer>
<QuestionAnswer>
<QuestionTextID>24</QuestionTextID>
<QuestionText>Q30X0103</QuestionText>
<CustomerName>D K Bose</CustomerName>
<Answer>
<TextValue>No</TextValue>
<BitValue xsi:nil="true"/>
<DateTimeValue xsi:nil="true"/>
<IntergerValue xsi:nil="true"/>
<DecimalValue xsi:nil="true"/>
<PercentageValue xsi:nil="true"/>
</Answer>
</QuestionAnswer>
<QuestionAnswer>
<QuestionTextID>25</QuestionTextID>
<QuestionText>Comments any?</QuestionText>
<CustomerName>D K Bose </CustomerName>
<Answer>
<TextValue>Best</TextValue>
<BitValue xsi:nil="true"/>
<DateTimeValue xsi:nil="true"/>
<IntergerValue xsi:nil="true"/>
<DecimalValue xsi:nil="true"/>
<PercentageValue xsi:nil="true"/>
</Answer>
</QuestionAnswer>
<QuestionAnswer>
<QuestionTextID>26</QuestionTextID>
<QuestionText>Q30X0105</QuestionText>
<CustomerName>D K Bose</CustomerName>
<Answer>
<TextValue>Better</TextValue>
<BitValue xsi:nil="true"/>
<DateTimeValue xsi:nil="true"/>
<IntergerValue xsi:nil="true"/>
<DecimalValue xsi:nil="true"/>
<PercentageValue xsi:nil="true"/>
</Answer>
</QuestionAnswer>
</QuestionAnswerList>
</Surveys>
</CustomerSurvey>
I have to create an XSL which give output like
CustomerName R A Msey
Question -
Answer -
Question
Answer
CustomerName D K Bose
Question -
Answer -
Question
Answer
In short I have to join two nodes based on the Cutomer Name in future it might be name and CustomerID two values...
Anyone gone through similar case ...
Thanks in advance
Last edited by Narendra.Patil; February 24th, 2012 at 06:53 PM .
February 25th, 2012, 10:34 AM
Friend of Wrox
Join Date: Nov 2007
Location: Germany
Posts: 1,151
Thanks: 0
Thanked 222 Times in 221 Posts
With XSLT 2.0 (as supported by XSLT 2.0 processor like Saxon 9 or AltovaXML) you can do
Code:
<xsl:stylesheet
version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text"/>
<xsl:template match="/">
<xsl:for-each-group select="//QuestionAnswer" group-by="CustomerName">
<xsl:text>Customer Name </xsl:text>
<xsl:value-of select="current-grouping-key()"/>
<xsl:text>
</xsl:text>
<xsl:value-of select="current-group()/concat('Question ', QuestionText, '
Answer ', Answer/TextValue)" separator="
"/>
<xsl:text>
</xsl:text>
</xsl:for-each-group>
</xsl:template>
</xsl:stylesheet>
With XSLT 1.0 you will need to use Muenchian grouping as document here:
http://www.jenitennison.com/xslt/grouping/muenchian.xml
__________________
Martin Honnen
Microsoft MVP (XML, Data Platform Development) 2005/04 - 2013/03
My blog
The Following User Says Thank You to Martin Honnen For This Useful Post:
March 5th, 2012, 09:39 AM
Authorized User
Join Date: Sep 2009
Posts: 30
Thanks: 9
Thanked 0 Times in 0 Posts
XML - XSLT to Word/PDF
Hello Martin,
Even though the post is not directly related to XSLT; I am putting it here cause I came across one post which was given by...
http://forums.asp.net/t/1428622.aspx/1 .
I have two data
1. XML which is coming from the data base and
2. I have created XSLT matching my required output.
I want to process these two a convert into a Word & PDF files (around 80 pages of report).
I came across many third party software to convert XML to PDF but I wish do it through C#. Is there any way that I will be able to use XML and XSL to generate PDF or only way is to use XSL-FO?
Thanks in advance
Last edited by Narendra.Patil; March 5th, 2012 at 09:46 AM .
Thread Tools
Display Modes
Linear Mode
Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off