我很难实现XSL转换。
我需要对此进行转换:
<records>
<item>
<id type="uid">1</id>
<name>Homepage</name>
<attr>AB308E</attr>
</item>
<item>
<id type="uid">5</id>
<name>Electronics</name>
<attr>F04550</attr>
</item>
<item>
<id type="uid">8</id>
<name>Accessories</name>
<attr>00EE80</attr>
</item>
</records>
到这个:
<records>
<item>
<id type="uid">1</id>
<category>Homepage - Electronics - Accessories</category>
<attr>AB308E</attr>
</item>
<item>
<id type="uid">5</id>
<name>Electronics</name>
<attr>F04550</attr>
</item>
<item>
<id type="uid">8</id>
<name>Accessories</name>
<attr>00EE80</attr>
</item>
</records>
我知道从语义上讲并没有多大意义,但这是我需要的一种技巧,以便以特定方式将数据注入某个接口。
规则1:每个标签name
的第item
一个records
标签(实际文件中有很多记录)变成category
并包含当前records
作用域中所有项目名称的串联
规则2:item
不是第一个子标签的标签records
保持不变。
我尝试使用<xsl:value-of select="concat(' - ', .)"/>
规则,但没有运气。
有人知道如何实现这一目标吗?
试试这个:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="records/item[1]/name">
<category>
<xsl:for-each select="../../item/name">
<xsl:value-of select="." />
<xsl:if test="position() != last()">
<xsl:value-of select="' - '" />
</xsl:if>
</xsl:for-each>
</category>
</xsl:template>
</xsl:stylesheet>
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句