【问题标题】:XSLT 1.0 (xsltproc) - How to replace last occurence of a character?XSLT 1.0 (xsltproc) - 如何替换最后出现的字符?
【发布时间】:2023-03-18 18:50:01
【问题描述】:

我正在尝试替换最后出现的 '.'使用 XSLT (xslt 1.0 xsltproc) 时带有“/”字符的字符。我该怎么做?

输入.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>

当前 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>

预期结果 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>

当前输出 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>

总结:

电流输出:

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

预期输出:

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

最后一个'.'字符应替换为'/'

【问题讨论】:

    标签: xslt xpath xslt-1.0


    【解决方案1】:

    使用 xsltproc - 即 libxslt 处理器 - 您可以利用 EXSLT str:tokenize() 扩展功能并执行以下操作:

    <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>
    

    【讨论】:

      【解决方案2】:

      可能需要递归。编写递归模板 T:如果提供的字符串 $S 不包含“.”,则原样复制它;否则令 $L 为 substring-before($S, ".") 并且 $R 为 substring-after($S, ".")。如果 $R 包含“。”然后返回 $L、"." 的连接,以及对 T 提供 $R 作为参数的递归调用的结果;否则返回 $L、“/”和 $R 的串联。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-10-10
        • 1970-01-01
        • 2011-11-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-04-06
        相关资源
        最近更新 更多