我在尝试实现 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>
我知道从语义上讲这没有多大意义,但这是我需要的一种技巧,以便以特定方式将数据注入(inject)到某些界面中。
规则#1:name
第一个标签item
每个records
tag(实际文件中有很多条记录)变成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>
关于xml - XSLT:如何用所有 sibling 的串联替换第一个 sibling ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66127319/