如何使用 XSLT 1.0 应用条件子字符串?我使用 xsltproc 处理器。
输入.xml
<testng-results>
<suite>
<test>
<class>
<test-method status="PASS" description="Test_ID:123,Test_Name:Test ABC,Category:Category ABC, Feature_ID:12345"></test-method>
<test-method status="PASS" description="Test_ID:456,Test_Name:Test XYZ,Category:Category XYZ"></test-method>
</class>
</test>
</suite>
</testng-results>
我当前的 XSL:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="/testng-results">
<Suite>
<xsl:for-each select="suite/test/class/test-method">
<test
status="{@status}"
Test_ID="{substring-before(substring-after(@description, 'Test_ID:'), ',') }"
Test_Name="{substring-before(substring-after(@description, 'Test_Name:'), ',') }"
Category="{substring-before(substring-after(@description, 'Category:'), ',') }"
Feature_ID="{substring-after(@description, 'Feature_ID:')}"/>
</xsl:for-each>
</Suite>
</xsl:template>
</xsl:stylesheet>
电流输出.xml(问题是第二行的"类别"和"Feature_ID"为空(:
<?xml version="1.0" encoding="UTF-8"?>
<Suite>
<test status="PASS" Test_ID="123" Test_Name="Test ABC" Category="Category ABC" Feature_ID="12345"/>
<test status="PASS" Test_ID="456" Test_Name="Test XYZ" Category="" Feature_ID=""/>
</Suite>
期望的输出.xml
<?xml version="1.0" encoding="UTF-8"?>
<Suite>
<test status="PASS" Test_ID="123" Test_Name="Test ABC" Category="Category ABC" Feature_ID="12345"/>
<test status="PASS" Test_ID="456" Test_Name="Test XYZ" Category="Category XYZ" Feature_ID=""/>
</Suite>
你可以做:
XSLT 1.0
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="/testng-results">
<Suite>
<xsl:for-each select="suite/test/class/test-method">
<xsl:variable name="description" select="concat(@description, ',')" />
<test
status="{@status}"
Test_ID="{substring-before(substring-after($description, 'Test_ID:'), ',')}"
Test_Name="{substring-before(substring-after($description, 'Test_Name:'), ',')}"
Category="{substring-before(substring-after($description, 'Category:'), ',')}"
Feature_ID="{substring-before(substring-after($description, 'Feature_ID:'), ',')}"/>
</xsl:for-each>
</Suite>
</xsl:template>
</xsl:stylesheet>
这样,您就不依赖于description
中子字符串的顺序。