使用 xslt 添加具有静态值的属性 [英] Add an attribute with static value with xslt

查看:25
本文介绍了使用 xslt 添加具有静态值的属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要使用 xslt 向现有 xml 文件中特定类型的所有节点添加一个具有静态值的属性.基本上是这样的:

I need to add an attribute with a static value to all nodes of a specific type in an existing xml file using xslt. Basically something like this:

<root>
  <somenode att1="something" />
  <mynode id="1" att1="value1" att2="value2"/>
  <mynode id="2" att1="value3" att2="value4"/>
</root>

我需要它像这样:

<root>
  <somenode att1="something" />
  <mynode id="1" att1="value1" att2="value2" newatt="static string"/>
  <mynode id="2" att1="value3" att2="value4" newatt="static string"/>
</root>

我查看了这个答案,但我无法使用它案例,如果它可以用于我正在尝试的东西.

I took a look at this answer but I was not able to use it for this case, if it could be used for what I'm trying.

我以前从未使用过 xslt,我真的需要一些帮助.

I've never used xslt before, I really need some help.

谢谢.

推荐答案

<xsl:template match="mynode">
 <xsl:copy>
  <xsl:attribute name="newatt">static string</xsl:attribute>
  <xsl:apply-templates select="node()|@*"/>
 </xsl:copy>
</xsl:template>

(或类似的东西)插入到进行身份转换的 XSLT 中(参见 http://www.dpawson.co.uk/xsl/sect2/identity.html) 应该为您解决问题.

(or something like that) inserted into an XSLT that does an identity transform (see http://www.dpawson.co.uk/xsl/sect2/identity.html) should do the trick for you.

这篇关于使用 xslt 添加具有静态值的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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