我有一个要求,我要根据根元素标签处理消息,为此,我根据根标签元素创建了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条消息:除root标签为之外,其他所有内容都相同process2
,process3
代码:
<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()
。声明和使用适当的名称空间很容易做到。
其次,只需创建一个模板,该模板不太特定,可以使用您没有想到的名称来捕获任何文档元素(请参见下面的第四个模板):
<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>
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句