将命名空间传递给包含的xslt

时间:2014-12-16 08:16:58

标签: xml xslt

我使用两个文件转换。主文件在其第二个转换文件中包含import语句。我的第二个转换文件包含几个对我所有转换都通用的模板。我的问题是我的所有转换都不是同一个数据命名空间。我希望我可以在第二次转换中使用命名空间。

XSLT1:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format" xmlns:n="http://novamap.fr/xml/data/v1/XmlModelBonDeCommande" version="1.0">

  <xsl:include href="Common.xslt"/>

  <xsl:template match="/n:XmlModelBonDeCommande">
  ......
  </xsl:template>
</xsl:stylesheet>

XSLT2:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format" xmlns:n="http://novamap.fr/xml/data/v1/XmlModelEtatDesLieux" version="1.0">

  <xsl:include href="Common.xslt"/>

  <xsl:template match="/n:XmlModelEtatDesLieux">
  ......
  </xsl:template>
</xsl:stylesheet>

Common.xslt:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format" xmlns:n="<--my problem-->" version="1.0">

  <xsl:template name="doc1">
    <xsl:value-of select="n:VALUE1"/>
  </xsl:template>

  <xsl:template name="doc2">
    <xsl:value-of select="n:VALUE2"/>
  </xsl:template>

</xsl:stylesheet>

我想将名称空间传递给XSLT1和XSLT2文件中的Common.xslt

2 个答案:

答案 0 :(得分:1)

您需要在包含的文档中声明两个命名空间,并编写代码以匹配或选择两个命名空间中的元素,例如而不是<xsl:value-of select="n:VALUE1"/>你会使用<xsl:value-of select="n1:VALUE1 | n2:VALUE1"/>或更好,但你会改变像

这样的东西
  <xsl:template name="doc1">
    <xsl:value-of select="n:VALUE1"/>
  </xsl:template>

  <xsl:template match="n1:VALUE1 | n2:VALUE1">
    <xsl:value-of select="."/>
  </xsl:template>

另一种方法是使用单独的样式表或单独的转换步骤,首先将XML输入文档规范化为单个命名空间。

答案 1 :(得分:0)

我找到了另一个解决方案

<xsl:variable name="namespace" select="'<your namespace>'" />

<xsl:template name="doc1">
    <xsl:value-of select="*[local-name()='VALUE1' and namespace-uri()=$namespace]"/>
</xsl:template>
相关问题