xsl-list
[Top] [All Lists]

Re: Building tables with optional data elements

2002-10-03 03:51:04
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



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