空白xmlns =“”导入的属性

前端之家收集整理的这篇文章主要介绍了空白xmlns =“”导入的属性前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在尝试对 XML文档进行转换.我的 XML转换可以导致两种不同类型的基本元素,具体取决于某个元素的值:

<xsl:template match="/">
  <xsl:choose>
    <xsl:when test="/databean/data[@id='pkhFeed']/value/text()='200'">
      <xsl:call-template name="StructureA">
        <xsl:with-param name="structure" select="//databean" />
      </xsl:call-template>
    </xsl:when>
    <xsl:otherwise>
      <xsl:call-template name="StructureB">
        <xsl:with-param name="structure" select="//databean" />
      </xsl:call-template>
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>

然后使用自己的名称空间和schemaLocations创建StructureA或StructureB:

<StructureA xmlns="http://...">

StructureA& B共享一些共同元素,因此这些元素在一个名为“xmlcommon.xslt”的单独文件中定义,两个结构都包含来自的模板.此xmlcommon文件没有定义默认名称空间,因为我希望它可以从StructureA或StructureB中定义的名称空间中使用.但是当我运行我的转换时,从公共文件中引入的任何模板都会产生空白的xmlns属性

<StructureA xmlns="http://...">
  <SharedElement xmlns="">Something</SharedElement>
</StructureA>

验证时,然后使用空白名称空间而不是正确的父名称空间.有谁知道如何阻止我的公共文件中的模板添加那些空白的xmlns属性

这是公共文件的片段:

<xsl:stylesheet version="1.0" xmlns:fn="http://www.w3.org/2005/02/xpath-functions" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

  <xsl:template name="ControlledListStructure">
    <xsl:param name="xmlElem" />
    <xsl:param name="structure" />

    <xsl:element name="{$xmlElem}">
      <!-- Blah blah blah -->
    </xsl:element>
  </xsl:template>
</xsl:stylesheet>

解决方法

要实现的关键是你的样式表规定了你添加到结果树的每个元素的名称.元素的名称有两部分:本地名称名称空间URI.在上面的代码中,您提供了本地名称($xmlElem的值),但是您没有指定名称空间URI,这意味着它将默认为空字符串. (实际上,它采用该样式表模块的默认命名空间;因为没有,所以它是空字符串.)换句话说,该元素不在命名空间中.在序列化文档时,XSLT处理器必须包含xmlns =“”取消声明,以便取消声明顶部显示的默认命名空间.否则,该元素将采用该命名空间,这不是您的样式表所指示的.修复此问题的最少侵入性方法添加另一个参数(例如$namespaceURI),就像使用$xmlElem一样.然后你会写:

<xsl:element name="{$xmlElem}" namespace="{$namespaceURI}">

现在,结果元素将采用您告诉它采用的任何命名空间(这将具有删除那些默认命名空间取消声明的效果).

这应该回答你的问题.我提供以下免费奖励材料.

猜你在找的XML相关文章