如何使用XSLT 1.0应用条件子字符串?我使用xsltproc处理器。
Input.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>
当前Output.xml(问题是第二行的“类别”和“功能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>
所需的Output.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>
答案 0 :(得分:1)
您可以这样做:
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
中子字符串的顺序。