将简单XML输出为HTML表的XSLT吗?

问题描述 投票:0回答:1

xml文件的片段:

  <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文件(我在上面仅引用了一个片段):

<?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个回答
0
投票

使用下面的代码

    <?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处的变换

© www.soinside.com 2019 - 2024. All rights reserved.