选择节点与默认命名空间(Selecting nodes with the default names

2019-06-23 12:42发布

我有在使用许多不同的命名空间和架构来验证XML文档。 该模式要求所有元素“合格”,而我认为,这意味着他们需要有充分的QName没有一个空的命名空间。

然而,这个巨大的XML文档中的某些元素都通过只使用默认的命名空间,在这个文件的情况下是空白的下滑。 Natually,他们无法验证用模式。

我试图写一个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/"到文档的根节点,但这并不与架构验证同意。 我如何能够使这项工作有什么想法?

谢谢!

Answer 1:

<!-- 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 =属性的相同的命名空间的一部分。



文章来源: Selecting nodes with the default namespace