以表格格式HTML使用XSL转换XML数据及其子元素

时间:2011-06-16 15:43:27

标签: html xml xslt

这是我在这个XML中的测试XML我有子元素:INSTRUMENT和子子元素:INSTRUMENT / issuer,可以这样......     5002199     10001      686184SE3

<INSTRUMENT>
    <type>FI</type>
    <issuer>
        <FICode>123456</FICode>
        <name>Test</name>
        <city>SF</city>
        <state>CA</state>
    </issuer>

    <issueDate>2011-06-22-05:00</issueDate>
    <maturityDate>2016-06-22-05:00</maturityDate>
    <firstCouponDate>2011-07-22-05:00</firstCouponDate>
    <lastCouponDate>2016-05-22-05:00</lastCouponDate>
    <couponRate>2.0</couponRate>

    <paymentFrequency>12</paymentFrequency>

    <callSchedule>
        <notice>15</notice>
        <timing>0</timing>
        <call id="1">
            <startDate>2011-12-22-05:00</startDate>
            <type>2</type>
            <freq>M</freq>
        </call>
    </callSchedule>
</INSTRUMENT>
<Commision>7.0</Commision>
<price>100.0</price>

我想以HTML表格形式显示这些数据,并且运行时xml元素可以是任何东西,所以我不能硬编码元素或子元素名称 我正在尝试follwinf XSL

<xsl:template match="/">
    <xsl:apply-templates />
</xsl:template>

<xsl:template match="*">
    <table border="1" width="1000">
        <tr>
            <td class="section_head">Key</td>
            <td class="section_head">Value</td>
        </tr>
        <xsl:for-each select="*" >
            <tr>
                <td>
                    <xsl:value-of select="name(.)" />
                </td>
                <td>
                    <xsl:value-of select="." />
                </td>
            </tr>
        </xsl:for-each>
    </table>
</xsl:template>

HTM Child INSTRUMENT和issuer以及callSchedule以表格形式出现 有什么办法可以迭代XSL recursivley来为XML子元素创建HTML子表吗?

             键         值                   ID         5002199                   码         10001                   CUSIP          686184SE3                   仪器         FI 123456测试SF CA 2011-06-22-05:00 2016-06-22-05:00 2011-07-22-05:00 2016-05-22-05:00 2.0 12 15 0 2011-12-22 -05:00 100.0 2 M 2012-01-22-05:00 100.0 2012-02-22-05:00 100                   佣金         7                   价钱         100.0                   ALLOC         100

1 个答案:

答案 0 :(得分:2)

我不确定你在这之后是什么。看起来您希望将节点转换为名称/值对,并根据XML层次结构嵌套html表。这是一个递归,解决了在运行时不知道节点名称的问题。希望它能帮助您开始使用它。如果它不是你想要的,你可以用它来澄清你的问题:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="html" indent="yes"/>

  <xsl:template match="/">   
    <html>
      <table>
        <xsl:apply-templates/>
      </table>  
    </html> 
  </xsl:template>

  <xsl:template match="*[count(*) = 0]">   
    <tr>
      <td>
        <xsl:value-of select="name(.)" />
      </td>
      <td>
        <xsl:value-of select="." />
      </td>
    </tr>
  </xsl:template>

  <xsl:template match="*[count(*) > 0]">  
    <tr>
      <td>
        <xsl:value-of select="name(.)" />
      </td>
      <td>
        <table>
          <xsl:apply-templates/>             
        </table>
      </td>
    </tr>     
  </xsl:template>
</xsl:stylesheet>