Friday, February 4, 2011

Using XSLT, How Do You Insert XML Into an existing XML node

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?

  • XSLT basically changes one XML document into another, for instance it might take a data XML file and produce a web display XHTML file.

    XSLT doesn't change the original file.

    It does include the ability to repeat some XML for each element in a data file - is that what you mean?

    From Keith
  • 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>
    
    warsze : 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!

0 comments:

Post a Comment