我需要在 XSLT 2.0 中结合 group-starting-with 和 group-by
<xsl:for-each-group select="xxx[@attr='yyy']" group-by="@id" group-starting-with="xxx[@attr='yyy']">
...
</xsl:for-each-group>
如何实现这样的组合?
输入:
<root>
<library id="L1">
<genre id="a">
<shelf1 id="1">
<book id="a1" action="borrow">
<attributes>
<user>John</user>
</attributes>
<other1>y</other1>
</book>
<book id="a1" action="extend">
<attributes>
<user>Woo</user>
<length>3</length>
</attributes>
<other2>y</other2>
</book>
</shelf1>
</genre>
</library>
</root>
输出:
<root>
<library id="L1">
<genre id="a">
<shelf1 id="1">
<book id="a1" action="borrow">
<attributes>
<user>Woo</user>
<length>3</length>
</attributes>
<other1>y</other1>
</book>
</shelf1>
</genre>
</library>
</root>
我的 XSL 片段:
<xsl:template match="genre/*">
<xsl:copy>
<xsl:apply-templates select="@*" />
<xsl:apply-templates select="
book[@action='extend']
[not( preceding-sibling::book[@action='borrow'])]" />
<xsl:for-each-group
select="book[@action='borrow']
|
book[@action='extend']
[preceding-sibling::book[@action='borrow']]"
group-by="@id" group-starting-with="book[@action='borrow']"> (: "This is the one which needs to be combined :)
<xsl:for-each select="current-group()[1]">
<xsl:copy>
<xsl:apply-templates select="@*" />
<xsl:call-template name="merge-books-deeply">
<xsl:with-param name="books" select="current-group()" />
<xsl:with-param name="name-path" select="()" />
</xsl:call-template>
</xsl:copy>
</xsl:for-each>
</xsl:for-each-group>
<xsl:apply-templates select="
node()[ not( self::book[@action=('borrow','extend')])]" />
</xsl:copy>
</xsl:template>
对于每个具有相同@id
的节点,action=borrow
后跟一个或多个节点action=extend
- 将其合并到带有 action=borrow 的节点。
- 将子属性合并在一起,使其具有兄弟姐妹的所有唯一属性和最新值。
- 保持其他孩子不变
谢谢。约翰