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
  #1 (permalink)  
Old March 10th, 2008, 01:03 PM
Registered User
 
Join Date: Mar 2008
Location: , , USA.
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default Create generic XSL Template to create table

Hi,

I want a construct a dynamic table using XSLT template, I want the template to be generic. I want to pass column headers and the child nodes from which the column values to be selected. I'm able to pass the repeating element (e.g.) <People> dynamically and loop through the same, but I'm not able to pass nodes name and Title dynamically, I have to hardcode them in template....if I can pass this as generic, then I can use this template for any 2-columned table.

I'm using <xsl:with-param> to pass the repeating element, but when I use the same for child nodes also, it is not going to the appropriate child node of the parent context node.

Can you please help me with this one.

Requirement: One template should help me convert both the XML's, into a table.

<?xml version"1.0"?>
<Employees>
    <People>
        <Name> ABC </Name><Title> Analyst</Title>
    </People>
    <People><Name> GHI </Name><Title> Manager </Title>
    </People>
</Employees>

or
<TopExecutives>
     <Executive>
         <Name> XYZ </Name><Title> CEO </Title>
     </Executive>
     <Executive>
         <Name> MNO </Name><Title> CFO </Title>
     </Executive>
</TopExecutives>


Reply With Quote
  #2 (permalink)  
Old March 10th, 2008, 01:07 PM
Friend of Wrox
Points: 6,522, Level: 34
Points: 6,522, Level: 34 Points: 6,522, Level: 34 Points: 6,522, Level: 34
Activity: 50%
Activity: 50% Activity: 50% Activity: 50%
 
Join Date: Nov 2007
Location: Germany
Posts: 1,220
Thanks: 0
Thanked 237 Times in 236 Posts
Default

Do you want to transform XML to XML or XML to HTML? Can you show us the result you want the stylesheet to produce from your two XML samples?

Reply With Quote
  #3 (permalink)  
Old March 10th, 2008, 01:14 PM
Registered User
 
Join Date: Mar 2008
Location: , , USA.
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I want the output to be XSL-FO table.....you can visualize it as HTML table...

If XML-1 is passed, the output from XSL should be:
Name Title
ABC Analyst
DEF Manager

If XML-2, the output from XSL should be:
Name Title
XYZ CEO
MNO CFO

Reply With Quote
  #4 (permalink)  
Old March 10th, 2008, 01:23 PM
Friend of Wrox
Points: 6,522, Level: 34
Points: 6,522, Level: 34 Points: 6,522, Level: 34 Points: 6,522, Level: 34
Activity: 50%
Activity: 50% Activity: 50% Activity: 50%
 
Join Date: Nov 2007
Location: Germany
Posts: 1,220
Thanks: 0
Thanked 237 Times in 236 Posts
Default

Unfortunately I don't know XSL-FO (tables) but I will provide a stylesheet that creates a HTML table, solely based on the element level, the root element is transformed to the table element, the children of the root element are transformed to table rows and the grandchildren to table cells:

Code:
<xsl:stylesheet
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  version="1.0">

  <xsl:output method="html" indent="yes"/>

  <xsl:template match="/*">
    <table>
      <thead>
        <tr>
          <xsl:apply-templates select="*[1]/*" mode="th"/>
        </tr>
      </thead>
      <tbody>
        <xsl:apply-templates select="*"/>
      </tbody>
    </table>
  </xsl:template>

  <xsl:template match="/*/*/*" mode="th">
    <th>
      <xsl:value-of select="name()"/>
    </th>
  </xsl:template>

  <xsl:template match="/*/*">
    <tr>
      <xsl:apply-templates select="*"/>
    </tr>
  </xsl:template>

  <xsl:template match="/*/*/*">
    <td>
      <xsl:value-of select="."/>
    </td>
  </xsl:template>

</xsl:stylesheet>
Reply With Quote
  #5 (permalink)  
Old March 10th, 2008, 01:45 PM
Registered User
 
Join Date: Mar 2008
Location: , , USA.
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks Martin !!!

It was a very quickly reply and provided a insight into processing the nodes in a different manner..

But I have more nodes in my XML repeating element, I want to pick only 2 nodes and use it for my table....HTML conversion should be fine, I can map it to XSL-FO....

<?xml version"1.0"?>
<Employees>
    <People>
        <Name> ABC </Name>
        <Title> Analyst</Title>
        <Salary> $12k </Salary>
        <Experience> 2 </Experience>
        <Project> DN123 </Project>
    </People>
    <People>
        <Name> GHI </Name>
        <Title> Manager </Title>
        <Salary> $15k </Salary>
        <Experience> 7 </Experience>
        <Project> DN123 </Project>
    </People>
</Employees>

or
<TopExecutives>
     <Executive>
         <Name> XYZ </Name>
         <Title> CEO </Title>
        <IsInBoard> Yes </IsInBoard>
        <BonusShares> 1200 </BonusShares>
     </Executive>
     <Executive>
         <Name> MNO </Name>
         <Title> CFO </Title>
        <IsInBoard> No </IsInBoard>
        <BonusShares> 500 </BonusShares>
     </Executive>
</TopExecutives>

Reply With Quote
  #6 (permalink)  
Old March 11th, 2008, 07:49 AM
Friend of Wrox
Points: 6,522, Level: 34
Points: 6,522, Level: 34 Points: 6,522, Level: 34 Points: 6,522, Level: 34
Activity: 50%
Activity: 50% Activity: 50% Activity: 50%
 
Join Date: Nov 2007
Location: Germany
Posts: 1,220
Thanks: 0
Thanked 237 Times in 236 Posts
Default

Well any algorithm creating the table needs to have some way to identify the elements you want to output. Assuming you want to output the first two elements here is a sample stylesheet:

Code:
<xsl:stylesheet
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  version="1.0">

  <xsl:output method="html" indent="yes"/>

  <xsl:template match="/*">
    <table>
      <thead>
        <tr>
          <xsl:apply-templates select="*[1]/*[position() &lt; 3]" mode="th"/>
        </tr>
      </thead>
      <tbody>
        <xsl:apply-templates select="*"/>
      </tbody>
    </table>
  </xsl:template>

  <xsl:template match="/*/*/*" mode="th">
    <th>
      <xsl:value-of select="name()"/>
    </th>
  </xsl:template>

  <xsl:template match="/*/*">
    <tr>
      <xsl:apply-templates select="*[position() &lt; 3]"/>
    </tr>
  </xsl:template>

  <xsl:template match="/*/*/*">
    <td>
      <xsl:value-of select="."/>
    </td>
  </xsl:template>

</xsl:stylesheet>


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
How to create Dynamic Text in xsl NewToXSL XSLT 3 June 8th, 2006 05:26 PM
How to create a document template and add db data kenn_rosie VB.NET 2002/2003 Basics 0 March 2nd, 2006 03:42 PM
Can't create html page from template thiwan Dreamweaver (all versions) 4 June 13th, 2005 11:53 AM
Create processing instruction in XSL hbcontract XML 6 May 7th, 2004 12:14 PM
how to create/use php template file/system junemo Beginning PHP 1 April 23rd, 2004 03:47 PM



All times are GMT -4. The time now is 01:53 AM.


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