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 August 4th, 2010, 08:34 AM
Registered User
 
Join Date: Aug 2010
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Default Fixed File Output/New Line

I am reading in an XML file and writing out a fixed text file output.

After some invesigating, here is how I am going about this.

Please let me know if there is a better way.

I am writing out each field one at a time for the length specified for that field. The field will either get truncated or padded with spaces for it to equal the length specified. I am building each record that way for the specified fixed length.

For Ex.

<xsl:variable name="pad3" select="' '"/>
<xsl:variable name="pad9" select="' '"/>
<xsl:variable name="pad10" select="' '"/>
.
.
<xsl:variable name="newline">
<xsl:text>
</xsl:text>
</xsl:variable>
.
.
.
<xsl:template name="HDR">

<xsl:if test="./XML_FIeld1"><xsl:value-of select='substring(./XML_Field1, 1, 10)'/></xsl:if>
<xsl:value-of select="substring($pad10,(string-length(XML_Field1)+1))"/>

<xsl:if test="./XML_FIeld2"><xsl:value-of select='substring(./XML_Field2, 1, 3)'/></xsl:if>
<xsl:value-of select="substring($pad3,(string-length(XML_Field2)+1))"/>
.
.
.
<!-- Fill out remaining record with spaces -->
<xsl:value-of select="$pad9"/>

<xsl:value-of select="$newline"/>
<!-- line break in output file -->

</xsl:template>


In addition to asking if there is a better way, I would like to know how the new line works in XSLT. I usually get them after that newline command I put in (which I don't fully understand, lol) but sometimes I also get them after each field.

It looks like things change when you add text directly (not from the input XML) I still looking for a patterns, but since I found this site and it has been helpful, I thought I'd ask what the rules are for new line feeds.

Thanks!
RR
 
Old August 4th, 2010, 08:46 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

A simpler way of doing padding to a fixed length N is to do (substring(concat($X, $PAD), 1, $N) where $PAD is a string of N or more spaces.
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference
 
Old August 4th, 2010, 09:52 AM
Registered User
 
Join Date: Aug 2010
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks! that's a good one.

It looks like if you end a line with a hard coded value, you get a new line, but if you put something after the hard coded value, or wite a variable that has a value assigned to it, you do not get a new line.

<!--This way does a carriage return after the hard coded "A" -->
<xsl:value-of select="myJavaScript:GetCurrentDateTime()"/>A
<xsl:value-of select="$pad30"/>
<xsl:value-of select="$pad30"/>
<xsl:value-of select="$pad9"/>

<!--This way does not do a carriage return -->
<xsl:value-of select="myJavaScript:GetCurrentDateTime()"/>A<xsl:value-of select="$pad30"/>
<xsl:value-of select="$pad30"/>
<xsl:value-of select="$pad9"/>

<!--This way also does not do a carriage return -->
<xsl:variable name="FileType" select="'A'"/>

<xsl:value-of select="myJavaScript:GetCurrentDateTime()"/>
<xsl:value-of select="$FileType"/>
<xsl:value-of select="$pad30"/>
<xsl:value-of select="$pad30"/>
<xsl:value-of select="$pad9"/>


Still playing around with things.
Thanks again!
RR
 
Old August 4th, 2010, 10:05 AM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

Generally speaking there are two types of whitespace - significant and insignificant.

Whitespace between XSLT instructions is considered insignificant. e.g.:

Code:
<xsl:value-of select="test1"/>
<xsl:value-of select="test2"/>
The above contains three nodes, two <xsl:value-of> elements and a text node between them containing only whitespace (spaces, tabs and carriage returns).

Whitespace included within a text node, or within an XSLT <xsl:text> instruction is considered significant.

Code:
<xsl:value-of select="test1"/>A
<xsl:value-of select="test2"/>
The text node above contains three nodes again, two <xsl:value-of> elements and a text node. In this instance the text node now contains non-whitespace content, i.e. "A" + carriage return. Therefore the whitespace is considered significant.

For a better understanding of whitespace I can really recommend you get a hold of Michael Kay's XSLT 2.0 reference - it covers all of this very well.
__________________
/- Sam Judson : Wrox Technical Editor -/

Think before you post: What have you tried?
 
Old August 4th, 2010, 03:05 PM
Registered User
 
Join Date: Aug 2010
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Ok, thanks, that helps to clear things up. I think I understand it now, but will investigate the book further if gets confusing again.

I think you meant to say insignificant in the first example and not significant . I hope so at least, becuase it makes sense to me that way, ha ha.

Thanks again!
RR





Similar Threads
Thread Thread Starter Forum Replies Last Post
How to read file line by line in EVC++ iriskab Visual C++ 0 September 27th, 2006 01:39 PM
Reading line by line from a .txt file x_ray VB.NET 2002/2003 Basics 5 February 10th, 2006 01:55 PM
DTS Fixed Width File Import rochak SQL Server DTS 3 September 26th, 2005 09:12 AM
xsl:element not on its own line in output EstherMStrom XSLT 3 February 26th, 2005 06:08 AM
generating a fixed length export file cooky4 VB Databases Basics 3 October 27th, 2003 03:42 PM





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