Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xslt/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
空白xmlns="&引用;来自导入的属性_Xml_Xslt_Xml Namespaces - Fatal编程技术网

空白xmlns="&引用;来自导入的属性

空白xmlns="&引用;来自导入的属性,xml,xslt,xml-namespaces,Xml,Xslt,Xml Namespaces,我正在尝试对XML文档进行转换。根据特定元素的值,我的XML转换可能会产生两种不同类型的基本元素: <xsl:template match="/"> <xsl:choose> <xsl:when test="/databean/data[@id='pkhfeed']/value/text()='200'"> <xsl:call-template name="StructureA"> <xsl:with-

我正在尝试对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>

然后使用各自的名称空间和模式位置创建StructureA或StructureB:

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

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

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

某物

在验证时,使用空白命名空间代替正确的父空间。strong>有人知道如何阻止我的公共文件中的模板添加那些空白的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。在上面的代码中,您提供了本地名称(值为
$xmllem
),但没有指定名称空间URI,这意味着它将默认为空字符串。(实际上,它采用该样式表模块的默认名称空间;因为没有名称空间,所以它是空字符串。)换句话说,元素将不在名称空间中。序列化文档时,XSLT处理器必须包含
xmlns=”“
un声明,以便取消声明顶部显示的默认命名空间。否则,元素将采用该名称空间,而不是样式表所指定的名称空间。解决此问题的最简单方法是添加另一个参数(例如,
$namespaceURI
),就像使用
$xmllem
一样。然后你会写:

<xsl:element name="{$xmlElem}" namespace="{$namespaceURI}">
这样做的好处是,如果有注释隐藏在代码中,代码不会中断:

<value>2<!--test-->00</value>
200
在这种情况下,有两个文本节点(“2”和“00”)。您最初的测试将失败,因为它会检查其中是否有一个等于“200”。在这种情况下不太可能发生,但在任何情况下,测试元素的字符串值(与其文本节点子元素相反)都是一种很好的做法

最后,我鼓励您学习模板规则和XPath上下文。我倾向于尽可能避免使用
。首先,模板规则可以帮助您避免XSLT中许多丑陋、冗长的部分

<xsl:template match="/databean[data[@id='pkhfeed']/value = '200']" priority="1">
  <StructureA xmlns="http://...">
    ...
  </StructureA>
</xsl:template>

<xsl:template match="/databean">
  <StructureB xmlns="http://...">
    ...
  </StructureB>
</xsl:template>

...
...
即使继续使用
,也不必传递
$structure
参数,因为当前节点在被调用的模板中保持不变。您可以从
//databean
(或者
/databean
,我怀疑这就是您的意思)中的任何一个
StructureA
StructureB
模板中轻松访问,因为当前节点仍然是“/”(文档节点)

如果您有兴趣进一步了解XSLT的核心处理模型及其最强大的功能(模板规则),那么我鼓励您查看我的XSLT1.0 Pocket Reference中的免费示例章节


我希望这对你有所帮助,即使这超出了你的预期

很好的解释,艾凡。没有更多的补充+从我这里得到1。欢迎来到SO,谢谢Evan。这是一个很大的帮助:)谢谢你提供的额外信息。我不经常接触XSLT,所以很高兴听到更好的方法。
<xsl:template match="/databean[data[@id='pkhfeed']/value = '200']" priority="1">
  <StructureA xmlns="http://...">
    ...
  </StructureA>
</xsl:template>

<xsl:template match="/databean">
  <StructureB xmlns="http://...">
    ...
  </StructureB>
</xsl:template>