复制节点并使用Xslt将值添加到属性 [英] Copy node and add value to attribute with Xslt

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

问题描述

Basiccly我有xml和xslt的问题,我不知道如何解决,并将感谢任何帮助,关于这个问题从哪里开始。我有XML:

Basiccly i have problem with xml and xslt which i do not know how to solve, and would appreciate any help regarding this matter where to start. I have XML:

<root>
 <test value="1" setting="3">
   <tag1>data....</tag1>
   <tag2>data....</tag2>
   <tag n+1>data....</tag n+1>
 </test>
 <test value ...
 .
 .
 .
 </test>
</root>

现在我需要以这种方式复制test节点中的所有节点,值3到设置值(设置值在测试节点中改变),如下所示,因此我将得到:

Now i would now need to copy, all nodes in "test" node in this way and add always value 3 to settings value (settings value is changing in test node) in 4 new nodes like shown below so i would get:

<root>
 <test value="2" setting="6">
   <ni1 name="1" setting1="6">data....</ni1>
   <ni2 name="1" setting1="6">data....</ni2>
   <ni3 name="1" setting1="6">data....</ni3>
   <ni4 name="1" setting1="6">data....</ni4>
   <tag1>data....</tag1>
   <tag2>data....</tag2>
   <tag n+1>data....</tag n+1>
 </test>
 <test value ...
 .
 .
 .
 </test>
</root>

感谢您就此问题提供任何帮助,
eoglasi

Thanks a lot for any help regarding this matter, eoglasi

推荐答案

如评论中所述,标识转换< a>是当您转换XML并且只想对XML的某些部分进行更改时所需要的

As mentioned in the comments, the identity transform is what you need when you are transforming XML and only want to make changes to certain parts of the XML

<xsl:template match="@*|node()">
   <xsl:copy>
      <xsl:apply-templates select="@*|node()"/>
   </xsl:copy>
</xsl:template>

你说你想将总是值3添加到设置,所以你会有一个模板

You say you want to "add always value 3 to settings", so you would have a template that matches the settings attribute.

<xsl:template match="test/@setting">

(在这种情况下,它只会匹配属于测试元素。)

(In this case, it will only match the settings attribute that belongs to a test element.)

然后在此模板中,您可以使用 xsl:attribute 属性改为具有相同的名称但修改后的值

And then within this template you then use xsl:attribute to output a new attribute instead with the same name but amended value

<xsl:attribute name="setting">
  <xsl:value-of select="number(.) + 3" />
</xsl:attribute>

你说你也想复制 test 节点下的4个节点。这意味着您需要一个模板来匹配测试节点,因为这是您需要转换以添加子元素

You say you also want to copy 4 nodes under the test node. This means you need a template to match the test node as that is what you need to transform to add the children

<xsl:template match="test">
   <xsl:copy>
     <xsl:apply-templates select="@*" />
     <!-- Add new nodes here -->
     <xsl:apply-templates select="node()"/>
   </xsl:copy>
</xsl:template>

不清楚新节点的数据来自何处,因此您必须但它看起来像是设置属性来自测试元素的设置属性。因此,您的代码可能如下所示:

It is not clear where the data for your new nodes comes from, so you will have to do that yourself, but it does look like the setting attribute comes from the setting attribute on the test element. Therefore, your code may look like this:

<ni1 name="1" setting1="{number(@setting) + 3}">data....</ni1>

请注意这里使用的属性值模板。 {} 表示要评估的表达式,而不是字面上的输出。

Note the use of Attribute Value Templates here. The curly braces { } indicate an expression to be evaluated rather than output literally.

尝试使用此XSLT作为示例。

Try this XSLT as a sample.

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:template match="@*|node()">
    <xsl:copy>
      <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="test/@setting">
    <xsl:attribute name="setting">
      <xsl:value-of select="number(.) + 3" />
    </xsl:attribute>
  </xsl:template>

  <xsl:template match="test">
    <xsl:copy>
      <xsl:apply-templates select="@*" />
      <ni1 name="1" setting1="{number(@setting) + 3}">data....</ni1>
      <xsl:apply-templates select="node()"/>
    </xsl:copy>
  </xsl:template>
</xsl:stylesheet>

这篇关于复制节点并使用Xslt将值添加到属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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