【发布时间】:2017-07-20 18:04:40
【问题描述】:
我想转换这个xml
XML:
<Page>
<Sections>
<Section id="parent_hdr_sec" rows="2" columns="1">
<Sections>
<Section id="plan_hdr" rows="0" columns="0" />
<Section id="plan_hdr_dtl" rows="0" columns="0" />
</Sections>
</Section>
<Section id="splitter_sec" rows="1" columns="2">
<Sections>
<Section id="parent_left_sec" rows="4" columns="1">
</Section>
<Section id="parent_right_sec" rows="7" columns="1">
</Section>
</Sections>
</Section>
</Sections>
</Page>
XSLT:
<xsl:template match="/">
<html>
<body>
<table>
<xsl:for-each select="Page/Sections/Section">
<tr>
<td>
<xsl:attribute name="id">
<xsl:value-of select="@id"/>
</xsl:attribute>
<table>
<xsl:variable name="Rows" select="@rows"/>
<xsl:variable name="Columns" select="@columns"/>
<xsl:for-each select="(//Section/Sections/Section)[$Rows >= position()]">
<tr>
<xsl:for-each select="(//Section/Sections/Section)[$Columns >= position()]">
<td>
<xsl:attribute name="id">
<xsl:value-of select="@id"/>
</xsl:attribute>
</td>
</xsl:for-each>
</tr>
</xsl:for-each>
</table>
</td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
我的输出:
<html>
<body>
<table>
<tr>
<td id="parent_hdr_sec">
<table>
<tr>
<td id="plan_hdr"></td>
</tr>
<tr>
<td id="plan_hdr"></td>
</tr>
</table>
</td>
</tr>
<tr>
<td id="splitter_sec">
<table>
<tr>
<td id="plan_hdr"></td>
<td id="plan_hdr_dtl"></td>
</tr>
</table>
</td>
</tr>
</table>
</body>
</html>
正确输出:
我的输出中错误地更新了“id”变量值。
我需要像下面这样的输出,
<html>
<body>
<table>
<tr>
<td id="parent_hdr_sec">
<table>
<tr>
<td id="plan_hdr"></td>
</tr>
<tr>
<td id="plan_hdr_dtl"></td>
</tr>
</table>
</td>
</tr>
<tr>
<td id="splitter_sec">
<table>
<tr>
<td id="parent_left_sec"></td>
<td id="parent_right_sec"></td>
</tr>
</table>
</td>
</tr>
</table>
</body>
</html>
我希望每个条件的 XSLT 都能获得上述输出。
谁能帮我解决这个问题..
【问题讨论】: