字符串值需要双引号

问题描述 投票:0回答:1

xslt 1.0 中是否有任何方法将“/”分隔的字符串转换为逗号分隔并用双引号引起来。

输入:

<?xml version="1.0" encoding="UTF-8" ?>
<process xmlns="http://xmlns.oracle.com/Application1/Project1/BPELProcess1">
   <input>input1/input2/input3/input4</input>
</process>

输出:

<?xml version = '1.0' encoding = 'UTF-8'?>
<ns0:processResponse xmlns:plnk="http://docs.oasis-open.org/wsbpel/2.0/plnktype" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:ns0="http://xmlns.oracle.com/Application1/Project1/BPELProcess1">
   <ns0:result>"input1","input2","input3","input4"</ns0:result>
</ns0:processResponse>
xslt-1.0
1个回答
0
投票

尝试这样:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:ns0="http://xmlns.oracle.com/Application1/Project1/BPELProcess1">
<xsl:output method="xml" indent="yes"/>

<xsl:template match="/ns0:process">
    <ns0:processResponse xmlns:plnk="http://docs.oasis-open.org/wsbpel/2.0/plnktype" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" >
        <ns0:result>
            <xsl:call-template name="convert">
                <xsl:with-param name="string" select="ns0:input"/>
            </xsl:call-template>
        </ns0:result>
    </ns0:processResponse>
</xsl:template>

<xsl:template name="convert">
    <xsl:param name="string"/>
    <xsl:if test="$string">
        <xsl:text>"</xsl:text>
        <xsl:value-of select="substring-before(concat($string, '/'), '/')" />
        <xsl:text>"</xsl:text>
    </xsl:if>
    <xsl:if test="contains($string, '/')">
        <xsl:text>,</xsl:text>
        <xsl:call-template name="convert">
            <xsl:with-param name="string" select="substring-after($string, '/')"/>
        </xsl:call-template>
    </xsl:if>
</xsl:template>

</xsl:stylesheet>

如果您的处理器支持 EXSLT str:tokenize 或 str:split 扩展功能,可能会更简单。

请注意,这不会转义现有引号,就像您对 CSV 所期望的那样。

© www.soinside.com 2019 - 2024. All rights reserved.