englishteeth.co.uk

… the weblog of Ian “English Teeth” Robinson
  • rss
  • Home

Augmenting XML using XSLT

January 16, 2008 | 10:42 am

In order to augment a piece of XML using XSLT, the base requirement is to make sure the existing document or fragment is preserved. This is done by recursively copying all the elements, attributes etc. as defined by the W3C Recommendation for copying.

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

The main things going are here is the matching of every attribute with “@*” and every child node (node() being shorthand for child::node()). As mentioned in the W3C Recommendation, the root node is considered a special case and is implicit, therefore “@*|node()” finds everything else.

This is all well and good, but the benefit of this recurisve copying is making the exceptions or augmentations to the input XML. To implement these we simply add templates matching the point of change:

<xsl:template match="existingElement" >
  <xsl:element name="replacementElement">
    <xsl:text>Some content for the new element</xsl:text>
  </xsl:element>
</xsl:template>

A really good description and set of examples of doing this sort of thing (also known as the identity template) can be found here.

Comments
No Comments »
Categories
development, software
Tags
xml, xslt
Comments rss Comments rss
Trackback Trackback

Author

Ian Robinson is a relatively agile software engineer interested in things both sides of the object relational divide and beyond.

Categories

  • development (35)
  • miscellaneous (20)
  • music (7)
  • software (13)

Blogroll

  • Dan North
  • Dave Astels
  • Dave Wood
  • eirikso.com
  • Matt Raible
  • Object Mentor Blog
  • The Ancient Art of Programming
  • The Wisdom of Ganesh

Tags

active-mq architecture bauhaus blu-ray css db eclipse esb festivals freesat gorm grails groovy hd hd-dvd hibernate java jboss jms junit links mce media center mini music oracle osgi patterns pirsig plugins satellite soa software spring sql struts2 testing themes tools tv web wordpress xml xpath xslt
rss Comments rss valid xhtml 1.1 design by jide powered by Wordpress get firefox