Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
|
XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old November 23rd, 2010, 06:12 AM
Authorized User
 
Join Date: Jul 2009
Posts: 23
Thanks: 5
Thanked 0 Times in 0 Posts
Default Removing whitespace from "text" output

Hi - I have an XSLT stylesheet that I use to preprocess some XML data and convert it to text (non-XML) data that is used in another program. I'd like to remove the whitespace characters including newlines, tabs, etc. from the text output but haven't been able to figure this out. I could change the code in my stylesheet to not have carriage returns between various lines of code and that would do it but that makes for rather unreadable code. I've tried adding the indent attribute as follows but that did not do much:

<xsl:output method="text" indent="no"/>

Any suggestions on how to remove whitespace characters globally from the text output of an XSLT transform? Thanks!
 
Old November 23rd, 2010, 06:44 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

The normalize-space() function will get you a long way. But you need to be more specific. If you really want to remove ALL whitespace you can use translate(., ' & #x9;& #xa; & #xc;', '') but that would be a rather unusual requirement.
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference
 
Old November 23rd, 2010, 07:09 AM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

Have you tried adding a <xsl:strip-space elements="*"/> to remove all unessential whitespace from the input XML.

If there are other specific examples you can give us we might be able to help further.
__________________
/- Sam Judson : Wrox Technical Editor -/

Think before you post: What have you tried?
 
Old November 23rd, 2010, 03:34 PM
Authorized User
 
Join Date: Jul 2009
Posts: 23
Thanks: 5
Thanked 0 Times in 0 Posts
Default

To be more specific, I have code that looks like the following:

Quote:
[<xsl:sequence select="something"></xsl:sequence>
[]
[["textstuff" []]<xsl:text> </xsl:text>]
[<xsl:for-each select="$varname/att"><some work here></xsl:for-each>]
[<xsl:for-each select="$varname2/att"><some more work here></xsl:for-each>]
]
My problem is that this code above when run, produces output that contains the newlines between the above lines. I don't want to get rid of these newlines in the code since that would make it tough to read but I do want the output string produced to be smaller in size. I'm running this via the Saxon .NET API and the memory consumption is rather high when the transform completes and passes back the string output. Hence the need to have some sort of a wrapper around the entire output produced to remove newlines/tabs. Essentially a search/replace on the whole string that results from the main part of the XSLT transform, but done within XSLT.
 
Old November 23rd, 2010, 04:56 PM
Authorized User
 
Join Date: Jul 2009
Posts: 23
Thanks: 5
Thanked 0 Times in 0 Posts
Default

Thinking about this some more, it appears that my problem would be solved if there is a way to invoke a stylesheet from within a stylesheet and assign the output of the invoked stylesheet (which would be text/string) to a variable. I could then apply normalize-space or translate on this variable. Not sure if there's a way to do this?
 
Old November 23rd, 2010, 06:30 PM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

You've got at least two alternatives in XSLT.

Firstly you could wrap the text in the text you want to output in <xsl:text> elements. Whitespace between the end tag of one <xsl:text> and the start of another might be ignored.

The reason for the newlines being left in is because whitespace next to a text node (not a <xsl:text> node) is considered significant, so newline followed by a square bracket the new line is kept.

If you wrap the square bracket in something else (i.e. a <xsl:text> element) then the newline on its own becomes insignificant and will be dropped.

Code:
[<xsl:sequence select="something"></xsl:sequence>
<xsl:text>[]</xsl:text>
<xsl:text>[["textstuff" []] ]</xsl:text>
<xsl:text>[</xsl:text><xsl:for-each select="$varname/att"><some work here></xsl:for-each><xsl:text>]</xsl:text>
...
Still going to make you XSLT harder to read but will work.

Alternatively you can do as you say above and store in a temporary variable and then process again (as you are using a XSLT 2.0 compliant xslt processor).

Code:
<xsl:variable name="temp">
[<xsl:sequence select="something"></xsl:sequence>
[]
[["textstuff" []]<xsl:text> </xsl:text>]
[<xsl:for-each select="$varname/att"><some work here></xsl:for-each>]
[<xsl:for-each select="$varname2/att"><some more work here></xsl:for-each>]
]
</xsl:variable>
<xsl:sequence select="normalize-space($temp)"/>
Normalize will only remove multiple whitespaces but will still leave a single space, so you may need to use translate instead.
__________________
/- Sam Judson : Wrox Technical Editor -/

Think before you post: What have you tried?
 
Old November 23rd, 2010, 09:31 PM
Authorized User
 
Join Date: Jul 2009
Posts: 23
Thanks: 5
Thanked 0 Times in 0 Posts
Default

Thanks for the suggestions. Unfortunately both those approaches require some heavy code change since the code I showed above is only a small part of my larger XSLT which involves multiple files and imports. The master XSLT file also applies several templates so I can't store all of that code in a variable. Hence, it would be ideal if there was a way to invoke an XSLT file from within an XSLT file. If not, I guess I'll have to make the changes based on your first suggestion above..
 
Old November 24th, 2010, 03:35 AM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

How about something like this:

Code:
<xsl:import href="main.xslt"/>
<xsl:variable name="doc" select="document('main.xml')"/>

<xsl:template name="main">
  <xsl:variable name="temp">
    <xsl:apply-templates select="$doc"/>
  </xsl:variable>
  <xsl:sequence select="normalize-spaces($temp)"/>
</xsl:template>
You'd need to call into this with the named template to kick it off.
__________________
/- Sam Judson : Wrox Technical Editor -/

Think before you post: What have you tried?
 
Old November 24th, 2010, 07:50 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

Capturing the output in a variable and post-processing it with a second transformation can only add to your memory problems rather than reducing them, so it doesn't seem the right way forward. Much better to avoid generating the unwanted whitespace in the first place.

I would go down the xsl:text route, despite the problems of stylesheet readability.
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference





Similar Threads
Thread Thread Starter Forum Replies Last Post
Difficulties with "web.config" and "ASPNETDB" CFRham BOOK: ASP.NET MVC Website Programming Problem Design Solution ISBN: 9780470410950 2 July 3rd, 2010 10:19 AM
Need code corrections "get sub or function not defined" error" bluesboytoo Excel VBA 1 October 22nd, 2009 11:12 PM
How to theme the "Browse" button of "FileUpload" control? varunbwj BOOK: Beginning ASP.NET 3.5 : in C# and VB BOOK ISBN: 978-0-470-18759-3 2 October 14th, 2009 01:22 AM
Add a CheckBox DataColumn to my DataGridView, Null format: "" or "True" but Error: F ismailc C# 2005 0 September 25th, 2009 04:56 AM
Code not going as planned: "icicle" vs "savedinstancestate" joopthecat BOOK: Professional Android Application Development ISBN: 978-0-470-34471-2 3 May 3rd, 2009 03:09 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.