XSLT 向变量中的元素添加属性 [英] XSLT add attributes to elements in a variable

查看:23
本文介绍了XSLT 向变量中的元素添加属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 XSLT 1.0 中,我有一个带有一些 XML 节点的变量:

In XSLT 1.0 I have a variable with some XML nodes:

<xsl:varible name="MyVariable">
   <SomeElement>text here</SomeElement>
   <SomeElement>text with <OtherElement>other element</OtherElement></SomeElement>
   <SomeElement>something else <DoNotAddAnything>...</DoNotAddAnything></SomeElement>
</xsl:variable>

我需要向元素 SomeElement 和 OtherElement(而不是其他元素)添加具有某些值的属性.我需要将变量的内容转换成这样的:

I need to add an attribute with some value to elements SomeElement and OtherElement (but not to others). I need to transform the content of the variable to something like this:

   <SomeElement NewAttribute="x">text here</SomeElement>
   <SomeElement NewAttribute="x">text with <OtherElement OtherAttribute="y">other element</OtherElement></SomeElement>
   <SomeElement NewAttribute="x">something else <DoNotAddAnything>...</DoNotAddAnything></SomeElement>

这是较大转换的一部分,我只需要对变量的内容做这个,而不是对XSLT转换的整个输入.我找到了转换整个输入 (xsl:apply-template) 的简单解决方案,但我很难仅对变量的内容实现相同的解决方案.非常感谢.

This is part of larger transformation, and I only need to do this for the content of the variable, not for the entire input of XSLT transformation. I found easy solutions for converting entire input (xsl:apply-template) but I'm having hard times to achieve the same only for the content of a variable. Thanks a lot.

推荐答案

在 XSLT 1.0 中,我有一个带有一些 XML 节点的变量:

In XSLT 1.0 I have a variable with some XML nodes:

这是一种误解.您有一个带有结果树片段的变量,它与一些 XML 节点"完全不同.

That's a misconception. You have a variable with a result tree fragment, which is strictly not the same as "some XML nodes".

您必须将结果树片段转换回实际的 XML 节点.对于普通的 XSLT 1.0,这是不可能的,但许多 XSLT 处理器都有一个名为 node-set() 的扩展函数,它就是这样做的.如何启用该功能取决于您的特定 XSLT 处理器.

You must convert the result tree fragment back to actual XML nodes. With vanilla XSLT 1.0 this is not possible, but many XSLT processors have an extension function called node-set() that does just that. How to enable that function depends on your specific XSLT processor.

<xsl:apply-templates select="exslt:node-set($MyVariable)" />

<xsl:template match="SomeElement">
  <xsl:copy>
    <xsl:copy-of select="@*" />
    <xsl:attribute name="NewAttribute">x</xsl:attribute>
    <xsl:copy-of select="node()" />
  <xsl:copy>
</xsl:template>

也就是说,您试图使用一个包含一些 XML 节点"的变量,这让我感到很奇怪.除了非常特殊的情况,你不应该这样做,所以我认为你可能做错了什么.

That said, it strikes me as odd that you're trying to use a variable with "some XML nodes" in it. Except for very special cases you should not have to do that, so I think you might be doing something wrong.

注意

<xsl:variable name="foo" select="/some/xml/nodes" />

包含结果树片段,而是一个普通的节点集.

does not contain a result tree fragment, but an ordinary node set.

然而,未通过 select="..." 定义的变量 从不 包含实际节点,但始终包含结果树片段(您可以将其视为字符串,基本上).

However, a variable that is not defined via select="..." never contains actual nodes, but always result tree fragments (which you can think about as strings, basically).

这篇关于XSLT 向变量中的元素添加属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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