Kumar_y
Kumar_y

Reputation: 239

how to do template not matching using xslt 1.0

I have an requirement in which I am processing the message based on the root element tag and for that I have created 3 different template match based on the root tag element. I was wondering how to process the message if the client is sending different message which is not matching the root tag element.

Input:

<?xml version="1.0"?>
<process1 xmlns="http://www.openapplications.org/oagis/10" systemEnvironmentCode="Production" languageCode="en-US">
    <Appdata>
        <Sender>
        </Sender>
        <Receiver>
        </Receiver>
        <CreationDateTime/>
    </Appdata>
</process1>

2nd message: Everything will be same except root tag will be process2, process3

Code:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format">
    <xsl:template match="/*[local-name()='proces1']">
        <operation>dosomthing</operation>
    </xsl:template>
    <xsl:template match="/*[local-name()='process2']">
        <operation>dosomthing2</operation>
    </xsl:template>
    <xsl:template match="/*[local-name()='process2']">
        <operation>blah blah</operation>
    </xsl:template>
</xsl:stylesheet>

My question here is I want to process message in case if it's not matching the 3 templates process1,process2,process3.

Can anyone please give advise how to achieve that?

Upvotes: 1

Views: 1859

Answers (1)

Tomalak
Tomalak

Reputation: 338248

First off, don't use local-name(). It's easy to declare and use the proper namespace, do it.

Secondly, just make a template that is less specific to catch any document element with a name that you did not anticipate (see the 4th template below):

<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:fo="http://www.w3.org/1999/XSL/Format"
    xmlns:oagis="http://www.openapplications.org/oagis/10"
>
    <xsl:template match="/oagis:process1">
        <operation>dosomething1</operation>
    </xsl:template>
    <xsl:template match="/oagis:process2">
        <operation>dosomething2</operation>
    </xsl:template>
    <xsl:template match="/oagis:process3">
        <operation>dosomething3</operation>
    </xsl:template>
    <xsl:template match="/*" priority="0">
        <!-- any document element not mentioned above -->
    </xsl:template>
</xsl:stylesheet>

Note: If the first three templates all do the same, you can collapse them into one.

<xsl:template match="/oagis:process1|/oagis:process2|/oagis:process3">
    <operation>dosomething</operation>
</xsl:template>

Upvotes: 3

Related Questions