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

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

有人可以指出我正确的方向,以便从标签 <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>

最佳答案

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

关于regex - 如何从 XSLT 2.0 中的字符串中选择特定部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34681387/

相关文章:

php - 如何找到网页中的所有链接(URL)并在所有链接的末尾添加一个字符串?

php - 需要用自定义同义词替换文本中的单词

javascript - 使用 Javascript 解析 xml 文件时,网络浏览器不显示任何内容

xml - 佛 :inline-elements create unwanted whitespace

xslt - 通过 XSLT 用 XHTML 中的标签替换 style= 属性

python - 使用 c++ 检索一些文本列比 python 慢得多

python - 从python中的字符串中提取英文单词

c# - 为 xml 转换图形 api

xml - 有没有办法只在 go xml 包中编码 XML 开始标记?

xml - 当我包含 xmlns ="http://www.sitemaps.org/schemas/sitemap/0.9"时,XSLT 不起作用