XSLT 1.0 (xsltproc) - How to replace last occurence of a character?

时间:2019-04-16 22:14:51

标签: xslt xpath xslt-1.0

I'm trying to replace the last occurrence of '.' character with '/' character in using XSLT (xslt 1.0 xsltproc). How can I do that?

Input.xml

<testng-results>
<suite>
<test>
<class name="system.apps.webdriver.tests.crm.mot.CreateTerritoryProposalTest">
<test-method status="PASS" started-at="2019-02-07T18:24:47Z" name="initTest">
</test-method>
<test-method status="FAIL" started-at="2019-02-07T18:24:47Z" name="ActListsForContactShowsContactRelatedTasksTest">
</test-method>
</class>
</test>
</suite>
</testng-results>

Current XSL:

<?xml version="1.0" encoding="UTF-8"?>

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                version="1.0">

    <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>

    <xsl:template match="/">
        <Suite>
            <xsl:for-each select="testng-results/suite/test/class/test-method">
                        <Test>
                            <Result_Path> <xsl:value-of select="concat('testngreports/', ../@name, '/', @name)"/> </Result_Path>
                        </Test>
            </xsl:for-each>
        </Suite>
    </xsl:template>
</xsl:stylesheet>

Expected Result XML:

<?xml version="1.0" encoding="UTF-8"?>
<Suite>
  <Test>
    <Result_Path>testngreports/system.apps.webdriver.tests.crm.mot/CreateTerritoryProposalTest/initTest</Result_Path>
  </Test>
  <Test>
    <Result_Path>testngreports/system.apps.webdriver.tests.crm.mot/CreateTerritoryProposalTest/ActListsForContactShowsContactRelatedTasksTest</Result_Path>
  </Test>
</Suite>

Current Output XML:


<?xml version="1.0" encoding="UTF-8"?>
<Suite>
  <Test>
    <Result_Path>testngreports/system.apps.webdriver.tests.crm.mot.CreateTerritoryProposalTest/initTest</Result_Path>
  </Test>
  <Test>
    <Result_Path>testngreports/system.apps.webdriver.tests.crm.mot.CreateTerritoryProposalTest/ActListsForContactShowsContactRelatedTasksTest</Result_Path>
  </Test>
</Suite>

Summary:

Current Output:

testngreports/system.apps.webdriver.tests.crm.mot.CreateTerritoryProposalTest/initTest

Expected Output:

testngreports/system.apps.webdriver.tests.crm.mot/CreateTerritoryProposalTest/initTest

Last '.' character should be replaced with '/'

2 个答案:

答案 0 :(得分:1)

Probably needs recursion. Write a recursive template T: if the supplied string $S doesn't contain ".", then copy it unchanged; otherwise let $L be substring-before($S, ".") and $R be substring-after($S, "."). If $R contains "." then return the concatenation of $L, ".", and the result of a recursive call on T supplying $R as the parameter; otherwise return the concatenation of $L, "/", and $R.

答案 1 :(得分:1)

With xsltproc - i.e. the libxslt processor - you can take advantage of the EXSLT str:tokenize() extension function and do:

<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">
            <xsl:variable name="class-name">
                <xsl:for-each select="str:tokenize(@name, '.')">
                    <xsl:value-of select="."/> 
                    <xsl:choose>
                        <xsl:when test="position() >= last() - 1 ">/</xsl:when>
                        <xsl:otherwise>.</xsl:otherwise>
                    </xsl:choose>
                </xsl:for-each>
            </xsl:variable>
            <xsl:for-each select="test-method">
                <Test>
                    <Result_Path>
                        <xsl:text>testngreports/</xsl:text>
                        <xsl:value-of select="$class-name"/> 
                        <xsl:value-of select="@name"/> 
                    </Result_Path>
                </Test>
            </xsl:for-each>
        </xsl:for-each>
    </Suite>
</xsl:template>

</xsl:stylesheet>