模板匹配 - 如何指定OR条件

时间:2012-02-29 13:54:52

标签: xslt

我想在模板中指定一个匹配表达式,该表达式将在元素的多个名称空间上调用:

<xsl:template match="*[namespace-uri()='abc.com' or namespace-uri()='def.com']">
  ...
</xsl:template>

但这似乎不起作用。只有在左侧或表达式为真时才会调用它。

2 个答案:

答案 0 :(得分:1)

使用命名空间的常用方法是声明它们,例如

<xsl:stylesheet
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  version="1.0"
  xmlns:abc="http://example.com/abc"
  xmlns:def="http://example.com/def"
  exclude-result-prefixes="abc def">

<xsl:template match="abc:* | def:*">...</xsl:template>

...

</xsl:stylesheet>

话虽如此,我认为你的or谓语表达没有任何问题,除了你没有提供任何你用它的输入。

答案 1 :(得分:0)

    <xsl:template match="*[namespace-uri()='abc.com' or namespace-uri()='def.com']"> 
      ... 
    </xsl:template>

But this does not seem to work. 

是正确的代码

因此,问题在于您未向我们展示的代码。请提供一个简单的XML文档,以便每个人都可以将提供的XSLT代码应用于提供的XML文档并重现问题。

以下是“疑似”代码正确的示例

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>

 <xsl:template match="*[namespace-uri()='def.com' or namespace-uri()='abc.com']">
  <xsl:copy-of select="."/>
 </xsl:template>
</xsl:stylesheet>

在此XML文档上应用此转换时

<a>
 <b:b xmlns:b="abc.com">
  <c/>
 </b:b>
 <f/>
 <d:d xmlns:d="def.com">
  <e/>
 </d:d>
</a>

产生了想要的正确结果

<b:b xmlns:b="abc.com">
   <c/>
</b:b>
<d:d xmlns:d="def.com">
   <e/>
</d:d>
相关问题