在XSL转换中为名称空间使用变量

chr*_*ead 5 xml xslt xslt-2.0

这可能是重复的,但我没有找到任何其他帖子的答案,所以我会继续问.

在XSL文件中,我希望有变量作为将输出的命名空间.

就像是:

<xsl:variable name="some_ns" select="'http://something.com/misc/blah/1.0'" />
Run Code Online (Sandbox Code Playgroud)

然后在模板中,执行以下操作:

<SomeElement xmlns="$some_ns">
Run Code Online (Sandbox Code Playgroud)

即使看起来相当简单,我也没有运气得到这项工作.

谢谢你的时间.

Tom*_*lak 8

要在运行时动态设置名称空间,请使用<xsl:element>和属性值模板.

<xsl:element name="SomeElement" namespace="{$some_ns}">
  <!-- ... -->
</xsl:element>
Run Code Online (Sandbox Code Playgroud)

如果您不需要设置动态命名空间,请为它们声明前缀并使用它:

<xsl:stylesheet 
  version="2.0" 
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
  xmlns:foo="http://something.com/misc/blah/1.0"
>
  <xsl:template match="/">
    <foo:SomeElement>
      <!-- ... -->
    </foo:SomeElement>
  </xsl:template>
</xsl:stylesheet>
Run Code Online (Sandbox Code Playgroud)

甚至将命名空间标记为默认值:

<xsl:stylesheet 
  version="2.0" 
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
  xmlns="http://something.com/misc/blah/1.0"
>
  <xsl:template match="/">
    <SomeElement>
      <!-- ... -->
    </SomeElement>
  </xsl:template>
</xsl:stylesheet>
Run Code Online (Sandbox Code Playgroud)