【发布时间】:2019-09-18 10:12:31
【问题描述】:
我的输入 xml 是这样的:
<row>
<entry colname="col1" colsep="0" rowsep="0">Get</entry>
<entry colname="col2" colsep="0" rowsep="0">Some</entry>
<entry colname="col3" colsep="0" rowsep="0">Manual</entry>
</row>
<row>
<entry colname="col1" colsep="0" rowsep="0">Skip</entry>
</row>
<row>
<entry colname="col1" colsep="0" rowsep="0" namest="col1" nameend="col3">Temp</entry>
</row>
<row>
<entry colname="col1" colsep="0" rowsep="0" namest="col1" nameend="col3">Task</entry>
</row>
我使用过的XSL如下:
<xsl:template match="row">
<row>
<xsl:apply-templates/>
</row>
</xsl:template>
<xsl:template match="entry">
<entry>
<xsl:attribute name="colname">
<xsl:value-of select="@colname"/>
</xsl:attribute>
<xsl:attribute name="colsep">
<xsl:value-of select="@colsep"/>
</xsl:attribute>
<xsl:attribute name="rowsep">
<xsl:value-of select="@rowsep"/>
</xsl:attribute>
<xsl:apply-templates/>
</entry>
</xsl:template>
<xsl:template match="entry[@colname and @namest and @nameend]">
<entry>
<xsl:attribute name="colname">
<xsl:value-of select="@colname"/>
</xsl:attribute>
<xsl:attribute name="colsep">
<xsl:value-of select="@colsep"/>
</xsl:attribute>
<xsl:attribute name="rowsep">
<xsl:value-of select="@rowsep"/>
</xsl:attribute>
<xsl:attribute name="namest">
<xsl:value-of select="@namest"/>
</xsl:attribute>
<xsl:attribute name="nameend">
<xsl:value-of select="@nameend"/>
</xsl:attribute>
<xsl:apply-templates/>
</entry>
</xsl:template>
预期输出如下:
<row>
<entry colname="col1" colsep="0" rowsep="0">Get</entry>
<entry colname="col2" colsep="0" rowsep="0">Some</entry>
<entry colname="col3" colsep="0" rowsep="0">Manual</entry>
</row>
<row>
<entry colname="col1" colsep="0" rowsep="0">Skip</entry>
<entry colname="col2" colsep="0" rowsep="0"/>
<entry colname="col3" colsep="0" rowsep="0">
</row>
<row>
<entry colname="col1" colsep="0" rowsep="0" namest="col1" nameend="col3">Temp</entry>
</row>
<row>
<entry colname="col1" colsep="0" rowsep="0" namest="col1" nameend="col3">Task</entry>
</row>
有很多具有 namest 和 nameend 属性的条目。上面的条目模板适用于行内有 3 个条目的情况。我想通过使用行内的条目数来创建新的条目模板。请提出建议。
【问题讨论】:
-
xsl:attribute的用例是动态属性的名称。改用AVT:<entry colname="{@colname}" colsep="{@colsep}" rowsep="{@rowsep}"><xsl:apply-templates/></entry>