This is the mail archive of the xsl-list@mulberrytech.com mailing list .


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

RE: generate unknow table


Hi, Tom

I don't know how many thanks I can say to you, I really
appreciate your help, it solves my big problem. Only
thing I can do is post the final result in case someone
else wants to do the simillar stuff. I would like to let
you know, I have learned a lot from your example, before
studying your example, I never realized that so powerful
of using <xsl:variable>. Thanks again and again!

Here is the XML pre-generated from display control file:

<?xml version="1.0"?>
<title>
	<field id="sel" ND="flag80">Sel</field>
	<field id="sflbr">Br</field>
	<field id="sflcyc">Cycle</field>
	<field id="sfsts">P/I Status Description</field>
	<field id="pidate">P/I Date</field>
</title>

It carries table field title, name and order information.
It also tell us that wheter to display field "sel" is
determined by "flag80".

Here is the XML generated from database:

<?xml version="1.0"?>
<display>
	<data>
		<record>
			<sel flag80="on">12</sel>
			<sflbr>001</sflbr>
			<sflcyc>200</sflcyc>
			<sfsts>This is a description</sfsts>
			<pidate>06-02-01</pidate>
		</record>
		<record>
			<sel flag80="off">11</sel>
			<sflbr>002</sflbr>
			<sflcyc>210</sflcyc>
			<sfsts>This is a description too</sfsts>
			<pidate>06-11-01</pidate>
		</record>
	</data>
</display>

Here is the XSL that merge this two XML together and make
a HTML table. I really think it is very good sample code,
and can be used again and again.

<?xml version='1.0' encoding='utf-8' ?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>
<xsl:output method="html"/>

	<xsl:variable name="display" select="document(&apos;title.xml&apos;)"/>
	<!-- ////// Get list of field names ////////-->
	<xsl:variable name="fields" select="$display/title/field/@id"/>
	<xsl:variable name="titles" select="$display/title/field"/>
	<!--/////// Get name of flag ////////-->
	<xsl:variable name="display-flag"
select='$display/title/field[@id=&quot;sel&quot;]/@ND'/>
	<!--////// Main Template ////////-->
	<xsl:template match="/display">
		<html>
			<title>XSl Example for Unknown &quot;sel&quot; Flag Name</title>
			<table border="1">
				<xsl:call-template name="headers"/>
				<xsl:call-template name="data"/>
			</table>
		</html>
	</xsl:template>
	<!--/////// Row headers //////-->
	<xsl:template name="headers">
		<tr>
			<xsl:for-each select="$titles">
				<th>
					<xsl:value-of select="."/>
				</th>
			</xsl:for-each>
		</tr>
	</xsl:template>
	<!--/////// Table body //////-->
	<xsl:template name="data">
		<xsl:for-each select="data/record">
			<tr>
				<xsl:call-template name="get-record"/>
			</tr>
		</xsl:for-each>
	</xsl:template>
	<!--/////// Format each row ////////-->
	<xsl:template name="get-record">
		<xsl:variable name="row" select="."/>
		<xsl:for-each select="$fields">
			<xsl:variable name="field" select="../@id"/>
			<xsl:variable name="cell" select="$row/*[name()=$field]"/>
			<xsl:variable name="flag" select="$cell/@*[$display-flag]"/>
			<td>
				<xsl:if test='not($flag) or $flag=&quot;on&quot;'>
					<xsl:value-of select="$cell"/>
				</xsl:if>
			</td>
		</xsl:for-each>
	</xsl:template>
</xsl:stylesheet>

I have tested your code, it works great unless it shows the
field name instead of title provided in the <title> element.
I just follow your example, make little change and it works.
It looks like you assign me a homework. Here is the final result:

<html>
  <title>XSl Example for Unknown "sel" Flag Name</title>
  <table border="1">
    <tr>
      <th>Sel</th>
      <th>Br</th>
      <th>Cycle</th>
      <th>P/I Status Description</th>
      <th>P/I Date</th>
    </tr>
    <tr>
      <td>12</td>
      <td>001</td>
      <td>200</td>
      <td>This is a description</td>
      <td>06-02-01</td>
    </tr>
    <tr>
      <td></td>
      <td>002</td>
      <td>210</td>
      <td>This is a description too</td>
      <td>06-11-01</td>
    </tr>
  </table>
</html>

We can close individual field "sel" at run time by setting the
"flag80="off".

Thanks again, Tom.

-John



 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]