xsl:strip-space

only for RuBoard

xsl: strip-space

xsl:strip-space adds the text nodes to the list of nodes to be stripped if their content contains only spaces.

XSLT parsers remove text nodes from the constructed source tree if their content contains only spaces. They do not strip text nodes unless their content contains only spaces. The xsl: preserve-space element allows preserving those child text nodes so they are still contained in the source tree upon parsing.

The MSXML Parser requires loading the source document into a DOM tree before parsing. The act of loading the source into the DOM might strip significant white space before the stylesheet is rendered, so the xsl:preserve- space or xsl:strip-space elements might seem to have no effect. To preserve significant white space when initially loading the source tree into the DOM, set the preserveWhiteSpace property of the DomDocument object to True before loading the XML.

The .NET parser handles white space differently. A property of the XmlReader is WhiteSpaceHandling , which is one of the WhiteSpaceHandling enumeration members . You can also handle white space stripping/preservation at the node level by using XsltContext . Finally, you can control white space using the XmlDocument object's preserveWhitespace property. These techniques and objects are covered in more detail in Chapter 3.

Attributes

Attribute Name

Enforced

Description

Values

elements

Required

The list of elements that are to have text node children preserved if they contain only spaces.

NMTokens

Example

Consider the following document, source.xml :

 <?xml version="1.0" encoding="UTF-8" ?>  <links>       <link>     </link>       <link>             </link>       <link>                     </link>  </links> 

We experiment with different combinations of xsl:preserve-space to show the effects of each setting. We begin with a simple stylesheet that does not preserve any white space. This stylesheet is trans.xslt . The stylesheet simply displays the number of spaces for each node in the selected nodes:

 <?xml version="1.0" encoding="UTF-8" ?>  <xsl:stylesheet version="1.0"  xmlns:xsl="http://www.w3.org/1999/XSL/Transform">       <xsl:output method="xml" version="1.0" />       <xsl:template match="/">            <xsl:for-each select="links/link">                 <xsl:text>[</xsl:text>                 <xsl:value-of select="string-length(.)" />                 <xsl:text>]</xsl:text>           </xsl:for-each>       </xsl:template>  </xsl:stylesheet> 

To parse the result tree, you must first examine using the MSXML 4.0 Parser with Visual Basic 6:

 Option Explicit  Private Sub Command1_Click()      Dim xml As New MSXML2.DOMDocument40      Dim trans As New MSXML2.DOMDocument40      Dim fso As New Scripting.FileSystemObject      Dim strm As Scripting.TextStream      On Error GoTo eh      xml.Load "c:\temp\source.xml"      trans.Load "c:\temp\trans.xslt"      Set strm = fso.OpenTextFile("c:\outfile.txt", ForWriting, True)      strm.Write xml.transformNode(trans)      strm.Close      Set xml = Nothing      Set trans = Nothing      Exit Sub  eh:      Debug.Assert False      strm.Close      Set strm = Nothing      Set fso = Nothing      Set trans = Nothing      Set xml = Nothing  End Sub 

This results in the following output in the file outfile.txt :

 [1][1][1] 

To preserve white space, you need to preserve white space in the source XML DOM and within the stylsheet. To do this, alter the stylesheet to include the xsl:preserve-space element:

 <?xml version="1.0" encoding="UTF-8" ?>  <xsl:stylesheet version="1.0"  xmlns:xsl="http://www.w3.org/1999/XSL/Transform">         <xsl:preserve-space elements="link"/>       <xsl:output method="xml" version="1.0" />       <xsl:template match="/">            <xsl:for-each select="links/link">                 <xsl:text>[</xsl:text>                 <xsl:value-of select="string-length(.)" />                 <xsl:text>]</xsl:text>            </xsl:for-each>       </xsl:template>  </xsl:stylesheet> 

Also alter the code for MSXML by setting the preserveWhiteSpace property to True prior to loading the XML source:

 On Error GoTo eh  xml.preserveWhitespace = True  xml.Load "c:\temp\source.xml" 

This combination yields the expected result:

 [5][13][21] 

To achieve the same result in .NET, you again need to preserve white space in the parser prior to loading the document. You can achieve this by using an XmlDocument object to load the source XML and set the PreserveWhitespace property of the XmlDocument object to True:

 Sub Main()      Dim xsltReader As System.Xml.XmlTextReader = New graphics/ccc.gif System.Xml.XmlTextReader("c:\temp\trans.xslt")      Dim xsltdoc As System.Xml.XPath.XPathDocument = New graphics/ccc.gif System.Xml.XPath.XPathDocument(xsltReader, XmlSpace.Preserve)      Dim xmldoc As System.Xml.XmlDocument = New System.Xml.XmlDocument()      'Preserve whitespace and then load the xml source      xmldoc.PreserveWhitespace = True      xmldoc.Load("c:\temp\source.xml")      'Create the XslTransform object and load the stylesheet.      Dim xslt As XslTransform = New XslTransform()      xslt.Load(xsltdoc)      'Create an XmlTextWriter which outputs to the console.      Dim writer As XmlTextWriter = New XmlTextWriter(Console.Out)      'Transform the file and send the output to the console.      xslt.Transform(xmldoc, Nothing, writer)      writer.Close()  End Sub 

The output in the console window is the same as the previous result:

 [5][13][21] 

Parent Elements

 Xsl:stylesheet, xsl:transform 

Child Elements

None.

only for RuBoard


XML and ASP. NET
XML and ASP.NET
ISBN: B000H2MXOM
EAN: N/A
Year: 2005
Pages: 184

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net