我在将html表转换为2种类型的xml时遇到了两个选项。 我需要制作一个xslt来像这样转换Html表
<html>
<categories place="row">1</categories>
<dataset place="head">Characteristics</dataset>
<table caption="City statistics">
<thead>
<tr>
<th id="1">Name</th>
<th id="2">Population</th>
<th id="3">Area</th>
<th id="4">Elevation</th>
</tr>
</thead>
<tbody>
<tr>
<td id="1">Moscow</td>
<td id="2">4.7</td>
<td id="3">11.54</td>
<td id="4">13</td>
</tr>
<tr>
<td id="1">London</td>
<td id="2">6</td>
<td id="3">15.54</td>
<td id="4">15</td>
</tr>
</tbody>
</table>
</html>
像这样的 我试图做的是: 我得到了第一个条件的答案。它位于Convert html table into xml using xslt <?xml version="1.0" encoding="UTF-8"?>
<chart caption="City statistics"
xAxisName="Сharacteristics"
yAxisName="Values">
<categories>
<category label="Moscow"/>
<category label="London"/>
</categories>
<dataset seriesName="Population">
<set value="4.7"/>
<set value="6"/>
</dataset>
<dataset seriesName="Area">
<set value="11.54"/>
<set value="15.54"/>
</dataset>
<dataset seriesName="Elevation">
<set value="13"/>
<set value="15"/>
</dataset>
</chart>
<?xml version='1.0' encoding='utf-8' ?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="xml"/>
<!-- Default template -->
<xsl:template match="/">
<chart caption="" xAxisName="City name" yAxisName="Values">
<xsl:attribute name="caption">
<xsl:value-of select="html/table/@caption"/>
</xsl:attribute>
<xsl:apply-templates select="html/table/thead"/>
<xsl:apply-templates select="html/table/tbody"/>
</chart>
</xsl:template>
<!-- template for the thead = categories container -->
<xsl:template match="thead">
<dataset>
<xsl:apply-templates select="tr/th[@id > 1]"/>
</dataset>
</xsl:template>
<!-- template for the th = each category -->
<xsl:template match="th">
<dataset>
<xsl:attribute name="seriesName">
<xsl:value-of select="."/>
</xsl:attribute>
<!--<xsl:apply-templates select="../../../tbody/tr/td[@id = ../../../thead/tr/th/@id]"/>\ -->
<xsl:for-each select="../../../tbody/tr/td[@id=../../../thead/tr/th/@id]">
<set value="">
<xsl:attribute name="value">
<xsl:value-of select="."/>
</xsl:attribute>
</set>
</xsl:for-each>
</dataset>
</xsl:template>
<!-- template for the thead = categories container -->
<xsl:template match="tbody">
<categories>
<xsl:apply-templates select="tr/td[@id=1]"/>
</categories>
</xsl:template>
<!-- template for the th = each category -->
<xsl:template match="td">
<category>
<xsl:attribute name="label">
<xsl:value-of select="."/>
</xsl:attribute>
</category>
</xsl:template>
<!-- <xsl:template match="td">
<set value="">
<xsl:attribute name="value">
<xsl:value-of select="."/>
</xsl:attribute>
</set>
</xsl:template> -->
</xsl:stylesheet>
答案 0 :(得分:1)
尝试以下样式表
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" omit-xml-declaration="yes" indent="yes"/>
<xsl:template match="/">
<chart caption="{html/table/@caption}" xAxisName="City name" yAxisName="Values">
<categories>
<xsl:for-each select="descendant::td[@id='1']">
<category label="{.}"/>
</xsl:for-each>
</categories>
<xsl:for-each select="descendant::th[@id > 1]">
<xsl:variable name="curr_id" select="@id"/>
<dataset seriesName="{.}">
<xsl:for-each select="../../../descendant::td">
<xsl:if test="@id=$curr_id">
<set value="{.}"/>
</xsl:if>
</xsl:for-each>
</dataset>
</xsl:for-each>
</chart>
</xsl:template>
</xsl:stylesheet>