首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用xslt按jira编号对subversion xml日志条目进行分组

使用xslt按jira编号对subversion xml日志条目进行分组
EN

Stack Overflow用户
提问于 2012-05-02 20:14:43
回答 1查看 407关注 0票数 3

让XML Subversion提交日志:

代码语言:javascript
复制
<?xml version="1.0"?>
<log>
<logentry
   revision="1236">
<author>me</author>
<date>2011-11-25T08:28:57.024571Z</date>
<msg>JIRA-5678: commit message</msg>
</logentry>
<logentry
   revision="1235">
<author>me</author>
<date>2011-11-25T08:28:47.024571Z</date>
<msg>JIRA-5678 - commit2 message</msg>
</logentry>
<logentry
   revision="1234">
<author>me</author>
<date>2011-11-25T08:28:37.024571Z</date>
<msg>JIRA-5678 commit3 message</msg>
</logentry>
</log>

我想生成一个按JIRA编号分组的报告,如下所示:

代码语言:javascript
复制
<table>
<tr><th>revision</th><th>comment</th></tr>
<tr><td colspan=2>JIRA-5678</td></tr>
<tr><td>r1236</td><td>JIRA-5678: commit message</td></tr>
<tr><td>r1235</td><td>JIRA-5678 - commit2 message</td></tr>
<tr><td>r1234</td><td>JIRA-5678 commit3 message</td></tr>
</table>

提取JIRA编号并将其输出到单独列的转换:

代码语言:javascript
复制
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:fn="http://www.w3.org/2005/xpath-functions">

<xsl:template match="/">
  <table summary="JIRAs">
    <tr>
      <th>JIRA#</th>
      <th>revision</th>
      <th>message</th>
    </tr>

  <xsl:for-each select="log/logentry[starts-with(msg,'JIRA-')]">
    <tr>
      <td><xsl:value-of select="fn:replace(msg,'^(JIRA-\d+).*$', '$1')" /></td>
      <td><xsl:value-of select="@revision" /></td>
      <td><xsl:value-of select="msg" /></td>
    </tr>
  </xsl:for-each>

  </table>
</xsl:template>

</xsl:stylesheet>

做这种分组的XSLT代码是什么?

更新:我最终在XML DOM的顶部编写了一个python脚本。如果你需要的话,请留下评论。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-05-02 21:36:57

I.这个XSLT1.0转换

代码语言:javascript
复制
<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="kEntryByNum" match="logentry" use="substring-before(msg, ':')"/>

 <xsl:template match="/*">
     <table>
       <tr><th>revision</th><th>comment</th></tr>

       <xsl:apply-templates select=
        "logentry
           [generate-id()
           =
            generate-id(key('kEntryByNum', substring-before(msg, ':'))[1])
           ]
      "/>
     </table>
 </xsl:template>

 <xsl:template match="logentry">
        <tr><td colspan="2"><xsl:value-of select="substring-before(msg, ':')"/></td></tr>
        <xsl:for-each select="key('kEntryByNum', substring-before(msg, ':'))">
            <tr>
                <td><xsl:value-of select="@revision"/></td>
                <td><xsl:value-of select="msg"/></td>
            </tr>
        </xsl:for-each>
 </xsl:template>
</xsl:stylesheet>

在以下XML文档上应用时的

代码语言:javascript
复制
<log>
    <logentry revision="1236">
        <author>me</author>
        <date>2011-11-25T08:28:57.024571Z</date>
        <msg>JIRA-5678: commit message</msg>
    </logentry>
    <logentry revision="1237">
        <author>me</author>
        <date>2011-11-25T08:28:57.024571Z</date>
        <msg>JIRA-5679: commit message</msg>
    </logentry>
    <logentry revision="1238">
        <author>me</author>
        <date>2011-11-25T08:28:57.024571Z</date>
        <msg>JIRA-5679: commit message</msg>
    </logentry>
    <logentry revision="1235">
        <author>me</author>
        <date>2011-11-25T08:28:47.024571Z</date>
        <msg>JIRA-5678: - commit2 message</msg>
    </logentry>
    <logentry revision="1234">
        <author>me</author>
        <date>2011-11-25T08:28:37.024571Z</date>
        <msg>JIRA-5678: commit3 message</msg>
    </logentry>
</log>

生成所需的、正确的结果

代码语言:javascript
复制
<table>
   <tr>
      <th>revision</th>
      <th>comment</th>
   </tr>
   <tr>
      <td colspan="2">JIRA-5678</td>
   </tr>
   <tr>
      <td>1236</td>
      <td>JIRA-5678: commit message</td>
   </tr>
   <tr>
      <td>1235</td>
      <td>JIRA-5678: - commit2 message</td>
   </tr>
   <tr>
      <td>1234</td>
      <td>JIRA-5678: commit3 message</td>
   </tr>
   <tr>
      <td colspan="2">JIRA-5679</td>
   </tr>
   <tr>
      <td>1237</td>
      <td>JIRA-5679: commit message</td>
   </tr>
   <tr>
      <td>1238</td>
      <td>JIRA-5679: commit message</td>
   </tr>
</table>

II. XSLT2.0解决方案

代码语言:javascript
复制
<xsl:stylesheet version="2.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="kEntryByNum" match="logentry" use="substring-before(msg, ':')"/>

 <xsl:template match="/*">
     <table>
       <tr><th>revision</th><th>comment</th></tr>

       <xsl:for-each-group select="logentry" group-by="substring-before(msg, ':')">
         <xsl:apply-templates select="."/>
       </xsl:for-each-group>
     </table>
 </xsl:template>

 <xsl:template match="logentry">
        <tr><td colspan="2"><xsl:value-of select="substring-before(msg, ':')"/></td></tr>
        <xsl:for-each select="current-group()">
            <tr>
                <td><xsl:value-of select="@revision"/></td>
                <td><xsl:value-of select="msg"/></td>
            </tr>
        </xsl:for-each>
 </xsl:template>
</xsl:stylesheet>
票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/10413537

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档