muenchian分组在第二级

时间:2012-08-27 14:37:25

标签: xml xslt xslt-1.0

这是我的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>

这是我的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>

如您所见,问题未分组在名为“错误”,“改进”或“功能”的容器中 - 但这就是我想要的。 我试过这样的东西     

但无论我尝试过什么 - 都没有用。我真的没有计划如何将它们分组。

2 个答案:

答案 0 :(得分:0)

我认为你必须让你的密钥更具体。在XSLT中定义密钥时,始终为整个XML文档定义密钥,而不是为部分定义。在定义键时,您必须concat()多个值。

Plesae看看这个主题:Muenchian Grouping - group within a node, not within the entire document

祝你好运, 彼得

答案 1 :(得分: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>