dev*_*dev 2 xml string xslt soap xslt-2.0
我有一个XSLT,它将从SOAP Response xml转换为Java对象.
在soap响应中,有一个节点叫做urn:Statues逗号(,)分隔的字符串.
我想拆分urn:Statuesnode 的值并替换一些值,然后再将它们连接成一个用逗号(,)分隔的字符串.
肥皂反应:
<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>
Run Code Online (Sandbox Code Playgroud)
我的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>
Run Code Online (Sandbox Code Playgroud)
这里的问题是,当值设置为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>
Run Code Online (Sandbox Code Playgroud)
预期结果:
<?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>
Run Code Online (Sandbox Code Playgroud)
有人可以告诉如何在结果标记中用逗号(,)分隔字符串<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>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
326 次 |
| 最近记录: |