开发者

How do I insert XML into another XML file with XSLT?

开发者 https://www.devze.com 2022-12-31 08:49 出处:网络
I looked at this thread to find out how to insert XML into XML with XSLT Insert XML node at a specific position of an existing document

I looked at this thread to find out how to insert XML into XML with XSLT Insert XML node at a specific position of an existing document

But I have a problem since I need to insert XML between two grand child nodes. For example I want to insert <s>...</s> between <r>...</r> and <t>...</t> in this file

<root>
  <child1>
    <a>...</a>
    <r>...</r>
    <t>...</t>
    <z>...</z>
  </child1>
</root>

to create this file

<root>
  <child1>
    <a>...</a>
    <r>...</r>
    <s>...</s>
    <t>...</t>
    <z>...</z>
  </child1>
<开发者_StackOverflow中文版/root>

Thanks for your help.


A standard "identity transform" plus one template to match element <r> and insert <s>...</s> afterwards:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" exclude-result-prefixes="xs" version="2.0">
  <xsl:output method="xml" indent="yes"/>
  <xsl:template match="@*|node()">
    <xsl:copy>
      <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
  </xsl:template>
  <xsl:template match="r">
    <xsl:copy>
      <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
    <s>...</s>
  </xsl:template>
</xsl:stylesheet>
0

精彩评论

暂无评论...
验证码 换一张
取 消