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

RE: regarding strip-space

Subject: RE: regarding strip-space
From: "Andrew Welch" <awelch@xxxxxxxxxxxxxxx>
Date: Tue, 16 Jul 2002 15:07:09 +0100
strip space elements
Here is a good example that may help you understand whitespace and the
way it is handled by some processors:

Take this xml:

  <?xml version="1.0"?>
  <root>
    <node>Hello</node>
    <node>World</node>
  </root>

Here you have the root element, with two <node> elements and some
whitespace (carriage returns, non-breaking-spaces etc) as its children.
Without the whitespace as children, it would look like this:

  <?xml version="1.0"?>
  <root><node>Hello</node><node>World</node></root>

This whitespace is pretty insignificant here - its only to make the xml
more readable.  However, in certain cases you will want to handle this
whitespace, which is where <xsl:strip-space> and <xsl:preserve-space>
come in.

To highlight how these work, try applying this stylesheet to the above
xml:

  <xsl:stylesheet 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    version="1.0" >

  <xsl:template match="root">
    <xsl:value-of select="."/>
  </xsl:template>

  </xsl:stylesheet>

Using <xsl:value-of select="."/> on the <root> element will give you the
string value of all of its children, including the whitespace:

  <?xml version="1.0" encoding="utf-8"?>
    Hello
    World

This whitespace was only to make the xml more readable, and not wanted
in the output.  To get rid of this, you can use <xsl:strip-space
elements="*"/> :

  <xsl:stylesheet 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    version="1.0" >

  <xsl:strip-space elements="*"/>

  <xsl:template match="root">
    <xsl:value-of select="."/>
  </xsl:template>

  </xsl:stylesheet>

(You can be more specific than '*' but for now this will do)

So you output will now look like this:

  <?xml version="1.0" encoding="utf-8"?>HelloWorld

Note all on one line - no whitespace (carriage returns, nbsp's)

You can use this in combination with <xsl:preserve-space> to control the
whitespace that gets copied to your output tree.  <xsl:preserve-space>
has a higher priority than <xsl:strip-space>, so you can strip-space on
* and then list the elements you want to keep the whitespace for:

  <xsl:strip-space elements="*">
  <xsl:preserve-space elements="keepme"/>

on this data:

  <remove>
    <node>Hello</node>
    <node>World</node>
  </remove>
  <keepme>
    <node>foo</node>
    <node>bar</node>
  </keepme>

would give you:

<?xml version="1.0" encoding="utf-8"?>HelloWorld
    foo
    bar

The carriage returns within <keepme> have been copied to the output
tree, whereas those within <remove> have been stripped.

<note>
MSXML effectively does a 'strip-space' during parse time, so the xslt
processor side of it doesn't ever get to see this kind of whitespace,
rendering these two commands useless.
</note> 


The joys of whitespace...

cheers
andrew





-----Original Message-----
From: subbu@xxxxxxxxxxxx [mailto:subbu@xxxxxxxxxxxx]
Sent: 16 July 2002 13:34
To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
Subject:  regarding strip-space



hi
I read in the XSLT Reference that xsl:strip-space would actually remove
the 
whitespace-only text nodes from the source.
My question is ..( sorry its a bit weird)
1.what is a whitespace-only node?? ( is it an empty element of kind 
<myel></myel>)??

2.If i have empty elements like what i have shown above(<myel></myel>)
and if i have to pick all the myel elements which have some text in it (
non 
empty ) then , i beleive the only way to do it is through a condition
<xsl:for-each select = //myel[not(string-length(.) = 0)]">
is it true??

or are there any better ways to do it?

Subbu


--------------------------------------------------------------
Sent with "Me-Mail", Boltblue's FREE mobile messaging service.
http://www.boltblue.com


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





---
Incoming mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.373 / Virus Database: 208 - Release Date: 01/07/2002
 

---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.373 / Virus Database: 208 - Release Date: 01/07/2002
 

 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.