英文:
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:
<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>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论