【发布时间】:2020-01-20 09:13:16
【问题描述】:
我有一个包含以下 XML 数据的 XML 文件。
<group1>
<item1>val1</item1>
<item2>val2</item2>
<group2>
<item3>val3</item3>
<item4>val4</item4>
<group3>
<item5>val5</item5>
</group3>
</group2>
<group2>
<item3>val6</item3>
<item4>val7</item4>
<group3>
<item5>val8</item5>
</group3>
</group2>
<group4>
<item6>val9</item6>
<item7>val10</item7>
</group4>
<group4>
<item6>val11</item6>
<item7>val12</item7>
</group4>
像 HTML 表格一样
<table --for group1>
<tr>
<th>item1</th>
<th>item2</th>
</tr>
<tr>
<td>val1</td>
<td>val2</td>
</tr>
</table>
<table --for group2>
<tr>
<th>item3</th>
<th>item4</th>
</tr>
<tr>
<td>val3</td>
<td>val4</td>
</tr>
</table>
<table --for group3>
<tr>
<th>item5</th>
</tr>
<tr>
<td>val5</td>
</tr>
</table>
<table --for group2>
<tr>
<th>item3</th>
<th>item4</th>
</tr>
<tr>
<td>val6</td>
<td>val7</td>
</tr>
</table>
<table --for group3>
<tr>
<th>item5</th>
</tr>
<tr>
<td>val8</td>
</tr>
</table>
<table --for group4>
<tr>
<th>item6</th>
<th>item7</th>
</tr>
<tr>
<td>val9</td>
<td>val10</td>
</tr>
<tr>
<td>val11</td>
<td>val12</td>
</tr>
</table>
在 group1 中有两个 group2,其中有 group3。因此,每个 group2 应该是一个单独的表,后面有 group3 表。有两个 group4,但其中没有另一个组。所以两个 group4 应该在一个表中。
注意:每个组可以嵌套任意数量的组。
我想要实现的是在 Oracle bi 发布者数据模型表视图中显示查询结果。
能够通过为不同深度使用不同的 XSL 模板来实现固定深度,但不知道在深度不固定时如何实现。
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<HTML>
<BODY>
<xsl:apply-templates />
</BODY>
</HTML>
</xsl:template>
<xsl:template match="/*">
<TABLE BORDER="1">
<TR>
<xsl:for-each select="*/*">
<xsl:choose>
<xsl:when test="child::*">
<!-- <h1>
<xsl:value-of select="local-name()" />
</h1> -->
</xsl:when>
<xsl:otherwise>
<!-- <h1>
<xsl:value-of select="local-name()" />
</h1> -->
<xsl:for-each select=".">
<TD>
<xsl:value-of select="local-name()" />
</TD>
</xsl:for-each>
</xsl:otherwise>
</xsl:choose>
</xsl:for-each>
<xsl:apply-templates />
</TR>
</TABLE>
</xsl:template>
<xsl:template match="/*/*">
<TR>
<xsl:for-each select="*">
<xsl:choose>
<xsl:when test="child::*">
<h1>
<xsl:value-of select="local-name()" />
</h1>
</xsl:when>
<xsl:otherwise>
<td>
<xsl:apply-templates select="." />
</td>
</xsl:otherwise>
</xsl:choose>
</xsl:for-each>
</TR>
</xsl:template>
<!-- <xsl:template match="/*/*/*">
<xsl:choose>
<xsl:when test="child::*">
<TABLE BORDER="1">
<TR>
<xsl:for-each select="*/*">
<TD>
<xsl:value-of select="local-name()" />
</TD>
</xsl:for-each>
</TR>
</TABLE>
<xsl:value-of select="." />
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="." />
</xsl:otherwise>
</xsl:choose>
</xsl:template> -->
【问题讨论】:
-
每个嵌套级别真的有不同的名称吗(例如 group1、group2、...、item1、item2、item3、...)?还是元素名称相同,您可以编写单个递归模板?
-
是的,他们有不同的名字
标签: javascript python xml xslt