开发者

xslt returning first occurance of column

开发者 https://www.devze.com 2023-01-11 10:40 出处:网络
I have an xml document which looks like <book>xxxxxxxxxxx</book> <record> <field>

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 li开发者_如何学运维ke :- tr td Title /td td Page /td /tr

Pointers welcome.. O


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>


This transformation:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <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>
0

精彩评论

暂无评论...
验证码 换一张
取 消