基于另一个元素值连接 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" 值。所以在这种情况下,输出将是:
<root>
<element name="id" value="ID1"/>
<element name="ids" value="ID1,ID2"/>
<element name="link" value="some-link"/>
</root>
"id" 元素应包含遇到的第一个值,"ids" 元素将包含连接后的值。如何实现?我有一个有效的 XSLT,我在其中使用标记并遍历所有属性,但无法解决这个问题。
以下是我当前的 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
<xsl:for-each-group select="attributes" group-by="attribute_name">
然后输出第一个元素,可以这样做:
<element name="{attribute_name}" value="{attribute_value}" />
(注意使用 attribute value templates 来简化创建属性)
然后如果组中有多个元素,您可以像这样创建串联版本:
<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>
我有以下 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" 值。所以在这种情况下,输出将是:
<root>
<element name="id" value="ID1"/>
<element name="ids" value="ID1,ID2"/>
<element name="link" value="some-link"/>
</root>
"id" 元素应包含遇到的第一个值,"ids" 元素将包含连接后的值。如何实现?我有一个有效的 XSLT,我在其中使用标记并遍历所有属性,但无法解决这个问题。
以下是我当前的 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
<xsl:for-each-group select="attributes" group-by="attribute_name">
然后输出第一个元素,可以这样做:
<element name="{attribute_name}" value="{attribute_value}" />
(注意使用 attribute value templates 来简化创建属性)
然后如果组中有多个元素,您可以像这样创建串联版本:
<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>