根据另一个元素值连接 XSLT 元素值 [英] Concatenate XSLT element values based on another element value
问题描述
我有以下 XML:
<root>
<attributes>
<attribute_value>ID1</attribute_value>
<attribute_name>id</attribute_name>
</attributes>
<attributes>
<attribute_value>ID2</attribute_value>
<attribute_name>id</attribute_name>
</attributes>
<attributes>
<attribute_value>some-link</attribute_value>
<attribute_name>link</attribute_name>
</attributes>
</root>
我想遍历文件并使用 XSLT(1.0 或 2.0)根据attribute_name"值连接所有attribute_value"值.所以在这种情况下,输出将是:
I want to iterate over the file and concatenate all the "attribute_value" values based on the "attribute_name" value using XSLT (1.0 or 2.0). So in this case the output would be:
<root>
<element name="id" value="ID1"/>
<element name="ids" value="ID1,ID2"/>
<element name="link" value="some-link"/>
</root>
id"元素应包含遇到的第一个值,ids"元素将包含连接的值.如何做到这一点?我有一个可用的 XSLT,我在其中使用标记并遍历所有属性,但无法解决此问题.
The "id" element should contain the first value encountered and the "ids" element would contain the concatenated value. How can this be achieved? I have a working XSLT where I use the tag and iterate over all the attributes, but not able to get around this problem.
以下是我当前的 XSLT:
The following is my current XSLT:
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" indent="yes"/>
<xsl:template match="root">
<root>
<xsl:for-each select="attributes">
<xsl:variable name="attribute_name" select="attribute_name"/>
<xsl:variable name="attribute_value" select="attribute_value"/>
<xsl:if test="$attribute_name = 'link'">
<element name="link">
<xsl:attribute name="value"><xsl:value-of select="$attribute_value" /></xsl:attribute>
</element>
</xsl:if>
</xsl:for-each>
</root>
</xsl:template>
</xsl:stylesheet>
推荐答案
当您使用 XSLT 2.0 时,您可以在此处使用 xsl:for-each-group
As you are using XSLT 2.0, you can use xsl:for-each-group
here
<xsl:for-each-group select="attributes" group-by="attribute_name">
然后输出第一个元素,你可以这样做:
Then to output the first element, you can do this:
<element name="{attribute_name}" value="{attribute_value}" />
(注意使用属性值模板来简化创建属性)
(Note the use of attribute value templates to simplify creating the attributes)
然后如果组中有多个元素,您可以像这样创建连接版本:
Then if there are more than one element in the group, you could create the concatenated version like so:
<element name="{@attribute_name}s" value="{string-join(current-group()/attribute_value, ',')}" />
试试这个 XSLT
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" indent="yes"/>
<xsl:template match="root">
<root>
<xsl:for-each-group select="attributes" group-by="attribute_name">
<element name="{attribute_name}" value="{attribute_value}" />
<xsl:if test="current-group()[2]">
<element name="{@attribute_name}s" value="{string-join(current-group()/attribute_value, ',')}" />
</xsl:if>
</xsl:for-each-group>
</root>
</xsl:template>
</xsl:stylesheet>
这篇关于根据另一个元素值连接 XSLT 元素值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!