【问题标题】:using XSL Transform XML data and their child elements in tabular format HTML使用 XSL 以表格格式 HTML 转换 XML 数据及其子元素
【发布时间】:2011-06-17 05:12:01
【问题描述】:

这是我在这个 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 元素可以是任何东西,所以我不能硬编码元素或子元素名称 我正在尝试跟随 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 尖顶 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.0 价格 100.0 分配 100

【问题讨论】:

  • 您要为孩子们​​准备一张单独的桌子吗?

标签: html xml xslt


【解决方案1】:

我不完全确定你在这里追求什么。看起来您希望将节点转换为名称/值对并根据 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>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-01
    • 2016-12-07
    相关资源
    最近更新 更多