我有一个 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/