如何从XSLT2.0中的字符串中选择特定部分

时间:2016-01-08 16:12:30

标签: regex xml xslt xslt-2.0

我正在使用XSLT转换SOAP响应。我读了响应xml并尝试在我的XSLT中对其进行转换。

我有包含errorCode和errorMessage的字符串,我需要根据正则表达式匹配组合并从中选择一个特定的部分(即errorCode)。

输入字符串:ERROR (7000): ; Mandatory field missing : DESCRIPTION mandatory

现在,我想检查传入的字符串是否与我的输入字符串模式匹配,只选择括号内的部分(即errorCode = 7000)

SOAP响应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>

在当前的XSLT中,它与正则表达式匹配,但我无法从中选择errorCode。

有人可以指向正确的方向,以便从标记<Incident_Number>中选择唯一的errorCode。

预期输出:

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

2 个答案:

答案 0 :(得分:4)

您可以使用<xsl:value-of select='replace($ticketId, "ERROR\s\(([0-9]+)\):\s;\s(.*)", "$1")' />

答案 1 :(得分:3)

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