将简单 XML 输出为 HTML table 的 XSLT?
An XSLT to output simple XML as an HTML table?
来自 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
上面的结果?
使用下面的代码
<?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>
处的转换
仅一类作品:
<?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>
当然有五个 "entry" 元素,所以...不确定如何处理...
来自 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
上面的结果?
使用下面的代码
<?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>
处的转换
仅一类作品:
<?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>
当然有五个 "entry" 元素,所以...不确定如何处理...