使用 XSLT <xsl:element> 和有什么区别?并从字面上声明元素? [英] What is the difference between using XSLT &lt;xsl:element&gt; and declaring elements literally?

查看:30
本文介绍了使用 XSLT <xsl:element> 和有什么区别?并从字面上声明元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近才开始使用 XSLT,我想知道使用 定义元素与仅将它们声明为 XSLT 中的文字之间的有效区别是什么.例如,让我们举一个简单的例子,我将一个很小的 ​​XML 文档的内容转换为 (x)HTML.

I have started using XSLT just recently and am wondering what the effective difference is between using <xsl:element> for defining elements vs. just declaring them as literals in the XSLT. For example, lets take a simplified case where I'm converting the contents of a tiny XML document into (x)HTML.

1.我可以采用 方式:

1.I could go with the <xsl:element> way:

<xsl:element name="h1">
    <xsl:value-of select="heading"/>
</xsl:element>

2.或者手动定义元素:

<h1>
    <xsl:value-of select="heading"/>
</h1>

这两者之间的实际区别是什么?如果存在差异,那么哪一个被认为是'好风格'?

What is the actual difference between these two and if a difference exists, which of them is considered 'good-style'?

推荐答案

它们几乎相同,不同之处在于文字

元素会将名称空间节点添加到结果树中在 样式表 中的那个点的范围内,而 不会.这对您的输出有何不同取决于您的样式表包含哪些命名空间声明以及您在结果树中使用它们的位置(如果有的话).例如,对任何输入 XML 文档运行以下转换:

They're almost identical, the exception being that a literal <h1> element will add to the result tree the namespace nodes that are in scope at that point in the stylesheet, whereas the <xsl:element name="h1"> won't. What difference this makes to your output depends on exactly what namespace declarations your stylesheet includes and where in the result tree you make use of them, if at all. For example, run against any input XML document the following transform:

<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:foo="http://example.com">

  <xsl:output method="xml" indent="yes" />

  <xsl:template match="/">
    <root>
      <foo:element1 />
      <foo:element2 />
    </root>
  </xsl:template>
</xsl:stylesheet>

产生以下输出(使用 xsltproc):

produces the following output (using xsltproc):

<?xml version="1.0"?>
<root xmlns:foo="http://example.com">
  <foo:element1/>
  <foo:element2/>
</root>

但是将样式表中的文字 改为 反而产生

but changing the literal <root> in the stylesheet to <xsl:element name="root"> instead produces

<?xml version="1.0"?>
<root>
  <foo:element1 xmlns:foo="http://example.com"/>
  <foo:element2 xmlns:foo="http://example.com"/>
</root>

因为 表单没有将foo"命名空间节点附加到生成的元素.如果这很重要,并且您确实想将样式表命名空间声明复制到您使用 <xsl:element> 创建的元素上,您可以通过嵌套类似

as the <xsl:element> form doesn't attach the "foo" namespace node to the generated element. If this matters, and you actually want to copy the stylesheet namespace declarations onto an element you create with <xsl:element> you can do so by nesting something like

<xsl:copy-of select="document('')/*/namespace::foo" />

直接在其中(使用 document('') 的习语,它提供对样式表 XML 文档本身的访问).

directly inside it (using the idiom of document('') which provides access to the stylesheet XML document itself).

但一般来说, 的主要用途是计算元素名称而不是编译时"字面量.

But generally, the main use of <xsl:element> is when the element name is calculated rather than a "compile-time" literal.

这篇关于使用 XSLT <xsl:element> 和有什么区别?并从字面上声明元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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