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

标签 xml string xslt soap xslt-2.0

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

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

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

SOAP react :

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

关于xml - 在 XSLT 2.0 中拆分、替换和连接字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36184919/

相关文章:

android - 显示 fragment 的 Activity 按钮

python - 复制字符串 - Python

mysql - SQL - 查询字符串是否包含 Column 中的部分值

javascript - AngularJS 不在 ng-click 和 ng-show 中解释 XSL

xslt - 选择当前元素和下一个当前元素之间的所有元素

Android ConstraintLayout跳到屏幕右侧

javascript - 使用 Javascript 或 jQuery - 查找具有正确属性日期范围的节点

excel - 有没有一种简单的方法来检查字符串是否以字母开头(任何 4 个字母)?

xml - 使用变量选择元素

javascript - 将 xml 文件数据保存在 javascript 变量中