如何从 XSLT 2.0 中的字符串中选择特定部分

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

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

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

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

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

肥皂响应 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 中,它与正则表达式匹配,但我无法从中提取错误代码。

有人可以指出我正确的方向,以便从标签 <Incident_Number> 中挑选出唯一的错误代码。

预期输出:

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

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

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