englishteeth.co.uk

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

Augmenting XML using XSLT

Ian | January 16, 2008

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 (37)
  • miscellaneous (28)
  • music (7)
  • software (19)

What I'm Doing...

  • @noelfielding11 why are you in watching telly!? in reply to noelfielding11 2010-04-16
  • What was so good about Nick Drake? These "artists" are covering, music is spot on but no effect at all. Totally lacking the goose pimples. 2010-04-16
  • Some Ginger bloke's on telly covering Nick Drake in a mediocre style. 2010-04-16
  • More updates...

Posting tweet...

Powered by Twitter Tools.

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 css db eclipse esb festivals freesat gorm grails groovy hd hibernate htpc 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 vmc web wordpress xml xpath xslt
rss Comments rss valid xhtml 1.1 design by jide powered by Wordpress get firefox