2009-06-17 85 views
0

我已经获得了一个XML文档,其中使用了许多不同的命名空间,并且有一个模式用于验证。该模式要求所有元素都是“合格的”,并且我认为这意味着他们需要具有完整的QNames而没有空名称空间。使用默认命名空间选择节点

但是,这个巨大的XML文档中的一些元素已经通过使用默认命名空间,在这个文档的情况下是空白的。自然,他们未通过模式验证。

我想写一个XSLT,它将选择没有名称空间的节点,并为它们指定一个具有与其他名称相同前缀的特定节点。例如:

<x:doc xmlns:x="http://thisns.com/"> 
    <x:node @x:property="true"> 
    this part passes validation 
    </x:node> 
    <node property="false"> 
    this part does not pass validation 
    </node> 
</x:doc> 

我已经尝试添加xmlns="http://thisns.com/"到文档的根节点,但这并不与架构验证同意。关于如何使这项工作有任何想法?

谢谢!

回答

2
<!-- Identity transform by default --> 
<xsl:template match="node() | @*"> 
    <xsl:copy> 
    <xsl:apply-templates select="node() | @*"/> 
    </xsl:copy> 
</xsl:template> 
<!-- Override identity transform for elements with blank namespace --> 
<xsl:template match="*[namespace-uri() = '']">  
    <xsl:element name="{local-name()}" namespace="http://thisns.com/"> 
    <xsl:apply-templates select="node() | @*"/> 
    </xsl:element> 
</xsl:template> 
<!-- Override identity transform for attributes with blank namespace --> 
<xsl:template match="@*[namespace-uri() = '']"> 
    <xsl:attribute name="{local-name()}" namespace="http://thisns.com/"><xsl:value-of select="."/></xsl:attribute> 
</xsl:template> 

这将使类似的结果:

<x:doc xmlns:x="http://thisns.com/"> 
    <x:node x:property="true"> 
    this part passes validation 
    </x:node> 
    <node xp_0:property="false" xmlns="http://thisns.com/" xmlns:xp_0="http://thisns.com/"> 
    this part does not pass validation 
    </node> 
</x:doc> 

注意,第二<节点>仍然没有命名空间前缀,但它现在被认为是因为XMLNS的相同的命名空间的一部分=属性。

相关问题