Re: [xsl] Processing tag text either side of nested tags

Subject: Re: [xsl] Processing tag text either side of nested tags
From: "Jon Gorman" <jonathan.gorman@xxxxxxxxx>
Date: Tue, 11 Apr 2006 09:56:11 -0500
On 4/11/06, Neil Crofts <neil.crofts@xxxxxxxxx> wrote:
> I have some XML with text contained either side of a nested tag. I
> would like to apply a named template to the text parts such that I
> modify the text but also preserve the nested tags as-is. For example,
> I have some XML of the form:
>
> <outer> hello there <inner name="alpha">beta gamma</inner> goodbye then
</outer>
>
> ...and I need to transform it to the following format:
>
> <outer> HELLO THERE <inner name="alpha">beta gamma</inner> GOODBYE THEN
</outer>
>
> where in this case a named template converts the text to upper case,
> although ideally the conversion function would be arbitrary.
>
> I have made a number of attempts so far at doing this, but without
> success. For example:

Hope you don't mind that I don't follow what you had too closely.
You're probably making this more complicated than it needs to be.

So you want all children text nodes of outer to be transformed, but
all children element nodes to be copied.  Your identity transform
takes care of the latter.  Now lets worry about the former.

<xsl:template match="outer/text()">
<xsl:value-of select="translate(.,"abc","ABC") />
</xsl:template>

Don't think in terms of tags, that will lead you down the wrong path.
Keep it simple and remember the tree model that we're working with:

outer (element node):
    hello there (child of outer - text node)
    inner (child of outer - element node)
           name (child of inner - attribute node)
           beta gamma (child of inner - text node)
      goodbye then (child of outer - text node

Hopefully my little ascii art comes through.  Since you can rely on
the default templates and the identity transform to recurse down the
tree, you just have to specify how to change the text nodes of outer.
The way that XSLT climbs down the tree by default is one of the things
I consider a perk.


Jon Gorman

Current Thread