tags:

views:

29

answers:

1

Hello,

I looked at this thread to find out how to insert XML into XML with XSLT http://stackoverflow.com/questions/862954/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>
</root>

Thanks for your help.

+1  A: 

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>
Jim Garrison
That worked. Thank you very much.
Sandra
If the answwer solved your problem, it's customary to mark it "accepted" by clicking on the checkbox at the top left of the answer.
Jim Garrison