2012-07-18 70 views
0

我需要結合組開始,與和組由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> 

對於具有相同@idaction=borrow隨後一個或多個每個節點節點action=extend

  • 將其合併到具有action = borrow的節點。
  • 合併兒童屬性,使其具有來自兄弟姐妹的具有最新值的所有獨特屬性。
  • 讓其他孩子不變

謝謝。 約翰

回答

1

那麼在語言中提供的結構是一個或一組或一組開始與所以我們不清楚你期望當你要求他們合併。

您可以做的是嵌套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']一組。

所以我認爲你需要更詳細地解釋你的輸入看起來以及你想如何分組。

+0

我已經添加了輸入和輸出expacted的XSL片斷。你介意看看嗎?非常感謝。 – John 2012-07-19 01:07:59

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>