使用 xslt 将两个节点合二为一 [英] Transform two nodes into one using xslt

查看:39
本文介绍了使用 xslt 将两个节点合二为一的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下 xml 输入:

I have the following xml input:

<data>
    <parent Id="1" value="ParentOne">
       <child x="1" y="2" />
    </parent>
    <parent Id="2" value="ParentTwo">
        <child x="3" y="4" />
    </parent>
</data>

我需要输出的应该像这样结合父节点和子节点:

What I need to output should look like this combining the parent and child nodes:

<data>
    <combined Id="1" value="ParentOne" x="1" y="2" />
    <combined Id="2" value="ParentTwo" x="3" y="4" />
</data>

如何使用 XSLT 实现此目的?另外,请注意名为 的新命名节点.

How can I achieve this using XSLT? Also, take note of the newly named node called <combined>.

感谢您的帮助.

谢谢.

推荐答案

您可以使用此模板将 parent-with-child 转换为组合元素:

You can use this template to transform the parent-with-child into the combined element:

<xsl:template match="parent">
   <combined>
      <xsl:copy-of select="@* | child/@*" />
   </combined>
</xsl:template>

这样做是将输入 元素及其 中的所有属性复制到输出 中. 元素.

What this does is copy all the attributes from the input <parent> element and its <child>, into the output <combined> element.

您还需要身份模板,以便通过 元素和其他节点传递:

You'll also want the identity template, in order to pass the <data> element and other nodes through:

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

这篇关于使用 xslt 将两个节点合二为一的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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