1

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

这是我的 xsl 代码

<?xml version="1.0" encoding="iso-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">


<xsl:key name="versions" match="issue" use="fix_versions"/>
<xsl:key name="type" match="issue" use="concat(fix_versions, '+', @type)"/>

<xsl:template match="issues">



  <xsl:for-each select="//issue[generate-id(.)=generate-id(key('versions', fix_versions)[1])]">

    <xsl:sort select="fix_versions" order="descending"/> 



        <xsl:variable name="version"><xsl:value-of select="fix_versions"/></xsl:variable>


                <xsl:if test="//release[@version = \$version]">
                    <div class="release">
                    <a href="" class="version"><xsl:value-of select="\$version"/></a>
                    <div class="version" style="display:none;">
                        <xsl:for-each select="key('versions', fix_versions)[generate-id() = generate-id(key('type', concat(fix_versions, '+', @type))[1])]">
                            <div class="types">
                                <a href="" class="type"><xsl:value-of select="@type"/>s</a>
                                <div class="type" style="display:none">
                    <xsl:for-each select="key('type', concat(fix_versions, '+', @type))">
                        <div style="border-bottom:1px solid grey;">
                        <a href="" class="title"><xsl:value-of select="title"/></a>
                        <div class="toggle" style="display:none;">
                            <xsl:value-of select="description"/>
                        </div>
                        </div>
                                </xsl:for-each>
                                </div>
                            </div>                      
                </xsl:for-each>

            </div>
            </div>
                </xsl:if>


  </xsl:for-each>
</xsl:template>

这就是我得到的

<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>

一切都好 - 唯一的问题是我只生成一个密钥,fix_versions但我有结构fix_versions/fix_version。因此,如果有类似的节点<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>,他将创建密钥6.14.06.13.06.12.0并为此版本创建一个容器 - 但我想要将此问题放入三个容器中6.14.06.13.0并且6.12.0。一般可以复制一个问题吗?

我尝试了类似<xsl:key name="versions" match="issue" use="fix_versions"/>然后使用的方法<xsl:for-each select="//issue[generate-id(.)=generate-id(key('versions', fix_versions)[1])]">,但他只是创建了一个大组,而不是将这个问题分类到所有这些组中。

任何的想法?

4

1 回答 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="kVersByVal" match="fix_version" use="."/>
 <xsl:key name="kBugTypeByVers" match="@type" use="../*/fix_version"/>

 <xsl:template match=
  "fix_version[generate-id()=generate-id(key('kVersByVal',.)[1])]">
  <div class="version">
   <a><xsl:value-of select="."/></a>
   <div class="issues">
     <xsl:for-each select="key('kBugTypeByVers',.)">
       <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-27T11:47:16.083 回答