xslt将节点值复制到其他节点变量

时间:2019-02-06 13:42:54

标签: xslt

我正在尝试编写xslt样式表,以使标准的junit报告可以通过jenkins读取。 我有此报告文件:

<?xml version="1.0" encoding="UTF-8"?>
<testsuites name="Test.postman.OpenWeather.01" tests="2" time="0.368">
  <testsuite name="getRandomCityById" id="a3fd8f91-a116-434b-9717-d40d61a03b59" timestamp="2019-02-06T11:28:55.732Z" classname="getRandomCityById" tests="1" failures="0" errors="0" time="0.252">
    <testcase name="Status code is 200" time="0.252"/>
  </testsuite>
  <testsuite name="getCityByName" id="4b65811e-9f86-48fb-904d-6eb9c8094b68" timestamp="2019-02-06T11:28:55.732Z" classname="getCityByName" tests="3" failures="0" errors="0" time="0.116">
    <testcase name="City Name matches Request'" time="0.116"/>
    <testcase name="Contains object: 'weather'" time="0.116"/>
    <testcase name="DOES NOT Contain string: 'snow' " time="0.116"/>
  </testsuite>
</testsuites>

但是我的输出必须是这样:

<?xml version="1.0" encoding="UTF-8"?>
<testsuites name="Test.postman.OpenWeather.01" tests="2" time="0.368">
  <testsuite name="getRandomCityById" id="a3fd8f91-a116-434b-9717-d40d61a03b59" timestamp="2019-02-06T11:28:55.732Z" classname="Test.postman.OpenWeather.01" tests="1" failures="0" errors="0" time="0.252">
    <testcase name="Status code is 200" time="0.252"/>
  </testsuite>
  <testsuite name="getCityByName" id="4b65811e-9f86-48fb-904d-6eb9c8094b68" timestamp="2019-02-06T11:28:55.732Z" classname="Test.postman.OpenWeather.01" tests="3" failures="0" errors="0" time="0.116">
    <testcase name="City Name matches Request'" time="0.116"/>
    <testcase name="Contains object: 'weather'" time="0.116"/>
    <testcase name="DOES NOT Contain string: 'snow' " time="0.116"/>
  </testsuite>
</testsuites>

在所有测试套件类名中,我需要测试套件名称(Test.postman.OpenWeather.01)

我无法编写xslt,有人可以帮助我吗? 预先感谢

1 个答案:

答案 0 :(得分:1)

如果给定的XML是您的输入文件,则结果可以在 XSLT 1.0 中实现,如下所示:

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="@*|node()">
    <xsl:copy>
        <xsl:apply-templates select="@*|node()" />
    </xsl:copy>
</xsl:template>

<xsl:template match="testsuite">
    <xsl:copy>
        <xsl:attribute name="classname">
          <xsl:value-of select="ancestor::node()/@name" />
       </xsl:attribute>
        <xsl:for-each select="@*">
            <xsl:if test="name() != 'classname'">
                <xsl:attribute name="{name()}"><xsl:value-of select="." /></xsl:attribute>
            </xsl:if>
        </xsl:for-each>
        <xsl:apply-templates select="node()" />
    </xsl:copy>
</xsl:template>