继我关于XSLT 1.0 - Concatenate known child nodes, group by unknown parent的查询之后,在重复上层节点时与 Group/merge childs of same nodes in xml/xslt类似的困境,我想进一步定义我的分组,并且转换
<root>
<object>
<entry>
<id>apples</id>
<parent1>
<object_id>1</object_id>
</parent1>
<parent1>
<object_id>2</object_id>
</parent1>
<parent2>
<object_id>3</object_id>
</parent2>
<parent2>
<object_id>4</object_id>
</parent2>
<parent2>
<object_id>5</object_id>
</parent2>
</entry>
</object>
<object>
<entry>
<id>pears</id>
<parent1>
<object_id>5</object_id>
</parent1>
<parent1>
<object_id>4</object_id>
</parent1>
<parent2>
<object_id>3</object_id>
</parent2>
<parent2>
<object_id>2</object_id>
</parent2>
<parent2>
<object_id>1</object_id>
</parent2>
</entry>
</object>
</root>
进入
<root>
<object>
<entry>
<id>apples</id>
<parent1>1-2</parent1>
<parent2>3-4-5</parent2>
</entry>
</object>
<object>
<entry>
<id>pears</id>
<parent1>5-4</parent1>
<parent2>3-2-1</parent2>
</entry>
</object>
</root>
我正在尝试这样的事情(尽管整个示例已简化):
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes" />
<xsl:key name="groupKey" match="/object/*/*/object_id" use="concat(../../id/text(),name(..))"/>
<xsl:template match="/">
<xsl:apply-templates select="./*[object_id]"/>
</xsl:template>
<xsl:template match="/object/*/*[generate-id(object_id)=generate-id(key('groupName',concat(../id/text(),name()))[1])]">
<field>
<xsl:attribute name="name">
<xsl:value-of select="local-name()" />
</xsl:attribute>
<xsl:for-each select="key('groupName',concat(../id/text(),name()))">
<xsl:if test="not(position()=1)">-</xsl:if>
<xsl:value-of select="."/>
</xsl:for-each>
</field>
</xsl:template>
</xsl:stylesheet>
但是我缺乏对 XPath 的理解,这是在每个父分组的第一个中整理所有对象 ID(即,连接键不起作用)。
如果有人可以帮助我整理我的 XPath 语法,我将不胜感激。
提前致谢!