XSLT-如何基于分隔符分割字符串并将值分配给多个属性

不安全的菜鸟

输入XML:

<testng-results>
<suite>
    <test>
        <class>
            <test-method status="PASS" description="Test_ID:123,Test_Name:Test ABC,Product:Product ABC"></test-method>
            <test-method status="PASS" description="Test_ID:456,Test_Name:Test XYZ,Product:Product XYZ"></test-method>
        </class>
    </test>
</suite>
</testng-results>

我当前的XSLT:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
   <xsl:template match="/">
  <Suite>
     <xsl:for-each select="testng-results/suite/test/class/test-method">
        <test>
           <xsl:attribute name="status">
              <xsl:value-of select="@status" />
           </xsl:attribute>
           <xsl:attribute name="Test_ID">
              <xsl:value-of select="" />
           </xsl:attribute>
           <xsl:attribute name="Test_Name">
              <xsl:value-of select="" />
           </xsl:attribute>
           <xsl:attribute name="Product">
              <xsl:value-of select="" />
           </xsl:attribute>
        </test>
     </xsl:for-each>
  </Suite>

预期的输出.XML:

<?xml version="1.0" encoding="UTF-8"?>
<Suite>
   <test status="PASS" Test_ID="123" Test_Name="Test ABC" Product="Product ABC" />
   <test status="PASS" Test_ID="456" Test_Name="Test XYZ" Product="Product XYZ" />
</Suite>

我必须从'description'值中获取字符串,并拆分这些值以生成输出xml。

我已经读到XSLT 2.0更好地配备了字符串标记化功能。但是,我只能使用XSLT 1.0。

michael.hor257k

如果您的处理器支持EXSLTstr:tokenize()扩展功能,则可以执行以下操作:

XSLT 1.0 + EXSLT

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:str="http://exslt.org/strings"
extension-element-prefixes="str">
<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}" >
                <xsl:for-each select="str:tokenize(@description, ',')">
                    <xsl:attribute name="{substring-before(., ':')}">
                        <xsl:value-of select="substring-after(., ':')" />
                    </xsl:attribute>
                </xsl:for-each>
            </test>
        </xsl:for-each>
    </Suite>
</xsl:template>

</xsl:stylesheet>

否则,您可以使用字符串函数提取3个必需值中的每一个:

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">
            <test 
                status="{@status}" 
                Test_ID="{substring-before(substring-after(@description, 'Test_ID:'), ',') }" 
                Test_Name="{substring-before(substring-after(@description, 'Test_Name:'), ',') }" 
                Product="{substring-after(@description, 'Product:')}"/>
        </xsl:for-each>
    </Suite>
</xsl:template>

</xsl:stylesheet>

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章