2016-02-19 79 views
0

所有元素我有以下源XML文件:滤波器定义的命名空间

<?xml version="1.0" encoding="UTF-8"?> 
<root xmlns:filter="filter_this" xmlns:also_filter="filter_also"> 
    <filter:error>This element should be filtred</filter:error> 
    <ok>This is ok</ok> 
    <filter:error>This element should be filtred also</filter:error> 
    <ok>This is also ok</ok> 
</root> 

的任务是使用命名空间来过滤元素,如*:*root元素中可能有随机命名空间的元素,而不仅仅是filteralso_filter,这些仅供参考。

因此所需的输出应该是这样的:

This is ok 
This is also ok 

我已经尝试了下面的XSLT模板:

<?xml version="1.0" encoding="UTF-8"?> 
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    xmlns:xs="http://www.w3.org/2001/XMLSchema" 
    exclude-result-prefixes="xs" 
    version="2.0"> 
    <xsl:output indent="yes" method="text"/> 

    <xsl:template match="/"> 
     <xsl:for-each select="/root/*"> 
      <xsl:if test="not(namespace::*)"> 
       <xsl:copy-of select="."/> 
       <xsl:text>&#xa;</xsl:text> 
      </xsl:if> 
     </xsl:for-each> 
    </xsl:template> 

</xsl:stylesheet> 

这给空输出。我需要找出<xsl:if...></xsl:if>声明中的条件。请帮助我:)

UPD: 由于Saxon-HE 9.5.1.7的使用情况,xslt 2.0解决方案正常。

回答

1

您当前的解决方案失败,因为根下的所有元素的名称空间轴包含两个节点(对于名称空间filter_thisfilter_also)。所以if总是评估为false。

但是你可以测试,如果一个元素没有命名空间URI:

<xsl:if test="not(namespace-uri())"> 
+0

测试,这个工程。我的误解是选择xpath-ax而不是比较实际的前缀。 TY。 – Helvdan