XML : Split, replace and concatenate a string in XSLT 2.0

I have an XSLT which will be transformed from a SOAP Response xml to a Java object.

In the soap response there is node called urn:Statues which has comma(,) separated string.

I want to split the value of urn:Statues node and replace some of the value and again concatenate those into a string with comma(,) separated.

Soap Response:

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

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

The issue here is, when the value is set to urn:Statuses the values are not separated by comma(,).

Current Result after transformation:

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

Expected Result:

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

Can someone tell how to separate the string by comma(,) in the resulting tag <troubleTicketState>.

Is there any better way to do this ?

Thanks in advance.

No comments:

Post a Comment