如何将XML显示为HTML表
我有一个XML文件,其中包含一些程序的配置信息。 该文件看起来像:如何将XML显示为HTML表,xml,xslt,Xml,Xslt,我有一个XML文件,其中包含一些程序的配置信息。 该文件看起来像: <master> <childCat1> <param1>test1</param1> <param2>test2</param2> </childCat1> <childCat2> <item1>test3</item1> <item2>tes
<master>
<childCat1>
<param1>test1</param1>
<param2>test2</param2>
</childCat1>
<childCat2>
<item1>test3</item1>
<item2>test4</item2>
</childCat2>
</master>*
等
这将是一个很好的类别之间的分隔线出现,但我可以生活没有他们
提前感谢您的帮助。如果所有这些都失败了,我将编写一些php代码来实现这一点,但是XSL样式表将更加通用
澄清:
我更喜欢XSL,因为它事先只知道一件事——XML有两个级别的元素、类别及其直接子元素。因此,无论我可能添加(或删除)什么—附加类别、子类或其他任何内容,xsl都可以在不进行手动更新的情况下工作。我认为这就是您想要的
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/master">
<html><body>
<table border="1">
<tr bgcolor="yellow">
<th style="text-align:left">Category</th>
<th style="text-align:left">Parameter</th>
<th style="text-align:left">Value</th>
</tr>
<xsl:apply-templates select="./*" mode='category' />
</table>
</body></html>
</xsl:template>
<xsl:template match='*' mode='category'>
<tr><td colspan='3'></td></tr>
<xsl:apply-templates select="./*" mode='parameter' />
</xsl:template>
<xsl:template match='*' mode='parameter'>
<tr>
<td><xsl:value-of select="name(..)"/></td>
<td><xsl:value-of select="name()"/></td>
<td><xsl:value-of select="."/></td>
</tr>
</xsl:template>
</xsl:stylesheet>
类别
参数
价值
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/master">
<html><body>
<table border="1">
<tr bgcolor="yellow">
<th style="text-align:left">Category</th>
<th style="text-align:left">Parameter</th>
<th style="text-align:left">Value</th>
</tr>
<xsl:apply-templates select="./*" mode='category' />
</table>
</body></html>
</xsl:template>
<xsl:template match='*' mode='category'>
<tr><td colspan='3'></td></tr>
<xsl:apply-templates select="./*" mode='parameter' />
</xsl:template>
<xsl:template match='*' mode='parameter'>
<tr>
<td><xsl:value-of select="name(..)"/></td>
<td><xsl:value-of select="name()"/></td>
<td><xsl:value-of select="."/></td>
</tr>
</xsl:template>
</xsl:stylesheet>