2012-03-01 151 views
2

在XSLT 2.0我想与2个参数得到的独特的节点不同的值返回节点..与基于在XSLT 2个参数2.0

例如,我有:

<items> 
     <item x="1" y="1" z="a"/> 
     <item x="1" y="2" z="b"/> 
     <item x="2" y="1" z="c"/> 
     <item x="1" y="2" z="d"/> 
     <item x="2" y="2" z="e"/> 
    </items> 

我想结果是:

<items> 
     <item x="1" y="1"/> 
     <item x="1" y="2"/> 
     <item x="2" y="1"/> 
     <item x="2" y="2"/> 
    </items> 

有没有更简单的方法来得到它? 我目前的代码真的是多余的。

回答

2
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 

    <xsl:output method="xml" indent="yes"/> 

    <xsl:template match="/items"> 
     <xsl:copy> 
      <xsl:for-each-group select="item" group-by="concat(@x,'#',@y)"> 
       <item x="{@x}" y="{@y}"/> 
      </xsl:for-each-group> 
     </xsl:copy> 
    </xsl:template> 
</xsl:stylesheet> 

编辑:的背景是该组的第一个项目,所以没有必要current-group()[1]。谢谢Michael Kay。

+0

谢谢。所述的concat确实起作用 – DRTauli 2012-03-01 07:27:57

+2

由于内的for-each-组是当前的基团()[1],则可以删除该变量,只是使用'<项x = “{@ X}” Y =“{上下文项@ Y}“/>。但也许更长的形式更具可读性。 – 2012-03-01 08:57:28