Xml 在XSLT 2.0中,如何组合以开头的组和以开头的组?
在XSLT2.0中,我需要将group start with和group by结合起来Xml 在XSLT 2.0中,如何组合以开头的组和以开头的组?,xml,xslt,Xml,Xslt,在XSLT2.0中,我需要将group start 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">
<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>
约翰
Y
求爱
3.
Y
输出:
<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>
求爱
3.
Y
我的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将其合并到节点
- 将属性子项合并在一起,这样它将具有来自具有最新值的同级的所有唯一属性
- 让其他孩子保持不变
John语言中提供的结构是一个分组或以开始的分组,因此我们不清楚当您要求组合它们时,您期望得到什么 您可以为每个组嵌套
<xsl:for-each-group select="xxx[@att = 'yyy']" group-by="@id">
<xsl:for-each-group select="current-group()" group-starting-with="xxx[@attr = 'yyy']">...</xsl:for-each-group>
</xsl:for-each-group>
...
尽管在写这篇文章时,我想知道分组人群xxx[@att='yyy']
和以模式xxx[@att='yyy']
开头的群体有什么意义
因此,我认为您需要更详细地解释您的输入外观以及您希望如何对其进行分组。我想这应该可以:
我已经添加了包含输入和输出的xsl代码段。您介意看一看吗?非常感谢。
<xsl:template match="root">
<xsl:apply-templates select="library | genre"/>
<xsl:apply-templates select="shelf1"/>
</xsl:template>
<xsl:template match="library | genre">
<xsl:copy>
<xsl:copy-of select="@*"/>
<xsl:apply-templates select="library | genre | shelf1"/>
</xsl:copy>
</xsl:template>
<xsl:template match="shelf1">
<xsl:copy>
<xsl:copy-of select="@*"/>
<xsl:for-each-group select="current()/book" group-by="@id">
<book id="{current-grouping-key()}" action="borrow">
<xsl:copy-of select="current-group()[@action='extend']/*"/>
</book>
</xsl:for-each-group>
</xsl:copy>
</xsl:template>