gpt4 book ai didi

java - 无法使用 XSLT 获取特定的整个 XML 标记

转载 作者:行者123 更新时间:2023-12-02 10:29:47 25 4
gpt4 key购买 nike

我有以下示例 XML 文件

示例 XML:

  <?xml version="1.0" encoding="UTF-8"?>
<testng-results skipped="0" failed="0" total="10" passed="10">
<class name="com.transfermoney.Transfer">
<test-method status="PASS" name="setParameter" is-config="true" duration-ms="4"
started-at="2018-08-16T21:43:38Z" finished-at="2018-08-16T21:43:38Z">
<params>
<param index="0">
<value>
<![CDATA[org.testng.TestRunner@31c2affc]]>
</value>
</param>
</params>
<reporter-output>
</reporter-output>
</test-method> <!-- setParameter -->
</class>
<class name="com.transfermoney.Transfer">
<test-method status="FAIL" name="setSettlementFlag" is-config="true" duration-ms="5"
started-at="2018-08-16T21:44:55Z" finished-at="2018-08-16T21:44:55Z">
<reporter-output>
<line>
<![CDATA[runSettlement Value Set :false]]>
</line>
</reporter-output>
</test-method> setSettlementFlag
</class>
</testng-results>

我只想根据状态 PASS 从上面的 XML 文件中获取以下标签(我不想获取 <?XML version<testng-results>class 标签,这些标签应该被忽略)。

预期输出:

   <test-method status="PASS" name="setParameter" is-config="true" duration-ms="4"
started-at="2018-08-16T21:43:38Z" finished-at="2018-08-16T21:43:38Z">
<params>
<param index="0">
<value>
<![CDATA[org.testng.TestRunner@31c2affc]]>
</value>
</param>
</params>
<reporter-output>
</reporter-output>
</test-method>

我只是使用下面的 XSLT 从示例 XML 文件中获取上述输出,但它不起作用。它返回了所有标签,但我只想要上面的输出,而不是其他任何内容。

XSLT:

     <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output indent="yes" method="xml"/>"
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()" />
</xsl:copy>
</xsl:template>
<xsl:template match="class"/>
<xsl:copy>
<xsl:apply-templates select="@*|node()" />
<xsl:for-each select="test-method[@status='PASS']">
<xsl:copy>
<xsl:apply-templates select="@*|node()" />
</xsl:copy>
</xsl:for-each>
</xsl:copy>
</xsl:stylesheet>

还使用下面的 java 代码来运行 XSLT 和示例 XML 文件

代码:

    String XML = fetchDataFrmXML(".//Test//testng-results_2.xml");
Transformer t = TransformerFactory.newInstance().newTransformer(new StreamSource(new StringReader(XSL)));
t.transform(new StreamSource(new StringReader(XML)), new StreamResult(new File(".//Test//Sample1.xml")));

这是示例有效负载。但实际的有效负载有多个状态为“PASS”和“Failed”的节点。我只是对获取上述输出格式的 PASS 节点感兴趣。

任何线索......

最佳答案

只需执行以下操作即可非常简单地获得您显示的结果:

XSLT 1.0

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" omit-xml-declaration="yes" version="1.0" encoding="utf-8" indent="yes"/>
<xsl:strip-space elements="*"/>

<xsl:template match="/testng-results">
<xsl:copy-of select="class/test-method[@status='PASS']" />
</xsl:template>

</xsl:stylesheet>
<小时/>

但是,如果有多个 test-methodstatus“PASS”,这将导致 XML 片段没有单个根元素。所以你可能最好这样做:

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" omit-xml-declaration="yes" version="1.0" encoding="utf-8" indent="yes"/>
<xsl:strip-space elements="*"/>

<xsl:template match="/testng-results">
<root>
<xsl:copy-of select="class/test-method[@status='PASS']" />
</root>
</xsl:template>

</xsl:stylesheet>

关于java - 无法使用 XSLT 获取特定的整个 XML 标记,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53676826/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com