2012-08-27 70 views
2

這是我的XML輸入Muenchian分組的

<issues> 
    <issue status="open" type="Bug"> 
    <fix_versions> 
     <fix_version>6.14.0</fix_version> 
     <fix_version>6.13.0</fix_version> 
     <fix_version>6.12.0</fix_version> 
    </fix_versions> 
    <title>issue1</title> 
    <description>Description</description> 
    </issue> 
    <issue status="open" type="Feature"> 
    <fix_versions> 
     <fix_version>6.13.0</fix_version> 
    </fix_versions> 
    <title>issue2</title> 
    <description>Description</description> 
    </issue> 
    <issue status="open" type="Improvement"> 
    <fix_versions> 
     <fix_version>6.14.0</fix_version> 
    </fix_versions> 
    <title>issue3</title> 
    <description>Description</description> 
    </issue> 
    ... 
</issues> 

,這是我的XSLT

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

    <xsl:key name="versions" match="fix_version" use="."/> 
    <xsl:key name="type" match="@type" use="../*/fix_version"/> 

    <xsl:template match="fix_version[generate-id()=generate-id(key('versions',.)[1])]"> 
    <div class="version"> 
    <a><xsl:value-of select="."/></a> 
    <xsl:value-of select="../../@type"/> 
     <div class="issues"> 
     <xsl:for-each select="key('type',.)"> 
      <div class="{..}"> 
      <a href="{../title}"><xsl:value-of select="../title"/></a> 
      </div> 
     </xsl:for-each> 
     </div> 
    </div> 
    </xsl:template> 
<xsl:template match="text()"/> 
</xsl:stylesheet> 

,我想這樣的輸出:

<div class="version"> 
    <a>6.14.0</a> 
    <div class="issues"> 
    <div class="Bugs"> 
     <a href="issue1">issue1</a> 
     <a href="issue7">issue7</a> 
     <a href="issue2">issue12</a> 
     <a href="issue17">issue17</a> 
    </div> 
    <div class="Improvements"> 
     <a href="issue3">issue3</a> 
     <a href="issue9">issue9</a> 
    </div> 
    </div> 
</div> 
<div class="version"> 
    <a>6.13.0</a> 
    <div class="issues"> 
    <div class="Bug"> 
     <a href="issue1">issue1</a> 
     <a href="issue11">issue11</a> 
    </div> 
    <div class="Feature"> 
     <a href="issue2">issue2</a> 
    </div> 
    </div> 
</div> 
<div class="version"> 
    <a>6.12.0</a> 
    <div class="issues"> 
    <div class="Bug"> 
     <a href="issue1">issue1</a> 
    </div> 
    </div> 
</div> 

但我有這樣的輸出:

<div class="version"> 
    <a>6.14.0</a> 
    <div class="issues"> 
    <div class="Bugs"> 
     <a href="issue1">issue1</a> 
    </div> 
    <div class="Bugs"> 
     <a href="issue7">issue7</a> 
    </div> 
    <div class="Bugs"> 
     <a href="issue2">issue12</a> 
    </div> 
    <div class="Bugs"> 
     <a href="issue17">issue17</a> 
    </div> 
    <div class="Improvements"> 
     <a href="issue3">issue3</a> 
    </div> 
    <div class="Improvements"> 
     <a href="issue9">issue9</a> 
    </div> 
    </div> 
</div> 
<div class="version"> 
    <a>6.13.0</a> 
    <div class="issues"> 
    <div class="Bug"> 
     <a href="issue1">issue1</a> 
    </div> 
    <div class="Bug"> 
     <a href="issue11">issue11</a> 
    </div> 
    <div class="Feature"> 
     <a href="issue2">issue2</a> 
    </div> 
    </div> 
</div> 
<div class="version"> 
    <a>6.12.0</a> 
    <div class="issues"> 
    <div class="Bug"> 
     <a href="issue1">issue1</a> 
    </div> 
    </div> 
</div> 

正如你看到的問題沒有分組在一個名爲「bug」,「改進」或「功能」的容器 - 但這就是我想要的。 我試過這樣的東西

但無論我嘗試 - 沒有任何工作。我真的沒有計劃如何分組他們。

回答

0

我認爲你必須讓你的鑰匙更具體。在XSLT中定義一個鍵時,它總是爲整個XML文檔定義的,而不是部分。定義密鑰時,必須concat()多個值。

Plesae看看這個線程:Muenchian Grouping - group within a node, not within the entire document

最好的問候, 彼得

+0

嗨,我已經試過了 - 我的問題的是,該密鑰,並與每個行必須是相同的,我不知道如何要做到這一點。 你知道我是否像這樣定義了這個鍵?',我是否可以像下面這樣爲每一行使用該行:''或者我還需要做另一個連接嗎? 哦,我真的閱讀關於muenchian方法的一些文章,並真正嘗試了它自己,但由於某種原因,我沒有得到我實際上在哪個節點。: -/ – KddC

+0

你好KddC,我嘗試了一段時間,老實說不能得到你想要的輸出你的問題+1 – Peter

+0

我還沒有找到解決方案 - 但我仍然試圖... – KddC

0

這種轉變

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

    <xsl:key name="versions" match="fix_version" use="."/> 
    <xsl:key name="type" match="@type" use="../*/fix_version"/> 

    <xsl:template match="fix_version[generate-id()=generate-id(key('versions',.)[1])]"> 
    <div class="version"> 
    <a><xsl:value-of select="."/></a> 
     <div class="issues"> 
     <xsl:for-each select="key('type',.)"> 
      <div class="{.}"> 
      <a href="{../title}"><xsl:value-of select="../title"/></a> 
      </div> 
     </xsl:for-each> 
     </div> 
    </div> 
    </xsl:template> 
<xsl:template match="text()"/> 
</xsl:stylesheet> 

時所提供的XML文檔應用:

<issues> 
    <issue status="open" type="Bug"> 
    <fix_versions> 
     <fix_version>6.14.0</fix_version> 
     <fix_version>6.13.0</fix_version> 
     <fix_version>6.12.0</fix_version> 
    </fix_versions> 
    <title>issue1</title> 
    <description>Description</description> 
    </issue> 
    <issue status="open" type="Feature"> 
    <fix_versions> 
     <fix_version>6.13.0</fix_version> 
    </fix_versions> 
    <title>issue2</title> 
    <description>Description</description> 
    </issue> 
    <issue status="open" type="Improvement"> 
    <fix_versions> 
     <fix_version>6.14.0</fix_version> 
    </fix_versions> 
    <title>issue3</title> 
    <description>Description</description> 
    </issue> 
    ... 
</issues> 

產生想要的,正確的結果

<div class="version"> 
    <a>6.14.0</a> 
    <div class="issues"> 
     <div class="Bug"> 
     <a href="issue1">issue1</a> 
     </div> 
     <div class="Improvement"> 
     <a href="issue3">issue3</a> 
     </div> 
    </div> 
</div> 
<div class="version"> 
    <a>6.13.0</a> 
    <div class="issues"> 
     <div class="Bug"> 
     <a href="issue1">issue1</a> 
     </div> 
     <div class="Feature"> 
     <a href="issue2">issue2</a> 
     </div> 
    </div> 
</div> 
<div class="version"> 
    <a>6.12.0</a> 
    <div class="issues"> 
     <div class="Bug"> 
     <a href="issue1">issue1</a> 
     </div> 
    </div> 
</div>