s c h e m a t i c s : c o o k b o o k

/ Cookbook.XMLRecipeSimpleChanges

This Web

TOC (with recipes)

Other Webs



Schematics Home
Sourceforge Page
Original Cookbook

Scheme Links

Scheme FAQ
Scheme Cross Reference
Scheme48 SCM
MIT Scheme scsh
JScheme Kawa
Chicken Guile
Bigloo Tiny
Gambit LispMe

Lambda the Ultimate

Making Simple Changes to Elements or Text


You want to filter some XML. For example, you want to make substitutions in the body of a document, or add a genre to every song in described described in an XML document, or you want to change:

<book id="1"> to <book><id>1</id></book>


The nifty thing about SXML is that the representation is simply a list, so the rules for altering lists apply equally well here. Here's a quick guide to manipulating tree structures: 1 car gives you the name of a node n. 1 cdr selects the children of a node in the tree. You can (define children cdr) to make that more obvious. 1 Based on the last rule, the cadr of a node n is is the first child of n. 1 If an element has attributes, the first child will be '@, the list of attributes of a node n will then be (cdr (children n)). 1 When dealing with attributes, the value of the attribute is the cadr of the list representing that attribute. Therefore, (set-car! (cdr n) "some value") will change the value of an attribute n in place. 1 Given a list of children of a node n, append! will append a child to the end of the list of children. Rember that the child you're appending has to be a list, if you're appending an entire subtree, you have to wrap it in a list. 1 Given a a node n, you can set-cdr! to the node to replace the children of a node. If you cons a new node to the cdr or cddr of a node (depending on whether it has attributes), that's the same as adding a child to the beginning of the list of children.

These rules work a bit differently than DOM: in DOM, children of an element are everything but attributes, attributes have their own access methods. Here, we're treating attributes more or less on equal footing as other children. However, this can get you into a little trouble, so later we'll need to take this into account.

Let's try some examples, starting with a sample document: 500 Can't connect to (connect: Connection refused)

Now, you can use the following definitions

500 Can't connect to (connect: Connection refused)

-- HectorEGomezMorales - 05 May 2004

-- GordonWeakliem - 03 Aug 2004

-- DannyYoo - 08 Oct 2007 Adjusted the example to use PLaneT's version of the SSAX library. Also included a definition for AIF.

TopicType: Recipe
ParentTopic: XmlRecipes
TopicOrder: 040

Copyright © 2004 by the contributing authors. All material on the Schematics Cookbook web site is the property of the contributing authors.
The copyright for certain compilations of material taken from this website is held by the SchematicsEditorsGroup - see ContributorAgreement & LGPL.
Other than such compilations, this material can be redistributed and/or modified under the terms of the GNU Lesser General Public License (LGPL), version 2.1, as published by the Free Software Foundation.
Ideas, requests, problems regarding Schematics Cookbook? Send feedback.
/ You are Main.guest