我是 xslt 的新手并且面临一个问题,因为我动态生成了一个 xml 文件(即节点名称未知)并尝试了许多方法来使用 xslt 查看 html 文件中嵌套表中的内容,但我所有的努力都失败了。
<?xml version="1.0" encoding="UTF-8"?>
<content>
<Source>
<Column1>Name</Column1>
<Column2>Organization Name</Column2>
<SubSource>
<Column1>Name</Column1>
<Column2>Person Name</Column2>
<Column1>Address</Column1>
<Column2>House: E5, Block-G, Road-02</Column2>
<Details>
<Column1>Entry date</Column1>
<Column2>6/6/13 12:04 PM</Column2>
<Column1>height</Column1>
<Column2>153</Column2>
<Column1>weight</Column1>
<Column2>53.5</Column2>
<Column1>temperature</Column1>
<Column2>98.67</Column2>
<Column1>pulse rate</Column1>
<Column2>76</Column2>
<Advices>
<Advice>
<Column1>Title</Column1>
<Column2>Workout</Column2>
<Column1>Location</Column1>
<Column2>In the Park</Column2>
</Advice>
<Advice>
<Column1>Title</Column1>
<Column2>Eating Habit</Column2>
<Column1>Remarks</Column1>
<Column2>Eat Less</Column2>
<Column1>Notes</Column1>
<Column2>Avoid salts</Column2>
</Advice>
</Advices>
</Details>
</SubSource>
</Source>
<Source>
<Column1>Name</Column1>
<Column2>Organization Name</Column2>
<SubSource>
<Column1>Name</Column1>
<Column2>Person Name</Column2>
<Column1>Address</Column1>
<Column2>House: E5, Block-G, Road-02</Column2>
<Details>
<Column1>Entry date</Column1>
<Column2>6/6/13 12:04 PM</Column2>
<Column1>height</Column1>
<Column2>153</Column2>
<Column1>weight</Column1>
<Column2>53.5</Column2>
<Column1>temperature</Column1>
<Column2>98.67</Column2>
<Column1>pulse rate</Column1>
<Column2>76</Column2>
<Advices>
<Advice>
<Column1>Title</Column1>
<Column2>Workout</Column2>
<Column1>Location</Column1>
<Column2>In the Park</Column2>
</Advice>
</Advices>
</Details>
</SubSource>
</Source>
</content>
我使用了一些 xslt 方法。然而,他们所有人都没有按需要查看数据,要么更少,要么格式不正确!!!
以下是我最好的方法(我没有进行多表设计,因为我想先获取所有数据)
<?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"/>
<xsl:template match="/">
<html>
<body>
<table>
<xsl:apply-templates select="content/Source"/>
</table>
</body>
</html>
</xsl:template>
<xsl:template match="content/Source">
<xsl:variable name="column" select="content/Source"/>
<tr>
<td>
<xsl:value-of select="Column1"/>
</td>
<td>
<xsl:value-of select="Column2"/>
</td>
</tr>
<xsl:apply-templates select="SubSource"/>
</xsl:template>
<xsl:template match="SubSource">
<tr>
<td>
<xsl:value-of select="Column1"/>
</td>
<td>
<xsl:value-of select="Column2"/>
</td>
</tr>
<xsl:apply-templates select="Details"/>
</xsl:template>
<xsl:template match="Details">
<tr>
<td>
<xsl:value-of select="Column1"/>
</td>
<td>
<xsl:value-of select="Column2"/>
</td>
</tr>
<xsl:apply-templates select="Advices"/>
</xsl:template>
<xsl:template match="Advices">
<xsl:for-each select="Advice">
<tr>
<td>
<xsl:value-of select="Column1"/>
</td>
<td>
<xsl:value-of select="Column2"/>
</td>
</tr>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
输出是:
<table>
<tr>
<td>Name</td>
<td>Organization Name</td>
</tr>
<tr>
<td>Name</td>
<td>Person Name</td>
</tr>
<tr>
<td>Entry date</td>
<td>6/6/13 12:04 PM</td>
</tr>
<tr>
<td>Title</td>
<td>Workout</td>
</tr>
<tr>
<td>Title</td>
<td>Eating Habit</td>
</tr>
<tr>
<td>Name</td>
<td>Organization Name</td>
</tr>
<tr>
<td>Name</td>
<td>Person Name</td>
</tr>
<tr>
<td>Entry date</td>
<td>6/6/13 12:04 PM</td>
</tr>
<tr>
<td>Title</td>
<td>Workout</td>
</tr>
</table>
我的目标是获得与 XML 结构相同的表格布局。
始终欢迎有关更好的 XML 格式的建议!
谢谢