Xml 使用XSLT将元素属性前置到另一个元素的另一个属性
我希望做一个转换,在转换中,我获取一个元素的属性,并将其前置到另一个元素的属性值。下面是我想做的一个例子“Xml 使用XSLT将元素属性前置到另一个元素的另一个属性,xml,xslt,transformation,Xml,Xslt,Transformation,我希望做一个转换,在转换中,我获取一个元素的属性,并将其前置到另一个元素的属性值。下面是我想做的一个例子“ 这是我的价值观 这是我的另一个价值观 我想使用xslt进行转换,以执行以下操作: <Stuff> <subsystem value="ssname"> <item value="ssname_A">This is my value</item> &l
这是我的价值观
这是我的另一个价值观
我想使用xslt进行转换,以执行以下操作:
<Stuff>
<subsystem value="ssname">
<item value="ssname_A">This is my value</item>
<item value="ssname_B">This is my other value</item>
</subsystem>
</Stuff>
这是我的价值观
这是我的另一个价值观
如何使用XSLT 1.0做到这一点?以下样式表使用匹配的
项/@value
的上下文,使用表达式:。/../@value
捕捉子系统/@value
的值。或者,您可以使用/Stuff/subsystem/@value
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:output indent="yes"/>
<!--Identity template to copy content forward-->
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<!-- specialized template for value attribute that concatenates
the subsystem/@value with the current @value -->
<xsl:template match="item/@value">
<xsl:attribute name="value">
<xsl:value-of select="concat(../../@value,
'_',
.)"/>
</xsl:attribute>
</xsl:template>
</xsl:stylesheet>
此转换:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="item">
<item value="{../@value}_{@value}">
<xsl:apply-templates select="node()"/>
</item>
</xsl:template>
</xsl:stylesheet>
<Stuff>
<subsystem value="ssname">
<item value="A">This is my value</item>
<item value="B">This is my other value</item>
</subsystem>
</Stuff>
<Stuff>
<subsystem value="ssname">
<item value="ssname_A">This is my value</item>
<item value="ssname_B">This is my other value</item>
</subsystem>
</Stuff>
应用于提供的XML文档时:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="item">
<item value="{../@value}_{@value}">
<xsl:apply-templates select="node()"/>
</item>
</xsl:template>
</xsl:stylesheet>
<Stuff>
<subsystem value="ssname">
<item value="A">This is my value</item>
<item value="B">This is my other value</item>
</subsystem>
</Stuff>
<Stuff>
<subsystem value="ssname">
<item value="ssname_A">This is my value</item>
<item value="ssname_B">This is my other value</item>
</subsystem>
</Stuff>
这是我的价值观
这是我的另一个价值观
生成所需的正确结果:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="item">
<item value="{../@value}_{@value}">
<xsl:apply-templates select="node()"/>
</item>
</xsl:template>
</xsl:stylesheet>
<Stuff>
<subsystem value="ssname">
<item value="A">This is my value</item>
<item value="B">This is my other value</item>
</subsystem>
</Stuff>
<Stuff>
<subsystem value="ssname">
<item value="ssname_A">This is my value</item>
<item value="ssname_B">This is my other value</item>
</subsystem>
</Stuff>
这是我的价值观
这是我的另一个价值观