Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > XML > XSLT
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 .
DRM-free e-books 300x50
Reply
 
Thread Tools Display Modes
  #21 (permalink)  
Old February 26th, 2010, 05:40 AM
Authorized User
Points: 134, Level: 2
Points: 134, Level: 2 Points: 134, Level: 2 Points: 134, Level: 2
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
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
Reply With Quote
  #22 (permalink)  
Old February 26th, 2010, 07:52 AM
Friend of Wrox
Points: 6,414, Level: 34
Points: 6,414, Level: 34 Points: 6,414, Level: 34 Points: 6,414, Level: 34
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Nov 2007
Location: Germany
Posts: 1,192
Thanks: 0
Thanked 235 Times in 234 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
Reply With Quote
The Following User Says Thank You to Martin Honnen For This Useful Post:
Narendra.Patil (February 26th, 2010)
  #23 (permalink)  
Old February 26th, 2010, 09:50 AM
Authorized User
Points: 134, Level: 2
Points: 134, Level: 2 Points: 134, Level: 2 Points: 134, Level: 2
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Sep 2009
Posts: 30
Thanks: 9
Thanked 0 Times in 0 Posts
Default

You are Great... Thanks ...:)
Reply With Quote
  #24 (permalink)  
Old February 26th, 2010, 10:05 AM
Authorized User
Points: 134, Level: 2
Points: 134, Level: 2 Points: 134, Level: 2 Points: 134, Level: 2
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
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..
Reply With Quote
  #25 (permalink)  
Old February 26th, 2010, 10:12 AM
Friend of Wrox
Points: 6,414, Level: 34
Points: 6,414, Level: 34 Points: 6,414, Level: 34 Points: 6,414, Level: 34
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Nov 2007
Location: Germany
Posts: 1,192
Thanks: 0
Thanked 235 Times in 234 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
Reply With Quote
  #26 (permalink)  
Old March 2nd, 2010, 01:09 AM
Authorized User
Points: 134, Level: 2
Points: 134, Level: 2 Points: 134, Level: 2 Points: 134, Level: 2
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
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.
Reply With Quote
  #27 (permalink)  
Old March 2nd, 2010, 08:41 AM
Friend of Wrox
Points: 6,414, Level: 34
Points: 6,414, Level: 34 Points: 6,414, Level: 34 Points: 6,414, Level: 34
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Nov 2007
Location: Germany
Posts: 1,192
Thanks: 0
Thanked 235 Times in 234 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
Reply With Quote
The Following User Says Thank You to Martin Honnen For This Useful Post:
Narendra.Patil (March 3rd, 2010)
  #28 (permalink)  
Old February 24th, 2012, 06:50 PM
Authorized User
Points: 134, Level: 2
Points: 134, Level: 2 Points: 134, Level: 2 Points: 134, Level: 2
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
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 06:53 PM.
Reply With Quote
  #29 (permalink)  
Old February 25th, 2012, 10:34 AM
Friend of Wrox
Points: 6,414, Level: 34
Points: 6,414, Level: 34 Points: 6,414, Level: 34 Points: 6,414, Level: 34
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Nov 2007
Location: Germany
Posts: 1,192
Thanks: 0
Thanked 235 Times in 234 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
Reply With Quote
The Following User Says Thank You to Martin Honnen For This Useful Post:
Narendra.Patil (March 2nd, 2012)
  #30 (permalink)  
Old March 5th, 2012, 09:39 AM
Authorized User
Points: 134, Level: 2
Points: 134, Level: 2 Points: 134, Level: 2 Points: 134, Level: 2
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
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 09:46 AM.
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Grouping with XSLT 1.0 rodmcleay XSLT 1 January 14th, 2008 10: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 05:30 PM
XSLT Grouping Help Missy XSLT 0 December 14th, 2005 09:28 PM



All times are GMT -4. The time now is 09:42 AM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.