Download presentation
Presentation is loading. Please wait.
Published byTrevor Hugo Hudson Modified over 9 years ago
1
1 Whitespace Handling Roger L. Costello XML Technologies
2
2 What is Whitespace? The following characters are considered to be whitespace characters: –space (#x020) –tab (#x09) –newline (#x0A) –carriage return (#x0D)
3
3 Whitespace-only Nodes Identify all of the whitespace-only nodes in this XML document: Text with surrounding whitespace Text with NO surrounding whitespace Text with embedded whitespace see whitespace-example01
4
4 Easy to identify in tree form Document / PI Element WhitespaceTest Element a Text "cr " Text " Text,,," Text "Text …" Text "Text …" Text " Text "cr " Element b Text "cr " Element c Text "cr " Element d Text "cr "... Note: cr = carriage return.
5
5 This XSL instruction tells the XSL Processor to "go to each child node and execute the template rule for the node" –The child nodes include the text nodes of course! Thus, if you write an identity transformation stylesheet then the output document will have the same indentation as the input document.
6
6 Strip whitespace-only nodes Problem: create a stylesheet which does an identity transformation, but it strips all indentation. Text with surrounding whitespace Text with NO surrounding whitespace Text with embedded whitespace Input: Text with surrounding whitespace... Output:
7
7 This XSL instruction tells the XSL Processor to "go to each child element node and execute the template for the node." Thus, this will result in skipping the text nodes that are giving the indentation. Will this give us the desired output? No! Output: In addition to stripping out the whitespace-only nodes (i.e., the indentation nodes), we have also stripped out the data.
8
8 This XSL instruction tells the XSL Processor to strip out all whitespace-only nodes in the XML document, prior to any processing. Thus, a stylesheet which does an identity transformation will yield an output document with no indentation. But there's one small problem...
9
9 We have element nodes that contain whitespace-only! Text with surrounding whitespace Text with NO surrounding whitespace Text with embedded whitespace The value of and is whitespace-only! So their values will get removed also. We don't want these values removed.
10
10 This XSL instruction has higher precedence than xsl:strip-space. It tells the XSL Processor "the space in elements and must be preserved".
11
11 xml:space="preserve" This is a standard XML attribute that you can add onto any XML element. This attribute instructs any application (e.g., an XSL Processor) that processes the element "the space in this element is to be preserved". This attribute has precedence over xsl:strip- space.
12
12 This stylesheet does an identity transformation, plus strips indentation <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
13
13 Notes about xsl:strip-space indicates that all whitespace-only text nodes should be stripped. indicates that the whitespace-only text nodes within,, and are to be stripped.
14
14 Note: only immediate children are stripped Element Foo Text "cr " Element bar Text "cr " Text "cr " Element gab Text "cr " will result in stripping only these two nodes: That is, only the immediate children are stripped.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.