xsl-list
[Top] [All Lists]

Re: How to generate XSLT for a Dynamic XML file?

2004-09-24 14:11:09
Paria Heidari wrote:

I have dynamic xml file that gets generated at the runtime. I need to
create xslt file that will transform this file to a tabular format with
the nodes as the table header and the values as the table data. I know
the Root node, and the program passes in the main node name. 


Hi Paria,

I have some difficulty understanding your scenario exactly... particularly,
why you need the first node name passed as a parameter?

Sample xml file --------------
<CUSTOMERS>
<CUSTOMER>
...
</CUSTOMER>
</CUSTOMERS>

The only value I know from the above is root name 'CUSTOMERS', but the
first node, 'CUSTOMER', gets passed into me as a param value.


If your files have a fixed structure (that is, the root element contains a 
number
of equally named children, each having a subtree to be represented in a table 
row), you don't have to hardcode any element name, nor obtain it from 
a parameter, because you can get the name directly from the input file:

    <xsl:value-of select="name(/*/*[1])"/> would give 'CUSTOMER'

So instead of a template match="CUSTOMER" you could as well do:

    <xsl:template match="/*">
        <xsl:apply-templates select="*" mode="table-row"/>
    </xsl:template>

    <xsl:template match="*" mode="table-row">
        ... this is a CUSTOMER element ...
    </xsl:template>

The first template matches the root element (in this case, CUSTOMERS) and
applies templates to its children (here, CUSTOMER). You can use modes to
ensure that the appropriate template is called.

Here's a stylesheet, please try it on your input files, I hope it does more or
less what you wanted, without any params:

<?xml version="1.0" encoding="utf-8"?>

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

 <xsl:output method="xml" version="1.0" encoding="utf-8" indent="yes"
  doctype-public="-//W3C//DTD XHTML 1.0 Strict//EN"
  doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"/>

 <xsl:template match="/*">
  <html>
   <head>
    <title><xsl:value-of select="name()"/></title>
    <style type="text/css">
     table { font-family: Arial, sans-serif; }
     th { font-size: 75%; background: #4D5D97; color: #FFFFFF; }
     td { vertical-align: top; }
     .odd { background: #FFFFFF; }
     .evn { background: #E7ECF0; }
    </style>
   </head>
   <body>
    <h1><xsl:value-of select="name()"/></h1>
    <xsl:apply-templates select="." mode="table"/>
   </body>
  </html>
 </xsl:template>

 <xsl:template match="*" mode="table">
  <table border="0">
   <tr>
    <xsl:apply-templates select="*[1]/descendant::*[not(*)]" mode="th"/>
   </tr>
   <xsl:apply-templates select="*" mode="tr"/>
  </table>
 </xsl:template>

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

 <xsl:template match="*" mode="tr">
  <xsl:variable name="class">
   <xsl:choose>
    <xsl:when test="(position() mod 2 = 1)">odd</xsl:when>
    <xsl:otherwise>evn</xsl:otherwise>
   </xsl:choose>
  </xsl:variable>
  <tr class="{$class}">
   <xsl:apply-templates select="descendant::*[not(*)]" mode="td"/>
  </tr>
 </xsl:template>

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

</xsl:stylesheet>

Cheers,
Anton


<Prev in Thread] Current Thread [Next in Thread>