将XML属性的值附加到同一文件中另一个属性的值。

huangapple go评论58阅读模式
英文:

Append value of XML attribute to value of Another attribute in same file

问题

以下是您的翻译内容:

给定以下XML文件

    <testsuites>
      <testsuite><testcase name="case 1" ><failure type="ERROR">Hello</failure></testcase></testsuite>
      <testsuite><testcase name="case 2" ><failure type="WARNING">Buddy</failure></testcase></testsuite>
    </testsuites>

我想将失败类型的值附加到测试用例的名称中。因此,上述XML中失败的值为“ERROR”,测试用例的名称为“Case 1”,结果名称将为“[ERROR] Case 1”。我想通过Shell脚本为所有测试用例执行此操作。

注意:同一文件中可能有多个具有不同失败类型的测试用例。

所需的目标XML文档如下

    <testsuites>
      <testsuite><testcase name="[ERROR] case 1" ><failure type="ERROR">Hello</failure></testcase></testsuite>
      <testsuite><testcase name="[WARNING] case 2" ><failure type="WARNING">Buddy</failure></testcase></testsuite>
    </testsuites>

到目前为止,我尝试过创建基于目标XML的XSLT文档,并使用该XSLT生成目标XML,但我目前还无法实现此目标。

我的XSLT

    <?xml version="1.0" encoding="UTF-8"?>
    <xsl:stylesheet version="1.0"   xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:template match="/">
    <testsuites>
    	<xsl:for-each select="/testsuites/testsuite">
    <testsuite>
     <xsl:for-each select="./testcase">
      <testcase>
       <xsl:variable name="name" select="./@name"/>
       <!-- The variable name can be used for further processing.  -->
       <xsl:attribute name="name"><xsl:value-of select="$name"/></xsl:attribute>
        <failure>
         <xsl:value-of   select="./failure"/>
         <xsl:variable name="type" select="./failure/@type"/>
         <!-- The variable type can be used for further processing.  -->
         <xsl:attribute name="type"><xsl:value-of select="$type"/></xsl:attribute>
        </failure>
       </testcase>
    </xsl:for-each>
    </testsuite>
    </xsl:for-each>
    </testsuites>
    </xsl:template>
    </xsl:stylesheet>

希望这对您有所帮助。

英文:

Given with the following xml file

<testsuites>
  <testsuite><testcase name="case 1" ><failure type="ERROR">Hello</failure></testcase></testsuite>
  <testsuite><testcase name="case 2" ><failure type="WARNING">Buddy</failure></testcase></testsuite>
</testsuites>

I want to append the failure type value to the name of test case. So value of failure in above xml is "ERROR" and name of the testcase is "Case 1" the resultant name would be "[ERROR] Case 1". I want to do it via shell scripts for all test cases.

Note: There can be multiple testcases with different failure type in same file.

Target XML document required is

<testsuites>
  <testsuite><testcase name="[ERROR] case 1" ><failure type="ERROR">Hello</failure></testcase></testsuite>
  <testsuite><testcase name="[WARNING] case 2" ><failure type="WARNING">Buddy</failure></testcase></testsuite>
</testsuites> 

What I have tried so far is creating XSLT doc based on target XML and use that XSLT to produce target XML, but I am not able to achieve this till now.

My XSLT

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"   xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<testsuites>
	<xsl:for-each select="/testsuites/testsuite">
<testsuite>
 <xsl:for-each select="./testcase">
  <testcase>
   <xsl:variable name="name" select="./@name"/>
   <!-- The variable name can be used for further processing.  -->
   <xsl:attribute name="name"><xsl:value-of select="$name"/></xsl:attribute>
    <failure>
     <xsl:value-of   select="./failure"/>
     <xsl:variable name="type" select="./failure/@type"/>
     <!-- The variable type can be used for further processing.  -->
     <xsl:attribute name="type"><xsl:value-of select="$type"/></xsl:attribute>
    </failure>
   </testcase>
</xsl:for-each>
</testsuite>
</xsl:for-each>
</testsuites>
</xsl:template>
</xsl:stylesheet>

答案1

得分: 1

I want to append the failure type value to the name of test case.

Should be quite simple:

<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="testcase/@name">
<xsl:attribute name="name">
xsl:text[</xsl:text>
<xsl:value-of select="../failure/@type" />
xsl:text] </xsl:text>
<xsl:value-of select="." />
</xsl:attribute>
</xsl:template>

</xsl:stylesheet>

英文:

> I want to append the failure type value to the name of test case.

Should be quite simple:

&lt;xsl:stylesheet version=&quot;1.0&quot; xmlns:xsl=&quot;http://www.w3.org/1999/XSL/Transform&quot;&gt;

&lt;xsl:template match=&quot;@*|node()&quot;&gt;
	&lt;xsl:copy&gt;
		&lt;xsl:apply-templates select=&quot;@*|node()&quot;/&gt;
	&lt;/xsl:copy&gt;
&lt;/xsl:template&gt;

&lt;xsl:template match=&quot;testcase/@name&quot;&gt;
	&lt;xsl:attribute name=&quot;name&quot;&gt;
		&lt;xsl:text&gt;[&lt;/xsl:text&gt;
		&lt;xsl:value-of select=&quot;../failure/@type&quot; /&gt;
		&lt;xsl:text&gt;] &lt;/xsl:text&gt;
		&lt;xsl:value-of select=&quot;.&quot; /&gt;
	&lt;/xsl:attribute&gt;
&lt;/xsl:template&gt;

&lt;/xsl:stylesheet&gt;

huangapple
  • 本文由 发表于 2023年6月8日 18:35:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/76430977.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定