tags:

views:

17

answers:

2

Hi,

I have an xml document which looks like

<book>xxxxxxxxxxx</book>
<record>
  <field>
    <column>Title</column>
    <value>HF80</value>
  </field>
  <field>
    <column>page</column>
    <value>97</value>
  </field>

..... ...

I need to print the "column" heading as a table row. I can get all the records in a table but dont know how to go about this.

The output I need would look like :- tr td Title /td td Page /td /tr

Pointers welcome.. O

A: 

Something like this?

<xsl:template match="/">
  <table width="100%" border="0" cellpadding="0" cellspacing="0">
    <tr>
      <xsl:apply-templates select="root/record/field" />  
    </tr>
  </table>
</xsl:template>

<xsl:template match="/root/record/field">
  <td>
    <xsl:value-of select="./value" />
  </td>
</xsl:template>
A: 

This transformation:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&gt;
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>

 <xsl:template match="record">
  <table border="1">
   <tr><xsl:apply-templates select="field/column"/></tr>
   <tr><xsl:apply-templates select="field/value"/></tr>
  </table>
 </xsl:template>

 <xsl:template match="column|value">
  <td><xsl:apply-templates/></td>
 </xsl:template>

 <xsl:template match="book/text()"/>
</xsl:stylesheet>

when applied on this XML document (your provided XML made well-formed):

<books>
    <book>xxxxxxxxxxx</book>
    <record>
        <field>
            <column>Title</column>
            <value>HF80</value>
        </field>
        <field>
            <column>page</column>
            <value>97</value>
        </field>
    </record>
</books>

produces the wanted result:

<table border="1">
   <tr>
      <td>Title</td>
      <td>page</td>
   </tr>
   <tr>
      <td>HF80</td>
      <td>97</td>
   </tr>
</table>
Dimitre Novatchev