在 XSLT 2.0 中拆分、替换和连接字符串

Split, replace and concatenate a string in XSLT 2.0

我有一个 XSLT,它将从 SOAP 响应 xml 转换为 Java 对象。

在 soap 响应中有一个名为 urn:Statues 的节点,它有逗号 (,) 分隔的字符串。

我想拆分 urn:Statues 节点的值并替换一些值,然后再次将它们连接成一个用逗号 (,) 分隔的字符串。

Soap 响应:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" 
    xmlns:urn="urn:HPD_Incident_Query_WS">
    <soapenv:Header/>
    <soapenv:Body> 
        <urn:Incident_Query_ServiceResponse>
            <urn:ErrorCode>0</urn:ErrorCode>
            <urn:Incidents_Number>INC80167842,INC77752907,INC20954581,INC20954533</urn:Incidents_Number>
            <urn:Statuses>CLOSED,CANCELLED,En Curso,Cerrado</urn:Statuses>
        </urn:Incident_Query_ServiceResponse>
    </soapenv:Body>
</soapenv:Envelope>

我的 XSLT:

<?xml version='1.0' encoding='UTF-8'?>
<xsl:stylesheet version='2.0' xmlns:xsl='http://www.w3.org/1999/XSL/Transform' xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
    xmlns:urn="urn:HPD_Incident_Query_WS">
    <xsl:output method='xml' indent='yes' />

    <xsl:template match="urn:Incident_Query_ServiceResponse" name="split">
        <xsl:for-each select="tokenize(./urn:Statuses,',')">
            <xsl:if test="(normalize-space(.) eq 'Cerrado')">
                <xsl:value-of select="replace(normalize-space(.), 'Cerrado', 'CLOSED')"/>  
            </xsl:if>
            <xsl:if test="(normalize-space(.) eq 'CANCELLED')">
                <xsl:value-of select="replace(normalize-space(.), 'CANCELLED', 'CLOSED')"/>
            </xsl:if>
            <xsl:if test="(normalize-space(.) eq 'En Curso')">
                <xsl:value-of select="replace(normalize-space(.), 'En Curso', 'OPENACTIVE')"/>
            </xsl:if>
            <xsl:if test="(normalize-space(.) eq 'Pendiente')">
                <xsl:value-of select="replace(normalize-space(.), 'Pendiente', 'CLOSED.PENDING')"/>
            </xsl:if>
        </xsl:for-each>
    </xsl:template>

    <xsl:template match='/'>
        <getTicketInfoResponse>
            <responseCode>
                <xsl:value-of select='/soapenv:Envelope/soapenv:Body/urn:Incident_Query_ServiceResponse/urn:ResponseCode' />
            </responseCode>
            <responseMessage>
                <xsl:value-of select='/soapenv:Envelope/soapenv:Body/urn:Incident_Query_ServiceResponse/urn:ResponseMsg' />
            </responseMessage>
            <errorCode>
                <xsl:value-of select='/soapenv:Envelope/soapenv:Body/urn:Incident_Query_ServiceResponse/urn:ErrorCode' />
            </errorCode>
            <errorMsg>
                <xsl:value-of select='/soapenv:Envelope/soapenv:Body/urn:Incident_Query_ServiceResponse/urn:ErrorMsg' />
            </errorMsg>
            <ticketDetails>
                <troubleTicketState>
                    <xsl:apply-templates select="/soapenv:Envelope/soapenv:Body"/>
                </troubleTicketState>
                <troubleTicketId>
                    <xsl:value-of select='/soapenv:Envelope/soapenv:Body/urn:Incident_Query_ServiceResponse/urn:Incidents_Number' />
                </troubleTicketId>
            </ticketDetails>
        </getTicketInfoResponse>
    </xsl:template>
</xsl:stylesheet>

这里的问题是,当值设置为 urn:Statuses 时,值没有用逗号 (,) 分隔。

当前转换后结果:

<?xml version="1.0" encoding="UTF-8"?>
<getTicketInfoResponse xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
   xmlns:urn="urn:HPD_Incident_Query_WS">
   <responseCode/>
   <responseMessage/>
   <errorCode>0</errorCode>
   <errorMsg/>
   <ticketDetails>
      <troubleTicketState> CLOSEDCLOSEDOPENACTIVECLOSED</troubleTicketState>
      <troubleTicketId>INC80167842,INC77752907,INC20954581,INC20954533</troubleTicketId>
   </ticketDetails>
</getTicketInfoResponse>

预期结果:

<?xml version="1.0" encoding="UTF-8"?>
<getTicketInfoResponse xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
   xmlns:urn="urn:HPD_Incident_Query_WS">
   <responseCode/>
   <responseMessage/>
   <errorCode>0</errorCode>
   <errorMsg/>
   <ticketDetails>
      <troubleTicketState>CLOSED,CLOSED,OPENACTIVE,CLOSED</troubleTicketState>
      <troubleTicketId>INC80167842,INC77752907,INC20954581,INC20954533</troubleTicketId>
   </ticketDetails>
</getTicketInfoResponse>

谁能告诉我如何在结果标签中用逗号 (,) 分隔字符串 <troubleTicketState>

有更好的方法吗?

提前致谢。

你不能简单地做:

XSLT 2.0

<xsl:stylesheet version="2.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
xmlns:soapenv='http://schemas.xmlsoap.org/soap/envelope/'
xmlns:urn='urn:HPD_Incident_Query_WS'>

<xsl:output method="xml" indent="yes" />

<xsl:template match="/soapenv:Envelope">
    <getTicketInfoResponse>
        <responseCode>
            <xsl:value-of select="soapenv:Body/urn:Incident_Query_ServiceResponse/urn:ResponseCode" />
        </responseCode>
        <responseMessage>
            <xsl:value-of select="soapenv:Body/urn:Incident_Query_ServiceResponse/urn:ResponseMsg" />
        </responseMessage>
        <errorCode>
            <xsl:value-of select="soapenv:Body/urn:Incident_Query_ServiceResponse/urn:ErrorCode" />
        </errorCode>
        <errorMsg>
            <xsl:value-of select="soapenv:Body/urn:Incident_Query_ServiceResponse/urn:ErrorMsg" />
        </errorMsg>
        <ticketDetails>
            <troubleTicketState>
                <xsl:apply-templates select="soapenv:Body/urn:Incident_Query_ServiceResponse/urn:Statuses" />
            </troubleTicketState>
            <troubleTicketId>
                <xsl:value-of select="soapenv:Body/urn:Incident_Query_ServiceResponse/urn:Incidents_Number" />
            </troubleTicketId>
        </ticketDetails>
    </getTicketInfoResponse>
</xsl:template>

<xsl:template match="urn:Statuses">
    <xsl:value-of select="replace(replace(replace(replace(., 'Cerrado', 'CLOSED'), 'CANCELLED', 'CLOSED'), 'En Curso', 'OPENACTIVE'), 'Pendiente', 'CLOSED.PENDING')"/> 
</xsl:template>

</xsl:stylesheet>