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 February 26th, 2010, 06:40 AM
Authorized User
 
Join Date: Sep 2009
Posts: 30
Thanks: 9
Thanked 0 Times in 0 Posts
Default 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
 
Old February 26th, 2010, 08:52 AM
Friend of Wrox
 
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

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:
Narendra.Patil (February 26th, 2010)
 
Old February 26th, 2010, 10:50 AM
Authorized User
 
Join Date: Sep 2009
Posts: 30
Thanks: 9
Thanked 0 Times in 0 Posts
Default

You are Great... Thanks ...:)
 
Old February 26th, 2010, 11:05 AM
Authorized User
 
Join Date: Sep 2009
Posts: 30
Thanks: 9
Thanked 0 Times in 0 Posts
Default

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..
 
Old February 26th, 2010, 11:12 AM
Friend of Wrox
 
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

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
 
Old March 2nd, 2010, 02:09 AM
Authorized User
 
Join Date: Sep 2009
Posts: 30
Thanks: 9
Thanked 0 Times in 0 Posts
Default

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.
 
Old March 2nd, 2010, 09:41 AM
Friend of Wrox
 
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

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:
Narendra.Patil (March 3rd, 2010)
 
Old February 24th, 2012, 07:50 PM
Authorized User
 
Join Date: Sep 2009
Posts: 30
Thanks: 9
Thanked 0 Times in 0 Posts
Default 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 07:53 PM..
 
Old February 25th, 2012, 11:34 AM
Friend of Wrox
 
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

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:
Narendra.Patil (March 2nd, 2012)
 
Old March 5th, 2012, 10:39 AM
Authorized User
 
Join Date: Sep 2009
Posts: 30
Thanks: 9
Thanked 0 Times in 0 Posts
Default 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 10:46 AM..





Similar Threads
Thread Thread Starter Forum Replies Last Post
Grouping with XSLT 1.0 rodmcleay XSLT 1 January 14th, 2008 11:42 PM
xslt grouping with attributes Jimi XSLT 4 October 31st, 2007 10:56 AM
Grouping based on attributes values Chamkaur XSLT 4 June 21st, 2006 05:51 AM
XSLT 1.0 Grouping kwilliams XSLT 0 January 11th, 2006 06:30 PM
XSLT Grouping Help Missy XSLT 0 December 14th, 2005 10:28 PM





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