2

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

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

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

4

2 回答 2

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>
于 2012-08-28T12:39:05.227 回答
0

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

请看一下这个线程:Muenchian Grouping - group within a node, not within the entire document

于 2012-08-27T16:16:11.683 回答