【发布时间】:2017-07-27 10:15:01
【问题描述】:
我想使用 xslt 将下面的 xml 转换为 html。
horder - 表示元素的行位置
vorder - 表示元素的列位置
行 - 总数。元素内的行数
列 - 总数。元素内的列数
<Page>
<Sections>
<Section id="parent_hdr_sec" haschild="y" layout="hbox" rows="2" columns="1" horder="1" vorder="1">
<Sections>
<Section id="plan_hdr" layout="vbox" rows="1" columns="6" horder="1" vorder="1" />
<Section id="plan_hdr_dtl" layout="vbox" rows="1" columns="5" horder="2" vorder="1" />
</Sections>
</Section>
<Section id="splitter_sec" haschild="y" layout="vbox" rows="1" columns="2" horder="2" vorder="1">
<Sections>
<Section id="parent_left_sec" haschild="y" layout="hbox" rows="4" columns="1" horder="1" vorder="1" />
<Section id="parent_right_sec" haschild="y" layout="hbox" rows="7" columns="1" horder="1" vorder="2" />
<Section id="parent_left_sec_1" layout="hbox" rows="1" columns="6" horder="2" vorder="1" />
<Section id="parent_right_sec_1" layout="hbox" rows="1" columns="5" horder="2" vorder="2" />
</Sections>
</Section>
</Sections>
</Page>
需要 HTML 输出:
<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>
<tr>
<td id="parent_left_sec_1"></td>
<td id="parent_right_sec_1"></td>
</tr>
</table>
</td>
</tr>
</table>
</body>
</html>
请帮助我为上述构建 XSLT...需要有关此方面的建议..
我的 XSLT:
<xsl:template match="/">
<html>
<body>
<table>
<xsl:for-each select="Page/Sections/Section">
<tr>
<td id="{@id}">
<table>
<xsl:variable name="Rows" select="@rows"/>
<xsl:variable name="Columns" select="@columns"/>
<xsl:variable name="Layout" select="@layout"/>
<xsl:if test="$Layout = 'hbox'">
<xsl:for-each select="Sections/Section[$Rows >= position()]">
<tr>
<td id="{@id}">
</td>
</tr>
</xsl:for-each>
</xsl:if>
<xsl:if test="$Layout = 'vbox'">
<tr>
<xsl:for-each select="Sections/Section[$Columns >= position()]">
<td id="{@id}">
</td>
</xsl:for-each>
</tr>
</xsl:if>
</table>
</td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
我使用 foreach 条件使用上述 XSLT 并获得这样的输出
我的输出:
<?xml version="1.0" encoding="utf-8"?>
<html>
<body>
<table>
<tr>
<td id="parent_hdr_sec">
<table>
<tr>
<td id="plan_hdr" />
</tr>
<tr>
<td id="plan_hdr_dtl" />
</tr>
</table>
</td>
</tr>
<tr>
<td id="splitter_sec">
<table>
<tr>
<td id="parent_left_sec" />
<td id="parent_right_sec" />
</tr>
</table>
</td>
</tr>
</table>
</body>
</html>
【问题讨论】:
-
投反对票,因为您还没有解释卡在哪里。您只是要求人们为您编写代码,而不是解释为什么您不能自己编写代码。
-
@MichaelKay 我是 XSLT 新手,所以只停留在这里...抱歉,我一开始没有发布我的 XSLT 代码...