在 XSL 转换中为命名空间使用变量 [英] Use a variable for a namespace in an XSL transform

查看:17
本文介绍了在 XSL 转换中为命名空间使用变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

This is probably a duplicate, but I haven't found the answer from any other posts so I will go ahead and ask.

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

Inside an XSL file, I'd like to have variables that are the namespaces that will be output.

类似于:

<xsl:variable name="some_ns" select="'http://something.com/misc/blah/1.0'" />

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

Then in a template, do this:

<SomeElement xmlns="$some_ns">

我没有运气得到这项工作,尽管它看起来很简单.

I have had no luck getting this work, even though it seems rather simple.

感谢您的时间.

推荐答案

要在运行时动态设置命名空间,请使用 和属性值模板.

To set namespaces dynamically at runtime, use <xsl:element> and an attribute value template.

<xsl:element name="SomeElement" namespace="{$some_ns}">
  <!-- ... -->
</xsl:element>

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

If you don't need to set dynamic namespaces, declare a prefix for them and use that:

<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>

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

or even mark the namespace as default:

<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>

这篇关于在 XSL 转换中为命名空间使用变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆