如何使用 XSLT 1.0 使用数字测试条件并匹配任何字母表

How to test the condition with numerical and match any alphabet using XSLT 1.0

我有一个要求,我正在根据植物编号测试条件,这非常简单,但问题是字符串带有字母表并且条件失败。

输入

<process1 xmlns="http://www.openapplications.org/oagis/10" systemEnvironmentCode="Production" languageCode="en-US">
    <Appdata>
        <Sender>
            <LogicalID>12344567</LogicalID>
        </Sender>
        <Receiver>
            <LogicalID>4545</LogicalID>
        </Receiver>
        <CreationDateTime/>
    </Appdata>
</process1>

我将值存储在配置 xml 文件中并匹配它。

<Receiver>
                <LogicalID>4545</LogicalID>
            </Receiver>

问题是当字符串像 4545A 这样出现时,它就失败了。

我想知道如何编写接受任何字母的条件?

代码:

<xsl:stylesheet xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <xsl:template match="/">
        <xsl:copy-of select="."/>
        <xsl:variable name="Partnerrouting" select="document('local:///ProcessShipmentvariables.xml')"/>
        <xsl:variable name="LogicalID" select="/*[local-name()='process1']/*[local-name()='Appdata']/*[local-name()='Receiver']/*[local-name()='LogicalID']"/>
        <xsl:variable name="partnerto">
            <xsl:choose>
                <xsl:when test="$Partnerrouting/*[local-name()='destinations']/*[local-name()='destination'][@PlantID=$LogicalID]/@*[local-name()='toPartnerIdentifier'] !=''">
                    <xsl:value-of select="$Partnerrouting/*[local-name()='destinations']/*[local-name()='destination'][@PlantID=$LogicalID]/@*[local-name()='toPartnerIdentifier']"/>
                </xsl:when>
                <xsl:otherwise>
                    <reject>Plant ID  didn't match </reject>
                </xsl:otherwise>
            </xsl:choose>
        </xsl:variable>
        <xsl:variable name="partnerfrom">
            <xsl:value-of select="$Partnerrouting/*[local-name()='destinations']/*[local-name()='destination']/@*[local-name()='fromPartnerIdentifier']"/>
        </xsl:variable>
        <xsl:variable name="partner-host">
            <xsl:value-of select="$Partnerrouting/*[local-name()='destinations']/*[local-name()='destination']/@*[local-name()='partner-host']"/>
        </xsl:variable>
        <xsl:variable name="partner-port">
            <xsl:value-of select="$Partnerrouting/*[local-name()='destinations']/*[local-name()='destination']/@*[local-name()='partner-port']"/>
        </xsl:variable>
    </xsl:template>
</xsl:stylesheet>

配置文件

<?xml version="1.0" encoding="UTF-8"?>
<destinations>
    <destination toPartnerIdentifier="KSKS" fromPartnerIdentifier="mypartner"  PlantID="5064"  partner-host="127.0.0.2" partner-port="80"/>
    <destination toPartnerIdentifier="KSKS" fromPartnerIdentifier="mypartner"  PlantID="5069"  partner-host="127.0.0.2" partner-port="80"/>
</destinations>

我正在从负载中获取逻辑 ID 并与配置文件进行比较。

这是条件不成立

<xsl:when test="$Partnerrouting/*[local-name()='destinations']/*[local-name()='destination'][@PlantID=$LogicalID]/@*[local-name()='toPartnerIdentifier'] !=''"> 当在逻辑 ID 中添加任何字母时,如 4545A,4545B

由于 XML 文件包含静态值,我想知道是否有任何方法可以一起忽略字母表。

有哪位请指教

您可以使用以下方法从字符串中删除任何 non-digit 个字符:

translate($string, translate($string, '0123456789', ''), '')

所以 :

[@PlantID=$LogicalID]

你会:

[translate(@PlantID, translate(@PlantID, '0123456789', ''), '') = $LogicalID]

这是假设 $LogicalID 已经只包含数字。如果不是,重新定义为:

<xsl:variable name="recid" select="/*[local-name()='process1']/*[local-name()='Appdata']/*[local-name()='Receiver']/*[local-name()='LogicalID']"/>
<xsl:variable name="LogicalID" select="translate($recid, translate($recid, '0123456789', ''), '')"/>

与您的问题无关:
我建议您使用前缀来处理 XML 中的名称空间,而不是使用像 *[local-name()='abcdef'.

这样笨拙的表达式来绕过它

例如声明:

xmlns:oag="http://www.openapplications.org/oagis/10"

然后使用:

<xsl:variable name="recid" select="oag:process1/oag:Appdata/oag:Receiver/oag:LogicalID"/>