2012-05-30 31 views
1

我有下面的XML輸入文件:XSLT分組和留出複製

<mappings> 
    <mapping> 
     <key>6718</key> 
     <value attribute="content_type">Info Page</value> 
    </mapping> 
    <mapping> 
     <key>35905</key> 
     <value attribute="content_type">Press releases</value> 
    </mapping> 
    <mapping> 
     <key>6718</key> 
     <value attribute="content_type">Info Page</value> 
    </mapping> 
    <mapping> 
     <key>36941</key> 
     <value attribute="content_type">Press releases</value> 
    </mapping> 
    <mapping> 
     <key>24920</key> 
     <value attribute="content_type">Press releases</value> 
    </mapping> 
    <mapping> 
     <key>40244</key> 
     <value attribute="content_type">Press releases</value> 
    </mapping> 
    <mapping> 
     <key>36639</key> 
     <value attribute="content_type">Press releases</value> 
    </mapping> 
    <mapping> 
     <key>1861</key> 
     <value attribute="content_type">Press releases</value> 
    </mapping> 
    <mapping> 
     <key>2280</key> 
     <value attribute="content_type">Press releases</value> 
    </mapping> 
    <mapping> 
     <key>42062</key> 
     <value attribute="content_type">Press releases</value> 
    </mapping> 
</mappings> 

我想產生以下XML:

<reductions> 
    <reduction> 
     <group>Info Page</group> 
     <key>6718</key> 
    </reduction> 
    <reduction> 
     <group>Press releases</group> 
     <key>35905</key> 
     <key>36941</key> 
     <key>24920</key> 
     <key>40244</key> 
     <key>36639</key> 
     <key>1861</key> 
     <key>2280</key> 
     <key>42062</key> 
    </reduction> 
</reductions> 

所以從輸入XML密鑰必須根據輸入XML中值節點的值將其分組到輸出XML中。 此外,需要刪除重複項,輸入XML包含值爲6718兩次的鍵,輸出XML只有一次。

我對Saxon 9 HE使用XSLT 2.0。

有人可以幫我嗎?

[編輯]

<?xml version="1.0" encoding="UTF-8"?> 
<xsl:transform 
    version="2.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    xmlns:xs="http://www.w3.org/2001/XMLSchema" 
    exclude-result-prefixes="xs"> 

    <xsl:template match="/"> 
     <xsl:variable name="mappings"> 
      <mappings> 
       <xsl:apply-templates select="resource/R"/> 
      </mappings> 
     </xsl:variable> 
     <reductions> 
      <xsl:apply-templates select="$mappings/*"/> 
     </reductions> 
    </xsl:template> 

    <xsl:template match="R"> 
     <mapping> 
      <key><xsl:value-of select="MT[@N='content_id']/@V"/></key> 
      <value> 
       <xsl:attribute name="attribute">content_type</xsl:attribute> 
       <xsl:value-of select="MT[@N='content_type']/@V"/> 
      </value> 
     </mapping> 
     <!--mapping> 
      <key><xsl:value-of select="MT[@N='content_id']/@V"/></key> 
      <value> 
       <xsl:attribute name="attribute">modified_timestamp</xsl:attribute> 
       <xsl:value-of select="format-dateTime(MT[@N='modified_timestamp']/@V, '[Y0001]-[M01]-[D01]')"/> 
      </value> 
     </mapping--> 
    </xsl:template> 

    <xsl:template match="mapping"> 
     <xsl:for-each-group select="." group-by="value"> 
      <reduction> 
       <group><xsl:value-of select="current-grouping-key()"/></group> 
       <xsl:for-each-group select="current-group()/key" group-by="."> 
        <xsl:copy-of select="."/> 
       </xsl:for-each-group> 
      </reduction> 
     </xsl:for-each-group> 
    </xsl:template> 

</xsl:transform> 

回答

4

下面是一個樣式表使用for-each-group兩次:

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

<xsl:output indent="yes"/> 

<xsl:template match="mappings"> 
    <reductions> 
    <xsl:for-each-group select="mapping" group-by="value"> 
     <reduction> 
     <group><xsl:value-of select="current-grouping-key()"/></group> 
     <xsl:for-each-group select="current-group()/key" group-by="."> 
      <xsl:copy-of select="."/> 
     </xsl:for-each-group> 
     </reduction> 
    </xsl:for-each-group> 
    </reductions> 
</xsl:template> 

</xsl:stylesheet> 

[編輯] 當運行與撒克遜9.4張貼的樣式表HE相對於輸入

<mappings> 
    <mapping> 
     <key>6718</key> 
     <value attribute="content_type">Info Page</value> 
    </mapping> 
    <mapping> 
     <key>35905</key> 
     <value attribute="content_type">Press releases</value> 
    </mapping> 
    <mapping> 
     <key>6718</key> 
     <value attribute="content_type">Info Page</value> 
    </mapping> 
    <mapping> 
     <key>36941</key> 
     <value attribute="content_type">Press releases</value> 
    </mapping> 
    <mapping> 
     <key>24920</key> 
     <value attribute="content_type">Press releases</value> 
    </mapping> 
    <mapping> 
     <key>40244</key> 
     <value attribute="content_type">Press releases</value> 
    </mapping> 
    <mapping> 
     <key>36639</key> 
     <value attribute="content_type">Press releases</value> 
    </mapping> 
    <mapping> 
     <key>1861</key> 
     <value attribute="content_type">Press releases</value> 
    </mapping> 
    <mapping> 
     <key>2280</key> 
     <value attribute="content_type">Press releases</value> 
    </mapping> 
    <mapping> 
     <key>42062</key> 
     <value attribute="content_type">Press releases</value> 
    </mapping> 
</mappings> 

我得到結果

<reductions> 
    <reduction> 
     <group>Info Page</group> 
     <key>6718</key> 
    </reduction> 
    <reduction> 
     <group>Press releases</group> 
     <key>35905</key> 
     <key>36941</key> 
     <key>24920</key> 
     <key>40244</key> 
     <key>36639</key> 
     <key>1861</key> 
     <key>2280</key> 
     <key>42062</key> 
    </reduction> 
</reductions> 

就我的理解您的要求而言,這是正確的。

+0

這不是預期的結果。我在輸入中獲得與節點一樣多的節點,每個節點只有一個和一個元素。他們根本沒有分組。 – mmjmanders

+0

對不起,我不明白你爲什麼會得到這樣的結果。我使用Saxon 9.4進行了測試,現在我的答案中包含的輸出結果似乎是您想要的。請提供您使用的XSLT 2.0處理器的詳細信息,以及是否有任何細節(如輸入中的名稱空間)在您的發佈中遺漏了。 –

+0

當我運行命令行時它可以工作,但是當從Java應用程序調用它時不起作用。 – mmjmanders