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 25th, 2007, 10:29 AM
Friend of Wrox
 
Join Date: Jul 2006
Posts: 430
Thanks: 28
Thanked 5 Times in 5 Posts
Send a message via Yahoo to bonekrusher
Default Output to text

Hi,

I am building a javascript menu tree based on some XML and using XSLT with SAXON 8.8.

My XML is as follows:
Code:
</ACM:SEC1>
    <ACM:TITLE>DE-ICING PROGRAM <FARREF>121.689</FARREF></ACM:TITLE>
</ACM:SEC1>
By XSLT is:

Code:
                        <xsl:for-each select="/ACM:ROOT/ACM:CHAPTER/ACM:SEC1">
                            <xsl:variable name="section1">
                            <xsl:value-of select="ACM:TITLE/text()"/>
                            </xsl:variable>
                            <xsl:text>d3 = CreateTreeItem( d2, "img/project.gif", "img/folder_open.gif", "</xsl:text>
                            <xsl:value-of select="$section1"/>
                            <xsl:text>", "</xsl:text>
                            <xsl:value-of select="$section1"/>
                            <xsl:text>.xml", "main" )</xsl:text>;
                        </xsl:for-each>
                    </xsl:for-each>


Desired output =

Code:
d3 = CreateTreeItem( d2, "img/project.gif", "img/folder_open.gif", "DE-ICING PROGRAM", "DE-ICING PROGRAM.xml", "main" );
Current Output:

Code:
d3 = CreateTreeItem( d2, "img/project.gif", "img/folder_open.gif", "DE-ICING PROGRAM 
            ", "DE-ICING PROGRAM 
            .xml", "main" );


The problem is if there is an element child of <ACM:TITLE>, the text output places a line return. How can I avoid this?
 
Old November 25th, 2007, 10:55 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

You can call normalize-space() to get rid of the trailing spaces and newline, but I don't know what you want to do about the embedded space.

I think there must have been a newline before <FARREF> in your input for this effect to occur.

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old November 25th, 2007, 11:00 AM
Friend of Wrox
 
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

If you want to escape white space inside of JavaScript string literals then you can use the replace function XPath 2.0 offers: http://www.w3.org/TR/xpath-functions/#func-replace

So for instance replaces
  
Code:
replace('#10;', '\n', '\\n')

replaces each newline character with \n, the JavaScript escape sequence for newline.

 
Old November 25th, 2007, 11:14 AM
Friend of Wrox
 
Join Date: Jul 2006
Posts: 430
Thanks: 28
Thanked 5 Times in 5 Posts
Send a message via Yahoo to bonekrusher
Default

Thanks to both of you for your help.

Code:
<xsl:variable name="section1">
                                <xsl:value-of select="normalize-space(ACM:TITLE)"/>
                            </xsl:variable>


Works like a charm!

Thanks

 
Old November 25th, 2007, 01:06 PM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

Even better would be

<xsl:variable name="section1" select="normalize-space(ACM:TITLE)"/>

It's one line of code instead of three, and it defines the variable as a string rather than a result tree fragment, which is a much simpler object that is likely to require much less system overhead.

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference





Similar Threads
Thread Thread Starter Forum Replies Last Post
Trailing blanks for 'text' output Neal XSLT 5 May 29th, 2008 06:25 AM
TEXT OUTPUT IN WINDOWS suman_16 XSLT 1 January 24th, 2008 06:09 AM
XSL:output method="text" Pankaj C XSLT 4 August 2nd, 2007 10:05 AM
text output problems dextermagnific XSLT 1 August 10th, 2006 12:31 PM
Dumping StoredProc output to text File ao7711 SQL Server DTS 8 January 12th, 2006 04:41 PM





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