在XSLT中将XML转换为HTML表
我试图将XML转换为HTML表,但仍然对如何使用模板进行行-列映射感到困惑。我的XML定义是:在XSLT中将XML转换为HTML表,html,xml,xslt,Html,Xml,Xslt,我试图将XML转换为HTML表,但仍然对如何使用模板进行行-列映射感到困惑。我的XML定义是: <Table> <Parent> <Head>Header 1</Head> <Children> <Node>Node 1</Node> <Node>Node 2</Node> <Node>Node 3</Node>
<Table>
<Parent>
<Head>Header 1</Head>
<Children>
<Node>Node 1</Node>
<Node>Node 2</Node>
<Node>Node 3</Node>
</Children>
</Parent>
<Parent>
<Head>Header 2</Head>
<Children>
<Node>Node 4</Node>
<Node>Node 5</Node>
<Node>Node 6</Node>
</Children>
</Parent>
</Table>
标题1
节点1
节点2
节点3
标题2
节点4
节点5
节点6
预期的HTML输出:
<table>
<tr>
<td>Header 1</td>
<td>Header 2</td>
</tr>
<tr>
<td>Node 1</td>
<td>Node 4</td>
</tr>
<tr>
<td>Node 2</td>
<td>Node 5</td>
</tr>
<tr>
<td>Node 3</td>
<td>Node 6</td>
</tr>
</table>
标题1
标题2
节点1
节点4
节点2
节点5
节点3
节点6
我使用了模板匹配,但不知道如何按位置进行映射。这是我当前的XSLT代码:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="Table">
<table>
<tr>
<xsl:apply-templates select="Parent"/>
</tr>
<xsl:apply-templates select="Parent/Children"/>
</table>
</xsl:template>
<xsl:template match="Parent">
<td>
<xsl:value-of select="Head"/>
</td>
</xsl:template>
<xsl:template match="Parent/Children">
<tr>
<xsl:apply-templates/>
</tr>
</xsl:template>
<xsl:template match="Parent/Children/Node">
<td>
<xsl:value-of select="."/>
</td>
</xsl:template>
</xsl:stylesheet>
如果可以假设每个
父节点都有相同数量的节点,则可以只选择第一个父节点的节点,因为这些节点将代表每一新行的开始
<xsl:apply-templates select="Parent[1]/Children/Node" mode="row"/>
试试这个XSLT
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="Table">
<table>
<tr>
<xsl:apply-templates select="Parent"/>
</tr>
<xsl:apply-templates select="Parent[1]/Children/Node" mode="row"/>
</table>
</xsl:template>
<xsl:template match="Parent">
<td>
<xsl:value-of select="Head"/>
</td>
</xsl:template>
<xsl:template match="Node" mode="row">
<xsl:variable name="pos" select="position()" />
<tr>
<xsl:apply-templates select="../../../Parent/Children/Node[position() = $pos]" />
</tr>
</xsl:template>
<xsl:template match="Node">
<td>
<xsl:value-of select="."/>
</td>
</xsl:template>
</xsl:stylesheet>
如果可以假设每个父节点都有相同数量的节点,则可以只选择第一个父节点的节点,因为这些节点将代表每一新行的开始
<xsl:apply-templates select="Parent[1]/Children/Node" mode="row"/>
试试这个XSLT
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="Table">
<table>
<tr>
<xsl:apply-templates select="Parent"/>
</tr>
<xsl:apply-templates select="Parent[1]/Children/Node" mode="row"/>
</table>
</xsl:template>
<xsl:template match="Parent">
<td>
<xsl:value-of select="Head"/>
</td>
</xsl:template>
<xsl:template match="Node" mode="row">
<xsl:variable name="pos" select="position()" />
<tr>
<xsl:apply-templates select="../../../Parent/Children/Node[position() = $pos]" />
</tr>
</xsl:template>
<xsl:template match="Node">
<td>
<xsl:value-of select="."/>
</td>
</xsl:template>
</xsl:stylesheet>
谢谢@Tim C。如果行不相等(例如,第二个父项比第一个父项多),我是否应该使用Muenchian分组来处理它?您需要找到子项最多的父项,而不是只选择第一个父项。问一个全新的后续问题可能更容易。谢谢或者,也许这有帮助。。。谢谢@Tim C。如果行不相等(例如,第二个父项比第一个父项多),我是否应该使用Muenchian分组来处理它?您需要找到子项最多的父项,而不是只选择第一个父项。问一个全新的后续问题可能更容易。谢谢或者,也许这有帮助。。。