2

I have a simple XML file

 <Config new="Y" >
   <schema_path value="/var/tmp/sh.xsd"/>
   <collection name="new" >        
       <unit-list>
         <Instance active="Y" unit_type="xp" unit_name="table"/>
       </unit-list>
   </collection>
</Config>

I'd like to instert a new element Instance into xml in unit-list

<Instance active="N" unit_type="linux" unit_name="door" />

How to do it ?

1
  • Thank you , your xsl is working perfectly! In addition I'd like to fix two issues : #1. I have a comment at the end of xml file , this comment doesn't appear in mew output.xml.How to fix ? #2 small cosmetic issue : after adding a new line </unit-list> appear in same line with inserted line .Could it be fixed ? and last : could I ask for comment in 2-3 sentences how your script works. Thanks in advance ! Commented Aug 4, 2011 at 8:28

1 Answer 1

3

This copies everything from the input XML and adds the Instance at the end of unit-list:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">

    <xsl:template match="unit-list">
        <unit-list>
            <xsl:apply-templates />
            <Instance active="N" unit_type="linux" unit_name="door" />
        </unit-list>
    </xsl:template>

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

</xsl:stylesheet>
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you , your xsl is working perfectly! In addition I'd like to fix two issues : #1. I have a comment at the end of xml file , this comment doesn't appear in mew output.xml.How to fix ? #2 small cosmetic issue : after adding a new line </unit-list> appear in same line with inserted line .Could it be fixed ? and last : could I ask for comment in 2-3 sentences how your script works. Thanks in advance !

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.