dev
dev

Reputation: 1355

How to pick specific part from a string in XSLT2.0

I am transforming a SOAP response using XSLT. I read the response xml and try to transform it in my XSLT.

I have string which contains the errorCode and errorMessage and I need to match the combination based on a regex and pick a specific part (i.e., errorCode) from it.

Input String: ERROR (7000): ; Mandatory field missing : DESCRIPTION mandatory

Now, I want to check if the incoming string matches my input string pattern and pick only the part inside parenthesis (i.e., errorCode=7000)

SOAP RESPONSE XML :

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
   <SOAP-ENV:Header xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"/>
   <soap:Body>
      <outputMapping1 xmlns="urn:GC_CreateTroubleTicketByValue">
         <Incident_Number>ERROR (7000): ; Mandatory field missing : DISCRIPTION mandatory</Incident_Number>
      </outputMapping1>
   </soap:Body>
</soap:Envelope>

XSLT :

<?xml version='1.0' encoding='UTF-8'?>
<xsl:stylesheet version='2.0'
    xmlns:xsl='http://www.w3.org/1999/XSL/Transform' 
    xmlns:fn="http://www.w3.org/2005/xpath-functions" 
    xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" 
    xmlns:urn="urn:GC_CreateTroubleTicketByValue">
    <xsl:output method='xml' indent='yes' />
    <xsl:variable name="ticketId" select="/soap:Envelope/soap:Body/urn:outputMapping1/urn:Incident_Number"></xsl:variable>
    <xsl:template match='/'>
        <createTicketResponse>
            <responseCode>
                <xsl:choose>
                    <xsl:when test="matches($ticketId, 'ERROR\s\(([0-9]+)\):\s;\s(.*)')">
                        <xsl:value-of select='fn:replace($ticketId, "ERROR\s\(([0-9]+)\):\s;\s(.*)", "([0-9]+)")' />
                    </xsl:when> 
                    <xsl:otherwise>1</xsl:otherwise>
                </xsl:choose>
            </responseCode>
            <responseMsg>
                <xsl:choose>
                    <xsl:when test='fn:matches($ticketId,"ERROR\s\(([0-9]+)\):\s;\s(.*)")'>
                        <xsl:value-of select='fn:replace($ticketId, "ERROR\s\(([0-9]+)\):\s;\s(.*)", "(.*)")' />
                    </xsl:when> 
                    <xsl:otherwise>FAILURE</xsl:otherwise>
                </xsl:choose>
            </responseMsg>
        </createTicketResponse>
    </xsl:template>
</xsl:stylesheet>

In the current XSLT it matches the regex but I am unable to pick the errorCode out of it.

Can someone point me into right direction in order to pick the only the errorCode out of the tag <Incident_Number>.

Expected Output:

<?xml version="1.0" encoding="UTF-8"?>
<createTicketResponse xmlns:fn="http://www.w3.org/2005/xpath-functions"
                      xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
                      xmlns:urn="urn:GC_CreateTroubleTicketByValue">
   <responseCode>7000</responseCode>
   <responseMsg>Mandatory field missing : DISCRIPTION mandatory</responseMsg>
</createTicketResponse>

Upvotes: 2

Views: 69

Answers (2)

Martin Honnen
Martin Honnen

Reputation: 167706

You can use <xsl:value-of select='replace($ticketId, "ERROR\s\(([0-9]+)\):\s;\s(.*)", "$1")' />.

Upvotes: 4

fafl
fafl

Reputation: 7385

Try analyze-string:

<createTicketResponse>
    <xsl:analyze-string select="$ticketId"
        regex="ERROR\s\(([0-9]+)\):\s;\s(.*)">
        <xsl:matching-substring>
          <responseCode><xsl:value-of select="regex-group(1)"/></responseCode>
          <responseMsg><xsl:value-of select="regex-group(2)"/></responseMsg>
        </xsl:matching-substring>
    </xsl:analyze-string>
</createTicketResponse>

Upvotes: 3

Related Questions