Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

display XML in 2 columns (XSL)

Status
Not open for further replies.

IlseDC

Programmer
Mar 24, 2003
49
BE
Hi,

I have an XML file which I want to display in 2 columns on a HTML page (xsl conversion).

XML file:
<Test>
<Part>
<Code>P1</Code>
<Items>
<Item><Code>ID1</Code><Desc>Item 1</Desc></Item>
<Item><Code>ID2</Code><Desc>Item 2</Desc></Item>
<Item><Code>ID3</Code><Desc>Item 3</Desc></Item>
</Items>
</Part>
<Part>
<Code>P2</Code>
<Items>
<Item><Code>ID4</Code><Desc>Item 4</Desc></Item>
<Item><Code>ID5</Code><Desc>Item 5</Desc></Item>
</Items>
</Part>
</Test>

In my HTML I want to display a table with 2 columns.
In the left column Item 1, 2, 3 (Part 1) have to be displayed, in the right column Item 4, 5 (Part 2).
How can this be done by xsl?

Thanks.



 
First of all I did this:

Code:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="[URL unfurl="true"]http://www.w3.org/1999/XSL/Transform">[/URL]
  <xsl:template match="/">
    <html>
      <head>
        <title/>
      </head>
      <body>
        <table>
          <tbody>
            <xsl:for-each select="Test/Part[1]/Items/Item">
              <xsl:variable name="position" select="position()" />
                <tr>
                  <td>
                    <xsl:text>Code: </xsl:text>
                      <xsl:value-of select="Code"/>
                      <br/>
                      <xsl:text>Desc: </xsl:text>
                      <xsl:value-of select="Desc"/>
                  </td>
                  <td>
                      <xsl:text>Code: </xsl:text>
                      <xsl:value-of select="../../../Part[2]/Items/Item[position() = $position]/Code"/>
                      <br/>
                      <xsl:text>Desc: </xsl:text>
                      <xsl:value-of select="../../../Part[2]/Items/Item[position() = $position]/Desc"/>
                  </td>
                </tr>
              </xsl:for-each>
            </tbody>
          </table>
        </body>
      </html>
   </xsl:template>
</xsl:stylesheet>
But that only works if you have more items in part 1 than part 2, so I thought of a better way with recursion:
Code:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="[URL unfurl="true"]http://www.w3.org/1999/XSL/Transform">[/URL]
  <xsl:template match="/">
    <html>
      <head>
        <title/>
      </head>
      <body>
        <table style="border: thin solid black">
          <tbody>
            <xsl:call-template name="maketable">
              <xsl:with-param name="col1" select="Test/Part[1]/Items/Item"/>
              <xsl:with-param name="col2" select="Test/Part[2]/Items/Item"/>
            </xsl:call-template>
          </tbody>
        </table>
      </body>
    </html>
  </xsl:template>
  <xsl:template name="maketable">
    <xsl:param name="col1"/>
    <xsl:param name="col2"/>
    <tr>
      <td style="border: thin solid black">
        <xsl:value-of select="$col1/Code"/>
        <xsl:value-of select="$col1/Desc"/>
      </td>
      <td style="border: thin solid black">
        <xsl:value-of select="$col2/Code"/>
        <xsl:value-of select="$col2/Desc"/>
      </td>
      </tr>
      <xsl:if test="$col1[position() > 1] or $col2[position() > 1]">
        <xsl:call-template name="maketable">
          <xsl:with-param name="col1" select="$col1[position() > 1]"/>
          <xsl:with-param name="col2" select="$col2[position() > 1]"/>
        </xsl:call-template>
      </xsl:if>
    </xsl:template>
</xsl:stylesheet>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top