草庐IT

xml - 需要 XSL 文件将内部 xml 测试格式转换为 Junit 格式(jenkins 的 xUnit 插件)

coder 2024-06-27 原文

我正在尝试编写一个 XSL 以将我的 XML 转换为 jenkins 采用的 JUNIT 格式(见下文)

我的 xml 看起来像这样: (我有几个“类”,如“数据中心”或“网络”)

<tests>
   <Datacenters>             
        <test_name>Create NFS Data Center</test_name>
    <end_time>2011-06-13 01:22:55</end_time>
    <iter_num>1</iter_num>
    <start_time>2011-06-13 01:22:52</start_time>
    <status>Pass</status>
   </Datacenters>
   <Datacenters>             
    <test_name>Create NFS Data Center</test_name>
    <end_time>2011-06-13 01:22:55</end_time>
    <iter_num>1</iter_num>
    <start_time>2011-06-13 01:22:52</start_time>
    <status>Pass</status>
   </Datacenters>
   <Network>             
    <test_name>Network test 1</test_name>
    <end_time>2011-06-13 01:22:57</end_time>
    <iter_num>1</iter_num>
    <start_time>2011-06-13 01:22:52</start_time>
    <status>Pass</status>
   </Network>
   .....
 </tests>

我从 WebUI 插件中获取了一个 XSL 并尝试更改它,我已经完成了一半,但它仍然很棘手。这是我到目前为止所做的:

    <?xml version="1.0" encoding="UTF-8"?>
    <xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:fn="http://www.w3.org/2005/xpath-functions">

    <xsl:output method="xml" indent="yes" />    
    <xsl:template match="/">
    <testsuites>        

     <! -- need to change /Datacenters to something else so it will work on all nodes -->
      <xsl:variable name="buildName" select="//tests/Datacenters/test_name"/>   
      <xsl:variable name="numOfTests" select="count(//tests/Datacenters/iter_num)"/>
      <xsl:variable name="numOfFail" select="count(//tests/Datacenters/status [.= 'Fail'])" />  
      <xsl:variable name="numberSkip" select="count(//tests/Datacenters/status [.!='Pass' and .!='Fail'])" />   

 <testsuite name="QE AUTOMATION TESTS [DATACENTERS]"
                tests="{$numberOfTests}" time="0"
                failures="{$numberOfFailures}"  errors="0"
                skipped="{$numberSkipped}">

                <xsl:for-each select="//rest/Datacenters">
                    <xsl:variable name="testName" select="test_name"/>
                    <xsl:variable name="executionId" select="iter_num"/>
                    <xsl:variable name="start_time" select="fn:replace(start_time,' ','T')" />
                    <xsl:variable name="end_time" select="fn:replace(end_time,' ','T')"/>
                    <xsl:variable name="test_parameters" select="test_parameters"/>
                    <xsl:variable name="test_positive" select="test_positive"/>
                    <xsl:variable name="time_diff" select="xs:dateTime($end_time)-xs:dateTime($start_time)"/>
                    <xsl:variable name="duration_seconds" select="seconds-from-duration($time_diff)"/>
                    <xsl:variable name="duration_minutes" select="minutes-from-duration($time_diff)"/>  
                    <xsl:variable name="duration_hours" select="hours-from-duration($time_diff)"/>      
                    <xsl:variable name="outcome" select="status"/>  
                    <xsl:variable name="message" select="$buildName"/>  
                    <!--<xsl:variable name="className" select="Data"/>  -->
                                <testcase classname="Datacenters"
                                    name="{$testName}"
                                    time="{$duration_hours*3600 + $duration_minutes*60 + $duration_seconds }">

                                    <xsl:if test="contains($outcome, 'Fail')"> 
                                        <failure>
                                               test_parameters: <xsl:value-of select="$test_parameters" />
                                               test_positive: <xsl:value-of select="$test_positive" />
                                        </failure>
                                    </xsl:if>
                            </testcase>
                </xsl:for-each>

    </testsuite>
    </testsuites>
        </xsl:template>
    </xsl:stylesheet>

我想要实现的是这个 xml:

  <testsuites xmlns:fn="http://www.w3.org/2005/xpath-functions" xmlns:fo="http://www.w3.org/1999/XSL/Format" xmlns:xs="http://www.w3.org/2001/XMLSchema">
        <testsuite name="Datacenters" tests="2" time="4" failures="0" errors="0" skipped="0">
            <testcase classname="Datacenters" name="Create NFS Data Center" time="3"/>
            <testcase classname="Datacenters" name="Create ISCSI Data Center" time="1"/>
        </testsuite>
        <testsuite name="Network" tests="1" time="5" failures="0" errors="0" skipped="0">
            <testcase classname="Datacenters" name="Network test 1" time="5"/>
        </testsuite>
    </testsuites>

但我不知道如何迭代所有“类”,所以我希望它展示所有现有的 <tests> 的子项,而不是硬编码“数据中心”

最佳答案

我只想回答模板中的评论(您的代码可能需要调试和重构):

   <!-- need to change /Datacenters to something 
          else so it will work on all nodes -->

为了避免硬编码:

1) 像这样替换 XPaths:

//tests/Datacenters/test_name

使用 //tests/*/test_name

2)修正迭代(完全错误),应该是:

<xsl:for-each select="//tests/Datacenters">

你想要:

 <xsl:for-each select="//tests/*">

3) 最后,做替换:

 <testcase classname="Datacenters">

 <testcase classname="{local-name(.)}">

评论后编辑

我将使用简化的输出来回答,只是为了向您展示分组在 XSLT 2.0 中的工作方式。希望这个答案对你来说是可以接受的,你的实际模板在这里有点难以测试:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">

    <xsl:output indent="yes"/>

    <xsl:template match="tests">
        <testsuites>
            <xsl:for-each-group select="*" group-by="local-name()">
                <testsuite name="{current-grouping-key()}">
                    <xsl:for-each   select="current-group()">
                        <testcase classname="{current-grouping-key()}"/>
                    </xsl:for-each>
                </testsuite>
            </xsl:for-each-group>
        </testsuites>   
    </xsl:template>

</xsl:stylesheet>

应用于您问题中提供的输入样本,产生:

<?xml version="1.0" encoding="UTF-8"?>
<testsuites>
   <testsuite name="Datacenters">
      <testcase classname="Datacenters"/>
      <testcase classname="Datacenters"/>
   </testsuite>
   <testsuite name="Network">
      <testcase classname="Network"/>
   </testsuite>
</testsuites>

关于xml - 需要 XSL 文件将内部 xml 测试格式转换为 Junit 格式(jenkins 的 xUnit 插件),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6345737/

有关xml - 需要 XSL 文件将内部 xml 测试格式转换为 Junit 格式(jenkins 的 xUnit 插件)的更多相关文章

  1. ruby - 使用 RubyZip 生成 ZIP 文件时设置压缩级别 - 2

    我有一个Ruby程序,它使用rubyzip压缩XML文件的目录树。gem。我的问题是文件开始变得很重,我想提高压缩级别,因为压缩时间不是问题。我在rubyzipdocumentation中找不到一种为创建的ZIP文件指定压缩级别的方法。有人知道如何更改此设置吗?是否有另一个允许指定压缩级别的Ruby库? 最佳答案 这是我通过查看ruby​​zip内部创建的代码。level=Zlib::BEST_COMPRESSIONZip::ZipOutputStream.open(zip_file)do|zip|Dir.glob("**/*")d

  2. ruby - 其他文件中的 Rake 任务 - 2

    我试图在一个项目中使用rake,如果我把所有东西都放到Rakefile中,它会很大并且很难读取/找到东西,所以我试着将每个命名空间放在lib/rake中它自己的文件中,我添加了这个到我的rake文件的顶部:Dir['#{File.dirname(__FILE__)}/lib/rake/*.rake'].map{|f|requiref}它加载文件没问题,但没有任务。我现在只有一个.rake文件作为测试,名为“servers.rake”,它看起来像这样:namespace:serverdotask:testdoputs"test"endend所以当我运行rakeserver:testid时

  3. ruby-on-rails - 在 Rails 中将文件大小字符串转换为等效千字节 - 2

    我的目标是转换表单输入,例如“100兆字节”或“1GB”,并将其转换为我可以存储在数据库中的文件大小(以千字节为单位)。目前,我有这个:defquota_convert@regex=/([0-9]+)(.*)s/@sizes=%w{kilobytemegabytegigabyte}m=self.quota.match(@regex)if@sizes.include?m[2]eval("self.quota=#{m[1]}.#{m[2]}")endend这有效,但前提是输入是倍数(“gigabytes”,而不是“gigabyte”)并且由于使用了eval看起来疯狂不安全。所以,功能正常,

  4. ruby - 我需要将 Bundler 本身添加到 Gemfile 中吗? - 2

    当我使用Bundler时,是否需要在我的Gemfile中将其列为依赖项?毕竟,我的代码中有些地方需要它。例如,当我进行Bundler设置时:require"bundler/setup" 最佳答案 没有。您可以尝试,但首先您必须用鞋带将自己抬离地面。 关于ruby-我需要将Bundler本身添加到Gemfile中吗?,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/4758609/

  5. ruby-on-rails - Rails 3 中的多个路由文件 - 2

    Rails2.3可以选择随时使用RouteSet#add_configuration_file添加更多路由。是否可以在Rails3项目中做同样的事情? 最佳答案 在config/application.rb中:config.paths.config.routes在Rails3.2(也可能是Rails3.1)中,使用:config.paths["config/routes"] 关于ruby-on-rails-Rails3中的多个路由文件,我们在StackOverflow上找到一个类似的问题

  6. ruby - 将差异补丁应用于字符串/文件 - 2

    对于具有离线功能的智能手机应用程序,我正在为Xml文件创建单向文本同步。我希望我的服务器将增量/差异(例如GNU差异补丁)发送到目标设备。这是计划:Time=0Server:hasversion_1ofXmlfile(~800kiB)Client:hasversion_1ofXmlfile(~800kiB)Time=1Server:hasversion_1andversion_2ofXmlfile(each~800kiB)computesdeltaoftheseversions(=patch)(~10kiB)sendspatchtoClient(~10kiBtransferred)Cl

  7. ruby - 使用 ruby​​ 将 HTML 转换为纯文本并维护结构/格式 - 2

    我想将html转换为纯文本。不过,我不想只删除标签,我想智能地保留尽可能多的格式。为插入换行符标签,检测段落并格式化它们等。输入非常简单,通常是格式良好的html(不是整个文档,只是一堆内容,通常没有anchor或图像)。我可以将几个正则表达式放在一起,让我达到80%,但我认为可能有一些现有的解决方案更智能。 最佳答案 首先,不要尝试为此使用正则表达式。很有可能你会想出一个脆弱/脆弱的解决方案,它会随着HTML的变化而崩溃,或者很难管理和维护。您可以使用Nokogiri快速解析HTML并提取文本:require'nokogiri'h

  8. ruby - 如何将脚本文件的末尾读取为数据文件(Perl 或任何其他语言) - 2

    我正在寻找执行以下操作的正确语法(在Perl、Shell或Ruby中):#variabletoaccessthedatalinesappendedasafileEND_OF_SCRIPT_MARKERrawdatastartshereanditcontinues. 最佳答案 Perl用__DATA__做这个:#!/usr/bin/perlusestrict;usewarnings;while(){print;}__DATA__Texttoprintgoeshere 关于ruby-如何将脚

  9. ruby - 使用 Vim Rails,您可以创建一个新的迁移文件并一次性打开它吗? - 2

    使用带有Rails插件的vim,您可以创建一个迁移文件,然后一次性打开该文件吗?textmate也可以这样吗? 最佳答案 你可以使用rails.vim然后做类似的事情::Rgeneratemigratonadd_foo_to_bar插件将打开迁移生成的文件,这正是您想要的。我不能代表textmate。 关于ruby-使用VimRails,您可以创建一个新的迁移文件并一次性打开它吗?,我们在StackOverflow上找到一个类似的问题: https://sta

  10. ruby - 如何每月在 Heroku 运行一次 Scheduler 插件? - 2

    在选择我想要运行操作的频率时,唯一的选项是“每天”、“每小时”和“每10分钟”。谢谢!我想为我的Rails3.1应用程序运行调度程序。 最佳答案 这不是一个优雅的解决方案,但您可以安排它每天运行,并在实际开始工作之前检查日期是否为当月的第一天。 关于ruby-如何每月在Heroku运行一次Scheduler插件?,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/8692687/

随机推荐