【问题标题】:Selecting nodes with the default namespace选择具有默认命名空间的节点
【发布时间】:2009-06-18 03:15:09
【问题描述】:

我有一个 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/" 添加到文档的根节点,但这与架构验证器不一致。对如何完成这项工作有任何想法吗?

谢谢!

【问题讨论】:

    标签: xslt xpath namespaces


    【解决方案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= 属性,它现在被视为同一命名空间的一部分。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-05-30
      • 2010-10-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-07-04
      相关资源
      最近更新 更多