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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

XSL expert needed

Status
Not open for further replies.

jebsilver

Programmer
Jul 21, 2005
3
US
I am in desperate need of assistance. I need the XSL to take the XML below and create the HTML (also below). Thank you very much in advance.

XML:
<?xml version="1.0"?>
<root>
<field name="field1">
<string>field1.row1</string>
<string>field1.row2</string>
<string>field1.row3</string>
<string>field1.row4</string>
</field>
<field name="field2">
<string>field2.row1</string>
<string>field2.row2</string>
<string>field2.row3</string>
<string>field2.row4</string>
</field>
<field name="field3">
<string>field3.row1</string>
<string>field3.row2</string>
<string>field3.row3</string>
<string>field3.row4</string>
</field>
</root>

I need this HTML:
<table>
<tr>
<td>field1</td>
<td>field2</td>
<td>field3</td>
</tr>
<tr>
<td>field1.row1</td>
<td>field2.row1</td>
<td>field3.row1</td>
<td>field4.row1</td>
</tr>
<tr>
<td>field1.row2</td>
<td>field2.row2</td>
<td>field3.row2</td>
<td>field4.row2</td>
</tr>
<tr>
<td>field1.row3</td>
<td>field2.row3</td>
<td>field3.row3</td>
<td>field4.row3</td>
</tr>
<tr>
<td>field1.row4</td>
<td>field2.row4</td>
<td>field3.row4</td>
<td>field4.row4</td>
</tr>
</td>
 
This will only work properly if field 1 contains as many rows as every other field:
Code:
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="[URL unfurl="true"]http://www.w3.org/1999/XSL/Transform">[/URL]
  <xsl:template match="/">
    <xsl:apply-templates/>
  </xsl:template>
  <xsl:template match="root">
    <table>
      <th>
        <xsl:apply-templates select="field/@name"/>
      </th>
      <xsl:for-each select="field[1]/string">
        <xsl:variable name="pos" select="position()"/>
        <tr>
          <xsl:apply-templates select="../../field/string[position() = $pos]"/>
        </tr>
      </xsl:for-each>
    </table>
  </xsl:template>
  <xsl:template match="@name">
    <td>
      <xsl:value-of select="."/>
    </td>
  </xsl:template>
  <xsl:template match="string">
    <td>
      <xsl:value-of select="."/>
    </td>
  </xsl:template>
</xsl:stylesheet>

Jon

"Asteroids do not concern me, Admiral. I want that ship, not excuses.
 
Thank you very much. This is perfect.

I just posted another XSL question, hope you can help with that too.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top