[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

Re: replace xml element

Subject: Re: replace xml element
From: Jeni Tennison <jeni@xxxxxxxxxxxxxxxx>
Date: Wed, 1 May 2002 09:47:25 +0100
replace xml content
Hi Valerie,

> I have a question about replacing xml elements. Here is the problem;
> I want to output the content of the element <content> and replace
> the element <break/> with the html tag <br/>. I know this is
> probably a very basic question but I appreciate your help.

Rather than just getting the *value* of the content (with
xsl:value-of), apply templates to it:

  <xsl:apply-templates select="item/par/content" />

and then add a template that matches break elements and, when they're
encountered, inserts a br element to the result instead:

<xsl:template match="break">
  <br />
</xsl:template>

When you tell the processor to apply templates to the content element,
it will search through the templates in your stylesheet to find one
that matches content elements. If it doesn't find one, it'll use the
built-in template:

<xsl:template match="*">
  <xsl:apply-templates />
</xsl:template>

This tells the processor to apply templates to all the child nodes of
the matched (content) element -- the text nodes and the break
elements. When the processor tries to find a template that matches
text nodes, again it won't find one in your stylesheet, so it'll use
the built-in one:

<xsl:template match="text()">
  <xsl:value-of select="." />
</xsl:template>

so the value of each text node will be added to the result. When the
processor tries to find a template that matches the break elements, it
will find the one that you've provided, and add a br element to the
result for each one.

Applying templates to content and having templates to match different
bits of that content (a push approach) is the best way of processing
mixed content such as that in your example.

Cheers,

Jeni

---
Jeni Tennison
http://www.jenitennison.com/


 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list


Current Thread

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.