XSLT:如何用所有兄弟姐妹的串联替换第一个兄弟姐妹?
XSLT: How to replace the first sibling with a concatenation of all siblings?
我很难尝试实施 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:每个records
标签的第一个item
的name
标签(实际文件中有很多记录)变成category
和包含当前 records
范围
中所有项目名称的串联
规则 #2:item
不是 records
的第一个子标签的标签保持不变。
我尝试使用 <xsl:value-of select="concat(' - ', .)"/>
规则,但没有成功。
有人知道如何实现吗?
使用身份转换作为基础模板并添加一个模板
<xsl:template match="records/item[1]/name">
<category>
<xsl:value-of select="., ../following-sibling::item/name" separator=" - "/>
</category>
</xsl:template>
试试这个:
<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>
我很难尝试实施 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:每个records
标签的第一个item
的name
标签(实际文件中有很多记录)变成category
和包含当前 records
范围
规则 #2:item
不是 records
的第一个子标签的标签保持不变。
我尝试使用 <xsl:value-of select="concat(' - ', .)"/>
规则,但没有成功。
有人知道如何实现吗?
使用身份转换作为基础模板并添加一个模板
<xsl:template match="records/item[1]/name">
<category>
<xsl:value-of select="., ../following-sibling::item/name" separator=" - "/>
</category>
</xsl:template>
试试这个:
<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>