如何使用 xslt 1.0 做模板不匹配

how to do template not matching using xslt 1.0

我有一个要求,我正在处理基于根元素标签的消息,为此我创建了 3 个基于根标签元素的不同模板匹配。我想知道如果客户端发送与根标记元素不匹配的不同消息,如何处理消息。

输入:

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

第 2 条消息: 除了根标签将是 process2process3

之外,一切都将相同

代码:

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

我的问题是我想处理消息以防它与 3 个模板不匹配 process1,process2,process3.

任何人都可以建议如何实现吗?

首先,不要使用 local-name()。声明和使用正确的命名空间很容易,去做吧。

其次,只需制作一个不太具体的模板,以捕获任何具有您未预料到的名称的文档元素(请参阅下面的第 4 个模板):

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

注意:如果前三个模板都做同样的事情,您可以将它们折叠成一个。

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