[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

Re: Building tables with optional data elements

Subject: Re: Building tables with optional data elements
From: Joerg Heinicke <joerg.heinicke@xxxxxx>
Date: Thu, 03 Oct 2002 12:51:04 +0200
xsl optional values
Hello Grant,

your XML is not very comfortable. You should remove 'Column' in the Name of every Item/NameValue, because otherwise you have to do a more complex comparison to get the maximum number of columns.

<xsl:variable name="maxNoOfColumns" select="Item/NameValue[last()]
[not(Name &lt; ../following-sibling::Item/NameValue[last()]/Name)]/Name"/>

Here the maximum number of columns are calculated. Every last NameValue/Name of an Item (your NameValue or sorted in an Item) is compared to all following last NameValue/Name and this is one is used, which has no following NameValue/Name with a higher column number. If you don't want to remove 'Column' you have to do it dynamically, what will further slow down the comparison:

<xsl:variable name="maxNoOfColumns" select="Item/NameValue[last()]
[not(translate(Name, 'Column', '') &lt; translate(../following-sibling::Item/NameValue[last()]/Name, 'Column', ''))]/Name"/>


So remove 'Column' from the XML. If it shell be written in the header, it's a styling question and should be handled in the XSL as following.

Complete XSL:

<xsl:template match="Data">
  <xsl:variable name="maxNoOfColumns" select="Item/NameValue[last()]
[not(Name &lt; ../following-sibling::Item/NameValue[last()]/Name)]/Name"/>
  <table border="1">
    <tr>
      <xsl:call-template name="header">
        <xsl:with-param name="maxNoOfColumns" select="$maxNoOfColumns"/>
      </xsl:call-template>
    </tr>
    <xsl:apply-templates select="Item">
      <xsl:with-param name="maxNoOfColumns" select="$maxNoOfColumns"/>
    </xsl:apply-templates>
  </table>
</xsl:template>

<xsl:template match="Item">
  <xsl:param name="maxNoOfColumns" select="0"/>
  <tr>
    <xsl:call-template name="row">
      <xsl:with-param name="maxNoOfColumns" select="$maxNoOfColumns"/>
    </xsl:call-template>
  </tr>
</xsl:template>

<xsl:template name="header">
  <xsl:param name="maxNoOfColumns" select="0"/>
  <xsl:param name="currentNo" select="1"/>
  <xsl:if test="$currentNo &lt; $maxNoOfColumns + 1">
    <td>Column<xsl:value-of select="$currentNo"/></td>
    <xsl:call-template name="header">
      <xsl:with-param name="maxNoOfColumns" select="$maxNoOfColumns"/>
      <xsl:with-param name="currentNo" select="$currentNo + 1"/>
    </xsl:call-template>
  </xsl:if>
</xsl:template>

<xsl:template name="row">
<xsl:param name="maxNoOfColumns" select="0"/>
<xsl:param name="currentNo" select="1"/>
<xsl:if test="$currentNo &lt; $maxNoOfColumns + 1">
<td>
<xsl:value-of select="NameValue[Name = $currentNo]/Value"/>
<xsl:if test="not(normalize-space(NameValue[Name = $currentNo]/Value))">&#160;</xsl:if>
</td>
<xsl:call-template name="row">
<xsl:with-param name="maxNoOfColumns" select="$maxNoOfColumns"/>
<xsl:with-param name="currentNo" select="$currentNo + 1"/>
</xsl:call-template>
</xsl:if>
</xsl:template>


Regards,

Joerg

Grant Thiselton wrote:
Good Day,
I am attempting to convert the following XML into a table. Each column is
optional and the possibility is that one or more of the column data will be
missing in each row. I need to generate the table to include all the columns for each row, with
the data in the correct column. The actual structure includes up to 22
columns with some rows having only six data entries. I have managed to build a list of column names from each row, but I have not
managed to place the correct data in the correct column when a previous
column is missing. i.e. put a blank entry when the column is missing. How do I do this?


Thanks

Grant Thiselton

Example XML:
<?xml version="1.0"?>
<Content>
  <Data>
    <Item>
	<NameValue>
	  <Name>Column1</Name><Value>A</Value>
	</NameValue>
	<NameValue>
	  <Name>Column2</Name><Value>B</Value>
	</NameValue>
      <NameValue>
	  <Name>Column3</Name><Value>C</Value>
	</NameValue>
  </Item>
  <Item>
	<NameValue>
	  <Name>Column1</Name><Value>X</Value>
	</NameValue>
	<NameValue>
	  <Name>Column3</Name><Value>Y</Value>
	</NameValue>
  </Item>
  <Item>
	<NameValue>
        <Name>Column3</Name><Value>z</Value>
	</NameValue>
  </Item>
</Data>
</Content>


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


Current Thread

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.