我有一个非常扁平的XML结构,我需要将其重新排序到分类的部分中,而且,在我的一生中,我不知道如何在XSLT中做到这一点(并不是说我是专家)。
基本上,原始的XML看起来有点像:
<things>
<thing>
<value>one</value>
<type>a</type>
</thing>
<thing>
<value>two</value>
<type>b</type>
</thing>
<thing>
<value>thee</value>
<type>b</type>
</thing>
<thing>
<value>four</value>
<type>a</type>
</thing>
<thing>
<value>five</value>
<type>d</type>
</thing>
</things>我需要输出如下内容:
<data>
<a-things>
<a>one</a>
<a>four</a>
</a-things>
<b-things>
<b>two</b>
<b>three</b>
</b-things>
<d-things>
<d>five</d>
</d-things>
</data>注意,如果没有任何<c>元素,我就不能输出<c-things>,但是我提前知道完整的类型列表是什么,而且它相当短,所以为每种类型手工编写模板绝对是可能的。我觉得我可以用<xsl:if>和<xsl:for-each>一起破解一些东西,但我也觉得必须有更多的……“‘templatey”的方式。有人能帮上忙吗?
干杯。
发布于 2010-03-25 23:52:38
<a-things>
<xsl:for-each select="thing[type = 'a']">
<a><xsl:value-of select="./value" /></a>
</xsl:for-each>
</a-things>如果你想变得更时髦,用参数替换<a-things>和谓词,并使用属性值模板:
<xsl:param name="type" />
<xsl:element name="{$type}-things">
<xsl:for-each select="thing[type = $type]">
<xsl:element name="{$type}"><xsl:value-of select="./value" /></xsl:element>
</xsl:for-each>
</xsl:element>https://stackoverflow.com/questions/2516869
复制相似问题