我需要結合組開始,與和組由XSLT 2.0如何在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的節點。
- 合併兒童屬性,使其具有來自兄弟姐妹的具有最新值的所有獨特屬性。
- 讓其他孩子不變
謝謝。 約翰
我已經添加了輸入和輸出expacted的XSL片斷。你介意看看嗎?非常感謝。 – John 2012-07-19 01:07:59