Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
| 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 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
  #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>


  #2 (permalink)  
Old March 10th, 2008, 01:07 PM
Friend of Wrox
Points: 6,676, Level: 34
Points: 6,676, Level: 34 Points: 6,676, Level: 34 Points: 6,676, Level: 34
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Nov 2007
Location: Germany
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 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?

  #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

  #4 (permalink)  
Old March 10th, 2008, 01:23 PM
Friend of Wrox
Points: 6,676, Level: 34
Points: 6,676, Level: 34 Points: 6,676, Level: 34 Points: 6,676, Level: 34
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Nov 2007
Location: Germany
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 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>
  #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>

  #6 (permalink)  
Old March 11th, 2008, 07:49 AM
Friend of Wrox
Points: 6,676, Level: 34
Points: 6,676, Level: 34 Points: 6,676, Level: 34 Points: 6,676, Level: 34
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Nov 2007
Location: Germany
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 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>




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





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