Kumar_y
Kumar_y

Reputation: 239

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

I have an requirement in which I am testing the condition based on plant number which is pretty straight forward, however the problem is something string comes with alphabet and condition is getting failed.

Input

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

I am storing the value in the config xml file and matching it.

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

The problem is when string comes like 4545A it's getting failed.

I was wondering how to write the condition which will accept any Alphabets?

Code:

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

Config file

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

I am taking the logicalID from the payload and comparing with config file.

This is the condition is failed

<xsl:when test="$Partnerrouting/*[local-name()='destinations']/*[local-name()='destination'][@PlantID=$LogicalID]/@*[local-name()='toPartnerIdentifier'] !=''"> when any alphabets are added in the logical ID like 4545A,4545B etc etc

Since the XML file contains the static value I was wondering is there any way of ignoring the Alphabets all together.

Can any please advise.

Upvotes: 0

Views: 319

Answers (1)

michael.hor257k
michael.hor257k

Reputation: 117165

You can remove any non-digit characters from a string using:

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

so that instead :

[@PlantID=$LogicalID]

you would have:

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

This is assuming $LogicalID already contains digits only. If not, redefine it as:

<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', ''), '')"/>

Unrelated to your question:
I'd suggest you use a prefix to handle the namespace in your XML, instead of dancing around it using awkward expressions like *[local-name()='abcdef'.

For example, declare:

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

then use:

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

Upvotes: 1

Related Questions