 |
| 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
|
|
|
|

October 29th, 2006, 09:13 AM
|
|
Authorized User
|
|
Join Date: Oct 2006
Posts: 14
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
XSL transforming to TEXT
Hi all
Can someone help me?
For the below XML and XSL, I am trying to transform it to a TEXT file.
When doing so, I get the output as shown below:
My CD Collection
================
Title Artist
Empire Burlesque Bob Dylan
Hide your heart Bonnie Tyler
Greatest Hits Dolly Parton
Still got the blues Gary Moore
I want the list of Artist to be aligned in a straight line same as Title.
XML FILE
========
<?xml version="1.0" encoding="ISO-8859-1"?>
<catalog>
<cd>
<title>Empire Burlesque</title>
<artist>Bob Dylan</artist>
<country>USA</country>
<company>Columbia</company>
<price>10.90</price>
<year>1985</year>
</cd>
<cd>
<title>Hide your heart</title>
<artist>Bonnie Tyler</artist>
<country>UK</country>
<company>CBS Records</company>
<price>9.90</price>
<year>1988</year>
</cd>
<cd>
<title>Greatest Hits</title>
<artist>Dolly Parton</artist>
<country>USA</country>
<company>RCA</company>
<price>9.90</price>
<year>1982</year>
</cd>
<cd>
<title>Still got the blues</title>
<artist>Gary Moore</artist>
<country>UK</country>
<company>Virgin records</company>
<price>10.20</price>
<year>1990</year>
</cd>
</catalog>
XSL FILE
========
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method='text' version='1.0' encoding='UTF-8' indent='yes'/>
<xsl:template match="/">
<xsl:text>My CD Collection</xsl:text>
<xsl:text>#xD;</xsl:text>
<xsl:text>================</xsl:text>
<xsl:text>#xD;</xsl:text>
<xsl:text>#xD;</xsl:text>
<xsl:text>Title#x09;#x09;#x09;#x09;#x09;#x09;#x09; #x09;#x09;#x09;#x09;#x09;#x09;#x09;#x09;Artist
</xsl:text>
<xsl:text>#xD;</xsl:text>
<xsl:for-each select="catalog/cd">
<xsl:value-of select="title"/>
<xsl:text>#x09;#x09;#x09;#x09;#x09;#x09;#x09;#x09; #x09;#x09;#x09;#x09;#x09;#x09;#x09;</xsl:text>
<xsl:value-of select="artist"/>
<xsl:text>#xD;</xsl:text>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
================================================== =================
Thanks a lot!
Suri
|
|

October 29th, 2006, 09:25 AM
|
 |
Wrox Author
|
|
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
|
|
I wouldn't rely on tab characters for vertical alignment since tabs really don't have well-defined display semantics and you'll get different results depending on how the file is displayed. The simplest approach is to do
<xsl:value-of select="substring(concat(title, $spaces), 1, 20)"/>
where $spaces is a string containing 20 spaces (or however many you need).
Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
|
|

October 29th, 2006, 10:43 AM
|
|
Authorized User
|
|
Join Date: Oct 2006
Posts: 14
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
hi Michael
Thx for the immdt reply. I tried it, it works great.
Now, I have one more problem
I have to declare the variable in one single line like this,
<xsl:variable name = "spaces"><xsl:text>#32;#32;....30 times</xsl:text></xsl:variable>
If declared as below then, the output gets disturbed, why is that?
Please, help me out of this.
<xsl:variable name = "spaces">
<xsl:text>
#32;#32;....30 times
</xsl:text>
</xsl:variable>
Also, what does this do #32;?
and where can i find the different unicode similar to this?
thx
Suri
|
|

October 29th, 2006, 11:30 AM
|
 |
Wrox Author
|
|
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
|
|
The way to declare a variable as a string containing 10 spaces is like this:
<xsl:variable name="tenSpaces" select="' '"/>
If you want a variable containing forty spaces, I would write it as
<xsl:variable name="tenSpaces" select="concat($tenSpaces, $tenSpaces, $tenSpaces, $tenSpaces)"/>
You can write a space as &_#32; or &_#x20; if you want (leave out the underscore) but you don't need to. This is called a "character reference". It always uses the Unicode code for a character (in hex or in decimal). You can find a full list of Unicode code points at http://www.unicode.org/charts/
Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
|
|

October 29th, 2006, 11:32 AM
|
 |
Wrox Author
|
|
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
|
|
Oh, I didn't really answer your question. If you write
<xsl:variable name = "spaces">
<xsl:text>
#32;#32;....30 times
</xsl:text>
</xsl:variable>
then the xsl:text has some real spaces and newlines in it as well as the Unicode character references, and these real spaces and newlines are part of the variable's value. You could avoid this by changing it to:
<xsl:variable name = "spaces">
<xsl:text>#32;#32;....30 times</xsl:text>
</xsl:variable>
(Though the ampersands seem to have gone missing from that...)
Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
|
|

October 29th, 2006, 12:14 PM
|
|
Authorized User
|
|
Join Date: Oct 2006
Posts: 14
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Hi Michael
Thx alot...! That helped me.
Got two more questions.
a) How to put a page break? Like for the above xml I need to display only 5 Cds per page.
b) Is it possible to generate page nos, if so how?
Thanks a lot!
Suri
|
|

October 29th, 2006, 01:47 PM
|
 |
Wrox Author
|
|
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
|
|
(a) Not sure what you mean by a "Page break" in a text file. There's an ASCII character called "form feed" but I don't know whether it has any effect on modern systems, and it's not a permitted XML character so you can't generate it. If you want pagination, surely you want something better than text output?
(b) Try floor(position()-1 div 5)+1
Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
|
|

October 29th, 2006, 08:47 PM
|
|
Authorized User
|
|
Join Date: Oct 2006
Posts: 14
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Hi Michael
I was trying to analyse, how to generate a report for one of my customer.
They are not interseted to see the output in IE hence html was ruled out, So thought of
going with text output.
But the output needs to have page nos and the page header/footer. Again this
Page header/footer is not same on all pages.
So, Michael what do you suggest me? Can I still have these done to text file or
do you have a better solution?
Thanks alot!
Suri
|
|

October 30th, 2006, 05:02 AM
|
 |
Wrox Author
|
|
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
|
|
I can't advise you on requirements - that's between you and your customer. If they really want a paginated ASCII report in a fixed-width font, then you'll need to generate output with form-feed characters, and the only way to do that from XSLT is to generate some other character in place of the formfeed, and use a post-processing step (e.g. sed) to change the character.
Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
|
|
 |