5
<ROOT>
<AA Aattr="xyz1">
    <BB bAttr1="firefox"  bAttr2="aaa" >
    </BB>   
    <BB bAttr1="chrome"   bAttr2="aaa" >
    </BB>
    <BB bAttr1="firefox"  bAttr2="bbb" >
    </BB>
    <BB bAttr1="chrome"   bAttr2="bbb" >
    </BB>
</AA>
<AA Aattr="xyz2">
    <BB bAttr1="firefox"  bAttr2="aaa" >
    </BB>   
    <BB bAttr1="chrome"   bAttr2="ccc" >
    </BB>
    <BB bAttr1="firefox"  bAttr2="ddd" >
    </BB>
</AA>

我想从节点'AA'中选择节点'BB'中的属性'bAttr2'的不同\唯一值,其中属性'Aattr'是xyz1

说给定的xml,我需要输出为“aaa”,“bbb”

我使用key尝试了以下逻辑。但没有奏效。请帮忙

<xsl:key name="nameDistinct" match="BB" use="@bAttr1"/>
<xsl:template match="/">
<xsl:for-each select="ROOT/AA[@Aattr='xyz1']">
    <xsl:for-each select="BB[generate-id()=generate-id(key('nameDistinct',@bAttr2)[1])]">
    <xsl:value-of select="@bAttr2"/>            
    </xsl:for-each>
</xsl:for-each>
</xsl:template>
4

2 回答 2

5

您在这里有两个选择:

定义键时过滤键可用的项目:

  <xsl:key name="nameDistinct" match="AA[@Aattr = 'xyz1']/BB" use="@bAttr2"/>

  <xsl:template match="/">
    <xsl:for-each 
      select="ROOT/AA/BB[generate-id() =
                         generate-id(key('nameDistinct', @bAttr2)[1])]">
      <xsl:value-of select="@bAttr2"/>
    </xsl:for-each>
  </xsl:template>

或在分组表达式中过滤:

  <xsl:key name="nameDistinct" match="BB" use="@bAttr2"/>

  <xsl:template match="/">
    <xsl:for-each 
      select="ROOT/AA/BB[generate-id() =
                         generate-id(key('nameDistinct', @bAttr2)
                                       [../@Aattr = 'xyz1']
                                       [1])]">
      <xsl:value-of select="@bAttr2"/>
    </xsl:for-each>
  </xsl:template>

前一种方法不那么混乱,效率略高,而后者允许您对分组进行参数化(即对未硬编码为“xyz1”的值进行分组),例如:

  <xsl:key name="nameDistinct" match="BB" use="@bAttr2"/>

  <xsl:template match="/">
    <xsl:for-each select="ROOT/AA">
      <xsl:for-each
        select="BB[generate-id() =
                   generate-id(key('nameDistinct', @bAttr2)
                                    [../@Aattr = current()/@Aattr]
                                    [1])]">
        <xsl:value-of select="@bAttr2"/>
      </xsl:for-each>
    </xsl:for-each>
  </xsl:template>
于 2013-03-21T13:36:39.173 回答
2

This transformation:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>

 <xsl:key name="kCompoundAttribs" match="BB"
  use="concat(generate-id(..), '+', @bAttr2)"/>

 <xsl:template match="/">
  <xsl:copy-of select=
   "/*/*[1]/*
     [generate-id()
     =
      generate-id(key('kCompoundAttribs',
                      concat(generate-id(..),'+', @bAttr2)
                      )[1]
                  )
     ]"/>
 </xsl:template>
</xsl:stylesheet>

when applied on the provided XML document:

<ROOT>
    <AA Aattr="xyz1">
        <BB bAttr1="firefox"  bAttr2="aaa" ></BB>
        <BB bAttr1="chrome"   bAttr2="aaa" ></BB>
        <BB bAttr1="firefox"  bAttr2="bbb" ></BB>
        <BB bAttr1="chrome"   bAttr2="bbb" ></BB>
    </AA>
    <AA Aattr="xyz2">
        <BB bAttr1="firefox"  bAttr2="aaa" ></BB>
        <BB bAttr1="chrome"   bAttr2="ccc" ></BB>
        <BB bAttr1="firefox"  bAttr2="ddd" ></BB>
    </AA>
</ROOT>

produces two BB elements, whose bAttr2 attributes have the desired set of distinct values (if you want just the string values of the attributes, just use xsl:apply-templates or xsl:for-each for that expression):

<BB bAttr1="firefox" bAttr2="aaa"/>
<BB bAttr1="firefox" bAttr2="bbb"/>

Do note:

This solution is simpler and more easier to understand and more efficient than "filtering" :)

于 2013-03-22T03:34:42.443 回答