我想改造
<entry>
<parent1>
<object_id>1580</object_id>
</parent1>
<parent1>
<object_id>1586</object_id>
</parent1>
<parent2>
<object_id>1582</object_id>
</parent2>
<parent2>
<object_id>1592</object_id>
</parent2>
</entry>
成
<entry>
<parent1>1580-1586</parent1>
<parent2>1582-1592</parent2>
</entry>
顶级条目名称未知 . 父名称是未知的,并且具有相同名称的父节点的数量可以变化 . 子节点是已知的“object_id” .
因此,我想以抽象的方式对未知父项进行分组,并将子节点值连接起来,用“ - ”分隔 .
Merge XML nodes using XSLT接近回答这个问题,Group/merge childs of same nodes in xml/xslt也是如此,但它们并不是我所需要的 .
到目前为止,我有:
<xsl:key name="groupName" match="*[object_id]" use="."/>
<xsl:template match="*[generate-id(.) = generate-id(key('groupName', .))]">
<xsl:copy>
<xsl:call-template name="join">
<xsl:with-param name="list" select="object_id" />
<xsl:with-param name="separator" select="'-'" />
</xsl:call-template>
</xsl:copy>
</xsl:template>
<xsl:template name="join">
<xsl:param name="list" />
<xsl:param name="separator"/>
<xsl:for-each select="$list">
<xsl:value-of select="." />
<xsl:if test="position() != last()">
<xsl:value-of select="$separator" />
</xsl:if>
</xsl:for-each>
</xsl:template>
提前致谢!
2 回答
This transformation:
when applied on the provided XML document:
produces the wanted, correct result:
Explanation :
正确使用和覆盖identity rule .
正确使用Muenchian grouping method .
II. In case all values must be concatenated together, use this slightly modified solution:
这是一个稍微不同的解决方案,在我注意到Dimitre的帖子之前开发 .
更新
我更新了样式表以反映OP关于' - '作为分隔符的注释,而不是第一个和最后一个值之间的分隔符 .