【问题标题】:An XSLT to output simple XML as an HTML table?将简单 XML 输出为 HTML 表的 XSLT?
【发布时间】:2020-10-06 18:11:42
【问题描述】:

来自xml 文件的sn-p:

  <record>
    <entry>2020-06-14</entry>
    <entry>Fraser</entry>
    <entry>F</entry>
    <entry>40-49</entry>
    <entry>Lab-diagnosed</entry>
  </record>
  <record>
    <entry>2020-06-14</entry>
    <entry>Fraser</entry>
    <entry>F</entry>
    <entry>20-29</entry>
    <entry>Lab-diagnosed</entry>
  </record>
  <record>
    <entry>2020-06-14</entry>
    <entry>Vancouver Coastal</entry>
    <entry>M</entry>
    <entry>30-39</entry>
    <entry>Lab-diagnosed</entry>
  </record>

一个xsd 文件,它使用xmllint 验证完整的xml 文件(我在上面只引用了一个sn-p):

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">
  <xs:element name="csv">
    <xs:complexType>
      <xs:sequence>
        <xs:element maxOccurs="unbounded" ref="record"/>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
  <xs:element name="record">
    <xs:complexType>
      <xs:sequence>
        <xs:element maxOccurs="unbounded" ref="entry"/>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
  <xs:element name="entry" type="xs:string"/>
</xs:schema>

输出:

 <table style="width:100%">
  <tr>
    <th>Reported Date</th>
    <th>HA</th>
    <th>Sex</th>
    <th>Age_Group</th>
    <th>Classification_Reported</th>
  </tr>
  <tr>
    <td>2020-06-14</td>    
    <td>Fraser</td>
    <td>F</td>
    <td>40-49</td>
    <td>Lab Diagnosed</td>
  </tr>
  <tr>
    <td>2020-06-14</td>    
    <td>Fraser</td>
    <td>F</td>
    <td>20-29</td>
    <td>Lab Diagnosed</td>
  </tr>
  <tr>
    <td>2020-06-14</td>    
    <td>Vancouver Coastal</td>
    <td>M</td>
    <td>30-39</td>
    <td>Lab Diagnosed</td>
  </tr>
</table> 

xslt 会产生什么html 上述结果?

【问题讨论】:

    标签: html xml xslt xsd xml-parsing


    【解决方案1】:

    只有一些作品:

    <?xml version="1.0" encoding="UTF-8"?>
    <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
      <xsl:template match="/">
        <html>
          <body>
            <h2>Catalog</h2>
            <table border="1">
              <xsl:for-each select="/csv/record">
                <tr>
                  <td>
                    <xsl:value-of select="entry"/>
                  </td>
                </tr>
              </xsl:for-each>
            </table>
          </body>
        </html>
      </xsl:template>
    </xsl:stylesheet>
    

    当然,有五个“入口”元素,所以...不知道如何处理...

    【讨论】:

    • 在元素到元素映射的情况下,您不需要任何 for-each 迭代
    【解决方案2】:

    使用下面的代码

        <?xml version="1.0" encoding="UTF-8"?>
    <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
        xmlns:xs="http://www.w3.org/2001/XMLSchema"
        exclude-result-prefixes="xs"
        version="2.0">
    
        <xsl:output indent="yes"/>
    
        <xsl:template match="csv">
            <table style="width:100%">
                <tr>
                    <th>Reported Date</th>
                    <th>HA</th>
                    <th>Sex</th>
                    <th>Age_Group</th>
                    <th>Classification_Reported</th>
                </tr>
                <xsl:apply-templates/>
            </table>
        </xsl:template>
    
        <xsl:template match="record">
            <tr>
                <xsl:apply-templates/>
            </tr>
        </xsl:template>
    
        <xsl:template match="entry">
            <td>
                <xsl:apply-templates/>
            </td>
        </xsl:template>
    </xsl:stylesheet>
    

    请参阅 https://xsltfiddle.liberty-development.net/naZYrpA 的转换

    【讨论】:

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