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 help needed again...

Status
Not open for further replies.

jebsilver

Programmer
Jul 21, 2005
3
US
I need the XSL that will take this XML and create the HTML below. Hope you can help, I'm in a jam with this one. Thanks for any help you can give.

XML:
<?xml version="1.0"?>
<root>
<field name="auctionid">
<string>432423</string>
<string>423425</string>
<string>524344</string>
</field>
<field name="propertyname">
<string>La Cabana Resort</string>
<string>Fairfield Royal Crown</string>
<string>Bakersfield Marriott</string>
</field>
<field name="city">
<string>Dayton</string>
<string>Moorstown</string>
<string>Jessup</string>
</field>
</root>

HTML:
<table>
<tr>
<th>city</th>
<th>auctionid</th>
<th>propertyname</th>
</tr>
<tr>
<td>Dayton</td>
<td>432423</td>
<td>La Cabana Resort</td>
</tr>
<tr>
<td>Moorstown</td>
<td>423425</td>
<td>Fairfield Royal Crown</td>
</tr>
<td>Jessup</td>
<td>524344</td>
<td>Bakersfield Marriott</td>
</tr>
</table>
 
Probably the simplest way to do it:
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>
      <tr>
        <th>city</th>
        <th>auctionid</th>
        <th>propertyname</th>
      </tr>
      <xsl:for-each select="field[1]/string">
        <xsl:variable name="pos" select="position()"/>
        <tr>
          <xsl:apply-templates select="../../field[3]/string[position() = $pos]"/>
          <xsl:apply-templates select="../../field[1]/string[position() = $pos]"/>
          <xsl:apply-templates select="../../field[2]/string[position() = $pos]"/>
        </tr>
      </xsl:for-each>
    </table>
  </xsl:template>
  <xsl:template match="string">
    <td>
      <xsl:value-of select="."/>
    </td>
  </xsl:template>
</xsl:stylesheet>
The semantic structure of the original XML is pretty terrible. I would change that if possible.

Jon

"Asteroids do not concern me, Admiral. I want that ship, not excuses.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top