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 的节点。
  • 将子属性合并在一起,使其具有兄弟姐妹的所有唯一属性和最新值。
  • 保持其他孩子不变

谢谢。约翰

4

2 回答 2

2

好吧,语言中提供的结构是 group-by 或 group-starting-with,因此当您要求组合它们时,我们不清楚您期望什么。

你可以做的是嵌套for-each-group例如

<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']

所以我认为您需要更详细地解释您的输入的外观以及您希望如何对其进行分组。

于 2012-07-18T16:29:11.890 回答
0

我想这应该这样做:

<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>

于 2014-08-13T08:40:46.087 回答