如何将 XML 插入现有的 XML 节点

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/54683/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-06 11:59:42  来源:igfitidea点击:

How Do You Insert XML Into an existing XML node

xmlxslt

提问by warsze

I'm not even sure if it's possible but say I have some XML:

我什至不确定这是否可能,但说我有一些 XML:

   <source>
        <list>
            <element id="1"/>
        </list>
    </source>

And I would like to insert into list:

我想插入列表:

<element id="2"/>

Can I write an XSLT to do this?

我可以编写一个 XSLT 来做到这一点吗?

回答by Chris Marasti-Georg

Add these 2 template definitions to an XSLT file:

将这 2 个模板定义添加到 XSLT 文件中:

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