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]
Other format: [Raw text]

RE: Newbie question: dynamic output column ordering


Here's the solution I came up with for my problem - but, as you can see, I
parse out the fields every time through the do_data_row template.  Is there
a way that I can pre-parse the $fields variable into a tree fragment and use
that to make it quicker?  What changes would you make to get the most bang
for my buck?

Thanks.

	Mike Bandy
	Entegrity Solutions
	Columbia, MD

<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>
  <!--
    -->
  <xsl:output method="html" indent="yes"/>
  <!--
	Global variables - to be passed down from Java
    -->
  <!-- Delimiter for fields passed to templates -->
  <xsl:param name="field_delimiter" select="','"/>
  <!-- Fields to be written to HTML Table, in order left to right -->
  <xsl:param name="fields">source_name,date_completed,status</xsl:param>
  <!--
    -->
  <xsl:template match="/">
    <!-- Match the high level subsystems -->
    <xsl:apply-templates select="report"/>
  </xsl:template>
  <!--

     TEMPLATE report
  -->
  <xsl:template match="report">
    <html>
      <head>
        <title><xsl:value-of select="$fields"/></title>
      </head>
      <body>
        <xsl:text disable-output-escaping="yes">&lt;table border="2"
bgcolor="yellow"&gt;</xsl:text>
        <!--
		Write the Table Header
	-->
        <tr>
          <xsl:call-template name="do_header_row">
            <xsl:with-param name="fields" select="$fields"/>
          </xsl:call-template>
        </tr>
        <!--
		Process each record element
	-->
        <xsl:for-each select="record">
          <!-- Process the record element -->
          <tr>
            <xsl:call-template name="do_data_row">
              <xsl:with-param name="fields" select="$fields"/>
            </xsl:call-template>
          </tr>
        </xsl:for-each>
        <!-- </table>   Note: can't just put the endtag in because then it
makes
			this non-well formed XML -->
        <xsl:text disable-output-escaping="yes">&lt;/table&gt;</xsl:text>
      </body>
    </html>
  </xsl:template>
  <!--

     TEMPLATE do_header_row
	Recursive HTML table header row generater
   -->
  <xsl:template name="do_header_row">
    <xsl:param name="fields" select="''"/>
    <!--   -->
    <xsl:variable name="field" select="substring-before( $fields,
$field_delimiter )"/>
    <xsl:variable name="rest_of_fields" select="substring-after( $fields,
$field_delimiter )"/>
    <xsl:choose>
      <xsl:when test="contains($fields,$field_delimiter)">
        <th>
          <xsl:value-of select="$field"/>
        </th>
        <!-- Recurse -->
        <xsl:call-template name="do_header_row">
          <xsl:with-param name="fields" select="$rest_of_fields"/>
        </xsl:call-template>
      </xsl:when>
      <xsl:otherwise>
        <th>
          <xsl:value-of select="$fields"/>
        </th>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>
  <!--

     TEMPLATE do_data_row
	Recursive HTML table data row generater
   -->
  <xsl:template name="do_data_row">
    <xsl:param name="fields" select="''"/>
    <!--   -->
    <xsl:variable name="field" select="substring-before( $fields,
$field_delimiter )"/>
    <xsl:variable name="rest_of_fields" select="substring-after( $fields,
$field_delimiter )"/>
    <xsl:choose>
      <xsl:when test="contains($fields,$field_delimiter)">
        <td>
          <strong><xsl:value-of select="*[name()=$field]"/></strong>
        </td>
        <!-- Recurse -->
        <xsl:call-template name="do_data_row">
          <xsl:with-param name="fields" select="$rest_of_fields"/>
        </xsl:call-template>
      </xsl:when>
      <xsl:otherwise>
        <td>
          <strong><xsl:value-of select="*[name()=$fields]"/></strong>
        </td>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>
</xsl:stylesheet>




> -----Original Message-----
> From: Mike Bandy [mailto:michael.bandy@entegrity.com]
> Sent: Wednesday, January 23, 2002 9:17 AM
> To: XSL-List (E-mail)
> Subject: [xsl] Newbie question: dynamic output column ordering
>
>
> I'm new to XSL, doing HTML report generation from an XML
> file.  I'm using
> xalan with Java to drive my transformations and I'd like to be able to
> specify the column ordering from my Java program.  So, from
> the XML below,
> I'd like to pass in the order, left to right, of the columns
> in my HTML
> report - for example "job_id,sow_id,date_created" or
> "date_created,job_id,file_name" or whatever based on the
> user's entries into
> a Java GUI.  (There's more to it than that, but this is where
> I'm stuck.)
>
> How do I pass in the ordering and then break out the
> individual fields of
> interest, then use the individual fields to extract the elements of
> interest?  I can do it all hardcoded (like below), but how do
> I make it
> dynamic based on parameters from my Java program?
>
> This seems like a FAQ but I can't find an answer online -
> sorry if this is
> trivial.
>
> Thanks.
>
>     Mike Bandy
>     Entegrity Solutions
>     Columbia Maryland
>
> ---- XSL snippet below ----
>   <xsl:template match="record">
>     <!--
>           Output one row of the HTML table
>      -->
>     <tr>
>       <td><strong><xsl:value-of select="job_id"/></strong></td>
>       <td><xsl:value-of select="sow_id"/></td>
>       <td><xsl:value-of select="date_completed"/></td>
>     </tr>
>   </xsl:template>
>
> ---- XML below ----
> <?xml version='1.0' encoding='UTF-8' standalone='yes'?>
> <report>
>   <record>
>     <job_id>3Apserver_CORVETTE_01011209642894</job_id>
>     <sow_id>B0AEAA2B2C55F6A41701010691436119</sow_id>
>     <date_created>2002-01-16</date_created>
>     <date_completed></date_completed>
>     <status>Job Failed</status>
>     <file_name>CORVETTE/3Apserver_CORVETTE_0</file_name>
>     <file_size>10485760</file_size>
>     <source_name>Corvette</source_name>
>     <destination_name> </destination_name>
>   </record>
>   <record>
>     <job_id>102Apserver_CORVETTE_01011233583133</job_id>
>     <sow_id>B0AEAA2B2C55F6A41701010691436119</sow_id>
>     <date_created>2002-01-16</date_created>
>     <date_completed>2002-01-16</date_completed>
>     <status>Job Completed</status>
>     <file_name>CORVETTE/102Apserver_CORVETTE_0</file_name>
>     <file_size>10485760</file_size>
>     <source_name>Corvette</source_name>
>     <destination_name> </destination_name>
>   </record>
> </report>
>
>
>
>
>  XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
>


 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]