2011-09-28 68 views
1

來獲取不同的元素我有下面元素命名查詢表元素的列表,可重複,我需要獲取不同的表格元素(而不是它的價值,但是標籤/元素名稱本身。需要通過XSLT

/ShopArea/Connection/Query/*名單表名包括重複。

下面是XML

<?xml version="1.0" encoding="utf-8"?> 
<?xml-stylesheet type="text/xsl" href="ShopArea.xslt"?> 
<ShopArea> 
<Connection name="Connection1"> 
    <Report date="25-09-2011"> 

     <Query id="1"> 
      <TABLE1>1.1</TABLE1> 
      <TABLE2>1.2</TABLE2> 
      <TABLE3>1.3</TABLE3> 
     </Query> 
     <Query id="2"> 
      <TABLE21>2.1</TABLE21> 
      <TABLE22>2.2</TABLE22> 
      <TABLE23>2.3</TABLE23> 
     </Query> 
    </Report> 

    <Report date="26-09-2011"> 
     <Query id="1"> 
      <TABLE1>26 1.1</TABLE1> 
      <TABLE2>26 1.2</TABLE2> 
      <TABLE3>26 1.3</TABLE3> 
     </Query> 
     <Query id="2"> 
      <TABLE21>26 2.1</TABLE21> 
      <TABLE22>26 2.2</TABLE22> 
      <TABLE23>26 2.3</TABLE23> 
     </Query> 
    </Report> 
</Connection> 
</ShopArea> 

List of elements including duplicates

我指的是How to select unique nodes in XSLT,但我無法做到。

回答

1

I. XSLT 1.0。這種變換簡單Muenchian grouping

<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="kChildByName" match="Query/*" 
    use="name()"/> 

<xsl:template match= 
    "Query/* 
     [generate-id() 
     = 
     generate-id(key('kChildByName', name())[1]) 
     ]"> 
    <xsl:value-of select="name()"/> 
    <xsl:text>&#xA;</xsl:text> 
</xsl:template> 

<xsl:template match="text()"/> 
</xsl:stylesheet> 

當所提供的XML文檔應用:

<ShopArea> 
    <Connection name="Connection1"> 
     <Report date="25-09-2011"> 
      <Query id="1"> 
       <TABLE1>1.1</TABLE1> 
       <TABLE2>1.2</TABLE2> 
       <TABLE3>1.3</TABLE3> 
      </Query> 
      <Query id="2"> 
       <TABLE21>2.1</TABLE21> 
       <TABLE22>2.2</TABLE22> 
       <TABLE23>2.3</TABLE23> 
      </Query> 
     </Report> 
     <Report date="26-09-2011"> 
      <Query id="1"> 
       <TABLE1>26 1.1</TABLE1> 
       <TABLE2>26 1.2</TABLE2> 
       <TABLE3>26 1.3</TABLE3> 
      </Query> 
      <Query id="2"> 
       <TABLE21>26 2.1</TABLE21> 
       <TABLE22>26 2.2</TABLE22> 
       <TABLE23>26 2.3</TABLE23> 
      </Query> 
     </Report> 
    </Connection> 
</ShopArea> 

想要的,正確的結果(即是Query的子元素的所有不同的名字)生產

TABLE1 
TABLE2 
TABLE3 
TABLE21 
TABLE22 
TABLE23 

二, XSLT 2.0:這種變換<xsl:for-each-group>

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

<xsl:template match="/*/*"> 
    <xsl:for-each-group select="Report/Query/*" 
         group-by="name()"> 
     <xsl:sequence select="current-grouping-key(), '&#xA;'"/> 
    </xsl:for-each-group> 
</xsl:template> 

<xsl:template match="text()"/> 
</xsl:stylesheet> 
+0

好極了!它可以工作,但是這個代碼(Muenchian分組)看起來太複雜了。需要一段時間才能理解。非常感謝你。 –

+0

@ Arvind Singh:不客氣。 Muenchian分組是高效XSLT 1.0分組的事實標準。爲了更好地理解它,只需按照此答案的第一行中的鏈接 - 這會導致Jeni Tennison出色的Muenchian分組頁面。 –