BizTalk Utilities CV ,   Jobs ,   Code library  
 
Home Page
XSLT
The XSLT Example: Combining and intersecting two nodesets
The XSLT Example: Generating a new stylesheet
The XSLT Example: Numbering paragraphs and chapters
The XSLT Example: Creating listboxes and checkboxes using variables
The XSLT Example: Creating an HTML document with 'previous' and 'next' links
The XSLT Example: Generating a new stylesheet
The XSLT Example: Using different axes
The XSLT Example: Creating a summary of author sales for a publisher
The XSLT Example: Creating a summary of all Apollo flights
The XSLT Example:
The XSLT Element: xsl:with-param
The XSLT Element: xsl:when
The XSLT Element: xsl:variable
The XSLT Element: xsl:value-of
The XSLT Element: xsl:transform
The XSLT Element: xsl:text
The XSLT Element: xsl:template
The XSLT Element: xsl:stylesheet
The XSLT Element: xsl:strip-space
The XSLT Element: xsl:sort
<< XQuery
.NET and XML >>

By :Mark Wilson
I am the creator of TopXML. I am available for international and local (Australia) contracts. I am a Solution Architect/Business Analyst. I have worked in IT in several countries (NZ, Australia, South Africa, UK) building and training teams for government and very large non-governmental organizations. I am ex-Microsoft Consulting Services. I wrote the first book on Microsoft XML published in 2000 called XML Programming with VB and ASP. Most recently I have been building tools for the SEO industry. Ask me for a 37 point SEO health-checkup for your website.
First posted :03/24/2008
Times viewed :623

 
[XSLT Reference] [XPath Reference] [Download Reference Download this Reference]

Example: Whitespace preserving and stripping

Whitespace stripping is one of the more confusing subjects in XSLT programming. What is important to understand is at which moments whitespace can be inserted or removed. The process of creating a result document from a source document and a stylesheet document has three of these moments:
  1. Creating a tree structure in memory from the source document (some whitespace can get stripped)
  2. Creating a tree structure in memory from the stylesheet document (normally most whitespace is stripped)
  3. Creating a result document from the resulting tree structure in memory (some whitespace can be inserted)
The only whitespace that gets stripped are text nodes existing entirely of whitespace characters (space, tab, newline). If one non-whitespace character is included in the textnode, it will never be stripped.
In step 1, by default no whitespace is stripped. In step 2, all whitespace is stripped by default, except from xsl:text elements. In step 3, by default no whitespace is added.
To have whitespace stripped from the source document, you can place an xml:space="preserve" attribute in the source document. It will cause the descending whitespace nodes to be stripped. In the stylesheet, the xsl:strip-space and xsl:preserve-space top-level elements can be used to have certain whitespace nodes from the source document stripped or preserved.
The insertion of whitespace into the XML output document after building the result tree only happens when the xsl:output top-level element has its indent attribute set to yes. There is no way to specify whitespace insertion more exactly.
XSLT elements used:xsl:stylesheet xsl:template xsl:apply-templates xsl:preserve-space xsl:strip-space xsl:value-of
XML source:<?xml version="1.0"?> <book> <chapter > content <para /> </chapter> <chapter > <para /> <para ></para> <para > </para> <para > content </para> </chapter> </book>
XSLT code:<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> <xsl:strip-space elements="*"/> <xsl:preserve-space elements="para"/> <xsl:template match="/"> <root> <xsl:apply-templates select="book/chapter"/> </root> </xsl:template> <xsl:template match="chapter">chapter:[<xsl:apply-templates select="para|text()"/>]</xsl:template> <xsl:template match="text()">[<xsl:value-of select="."/>]</xsl:template> <xsl:template match="para">para:[<xsl:apply-templates select="text()"/>]</xsl:template> </xsl:stylesheet>
Resulting XML output:
saxon
xt
xalan
msxml3
msxml.NET
<?xml version="1.0" encoding="utf-8"?><root>chapter:[[ content ]para:[]]chapter:[para:[]para:[]para:[[ ]]para:[[ content ]]]</root><?xml version="1.0" encoding="utf-8"?> <root>chapter:[[ content ]para:[]]chapter:[para:[]para:[]para:[[ ]]para:[[ content ]]]</root><?xml version="1.0" encoding="UTF-8"?> <root>chapter:[[ content ]para:[]]chapter:[para:[]para:[]para:[[ ]]para:[[ content ]]]</root><?xml version="1.0" encoding="UTF-16"?><root>chapter:[[ content ]para:[]]chapter:[para:[]para:[]para:[[ ]]para:[[ content ]]]</root><?xml version="1.0" encoding="utf-8"?><root>chapter:[[ content ]para:[]]chapter:[para:[]para:[]para:[]para:[[ content ]]]</root>

Note: You can now download the full content of the XPath reference, the XSLT reference and the DOM reference in one PDF document. This document contains the implementation tables, internal links to navigate through the references, a full linked table of contents. You can use free text search through the whole reference or print as a nicely formatted document. Download here...

© 2000 Teun Duynstee. Shown on TopXML.com. Information used from XSLT and XPath recommendations © W3C and MSDN documentation © Microsoft.
Do you have comments, additions or suggestions, mail me.

Rate this article on a scale of 1 to 10

Your vote :  


 

Recent Jobs

Sr. Software Engineer - Analytics
Immediate Mainframe openings for Ch
Immediate TANDEM-TAL openings for C
Immediate ASP.NET/C# Openings for C
Sr. Software Engineer

View all Jobs (Add yours)
View all CV (Add yours)



teleconferencing service
water softener
Teleconference
Host Department NOLIMIT Web Hosting
MSN
sunglasses
fax.com


    Email TopXML  

Front Page Daily Stuff TopXML Forum XML blogs XML Newsgroups BizTalk Biztalk Utilities Biztalk Utilities Tutorial B2B SAP XML Microsoft .NET Dotnet System XML Soapformatter SQLXML XMLserializer XQuery PHP PHP SimpleXML PHP XML Dom PHP XML RPC PHP XSLT Java Java Java XML Xalan Microsoft ASP ASP Schemas XML SQL Server XML XMLDom XSL XSL Tutorial XSLT Stylesheets General Javascript CSS XHTML WAP