23

I'm not even sure if it's possible but say I have some 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?

1 Answer 1

37

Add these 2 template definitions to an XSLT file:

<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> 
Sign up to request clarification or add additional context in comments.

2 Comments

That's exactly what I was looking for. We have vendor config files where we have to add a bunch of custom properties. Ideally we would like to automate this rather than hand edit it everytime. Thanks!
Awesome answer even 7 years later. :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.