473,761 Members | 5,839 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

unwanted blank lines in output when using xalan

Hello everyone

I am transforming an XML document to text, basically only outputting
a small portion of it. When I run the following XSLT via Xalan's
processor,
I get a bunch of unwanted blank lines in the output.
Here is a simplified XML and XSLT:
(Note the problem does not happen when testing in XMLSpy)

- - - - - - - - - - - - - - - - - - - - - - - -
....
<AAA>
<anne anneId="blah" annName="blah"/>
<anne anneId="blah" annName="blah"/>
</AAA>
<BBB>
junk
</BBB>
<CCC>
junk
</CCC>
....
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- - -
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE stylesheet [
<!ENTITY cr "<xsl:text>
</xsl:text>">
]>

<xsl:styleshe et version="2.0"
xmlns:xsl="http ://www.w3.org/1999/XSL/Transform">

<xsl:output method="text" version="1.0" encoding="UTF-8" />

<xsl:template match="/AAA/anne">
&cr;
<xsl:value-of select="@anneId "/> <xsl:text> </xsl:text>
<xsl:value-of select="@anneNa me"/>
</xsl:template>
</xsl:stylesheet>

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- - -

I end up with a vast segment of blank lines before and after my data.
It is as if every time the processor reads any element, it's default
behaviour is to output a blank line, and when it actually matches with
the elements I want, then it behaves fine. If I put in "do nothing"
matches for all the other top level elements, then the number of blank
lines
is cut down, but not entirely eliminated:

<xsl:template match="/BBB"></xsl:template>
<xsl:template match="/CCC"></xsl:template>

NOTE: The "cr" entity is necessary to put *desired* blank lines between
my
lines of output data, and it shouldn't be triggered unless I match on
/AAA/anne right?

Does anyone know why I get these blanks, and how I can totally
eliminate them?

thanks,
Jeff

Feb 7 '06 #1
3 3703
Remember, whitespace in the source document is a legitimate part of the
document!

Your stylesheet provides an explicit rule for template for the AAA/anne
nodes... but it doesn't provide a template for the root element
(match="/"), which is where processing starts. That means your
stylesheet starts by applying the built-in default templates:

<xsl:template match="*|/">
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="text()|@ *">
<xsl:value-of select="."/>
</xsl:template>

So, given your input document:

<doc>
<AAA>
<anne anneId="blah" annName="blah"/>
<anne anneId="blah" annName="blah"/>
</AAA>
<BBB>
junk
</BBB>
<CCC>
junk
</CCC>
</doc>

We first match the root node as "/". The built-in template processes
this by running apply-templates against its the children.

That takes us to <doc>. It matches as "*", so we do another level of
apply-templates against the children.

The first child of <doc> is the whitespace text node between <doc> and
<AAA>. This matches as text(), and we output its value -- a newline.

The next child is <AAA>. That matches as "*", so we start applying
templates to its content.

The next child is whitespace again -- between <AAA> and <anne> -- so we
output it. That's another blank line.

And so on. Basically, because you're letting the defaults run, you're
going to get most of the text content of the source document, including
all its newlines.

The best fix is to write your own match="/" template which takes you
more directly to what you want to process. If all you care about is
those <anne> elements, I'd suggest you try adding this to override the
builtin default and take you right to the nodes you want to see:

<xsl:template match="/">
<xsl:apply-templates select="/AAA/anne">
</xsl:template>

By the way, note that your template for the <anne> elements can just say
match="anne". You only need to say "/AAA/anne" if you need to
distinguish these from <anne> elements that might be encountered
elsewhere in the document.
Feb 8 '06 #2

Joe Kesselman wrote:
Remember, whitespace in the source document is a legitimate part of the
document!
[snip]

Thanks, Joe! That was a very informative discussion, and the result
works
great!
<xsl:template match="/">
<xsl:apply-templates select="/AAA/anne">
</xsl:template>

By the way, note that your template for the <anne> elements can just say
match="anne". You only need to say "/AAA/anne" if you need to
distinguish these from <anne> elements that might be encountered
elsewhere in the document.


I couldn't get it to work if I just specified the final element "anne"
(I suppose I could
do "//anne", but anyway the full path works, and I had the idea that
doing it that way
saved the XSLT processor from having to search through the DOM tree...

--Jeff

Feb 8 '06 #3
>>By the way, note that your template for the <anne> elements can just say
match="anne ". You only need to say "/AAA/anne" if you need to
distinguish these from <anne> elements that might be encountered
elsewhere in the document.
.... I had the idea that doing it that way
saved the XSLT processor from having to search through the DOM tree...


Nope. If anything, you've added work, because now the processor has to
check that the <anne> element's parent is an <AAA>.

Select does the search. Match confirms that you've found the ones you're
interested in.
Feb 9 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
1944
by: Frank | last post by:
Hey all, I can't seem to get javascript running in my XSL document. <?xml version="1.0"?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0" xmlns:xalan="http://xml.apache.org/xalan" xmlns:my-ext="ext1" extension-element-prefixes="my-ext"> <!--The component and its script are in the xalan namespace and define the
3
2011
by: Brynjar Glesnes | last post by:
Hi, I've browsed the Fop FAQ, usenet and the internet hoping to find a solution to my problem, but in vain. I am using Xerces 2.6.2, Xalan-J 2.6.0 and Fop 0.20-5 to create PDF-documents. In the rendered PDF there is an unwanted whitespace after each xsl:value-of. An example: What I want to render to "Doe, John" renders to "Doe ,
3
1941
by: johkar | last post by:
What is the proper doctype syntax for XHTML transitional??? Strict? How come XSLT doesn't preserve XHTML when it is compiled (Xalan)? Meaning, <br /> tags become <br> <input /> tags become <input> etc. It seems I am dazed and confused? Any hints or clear online resources appreciated. John
3
411
by: Buz | last post by:
I am having a problem with extra, unwanted space showing up on my web form tables. I've tried hand coding and can get the htm file to display fine. But as soon as I bring the form into Visual Studio, convert it to an aspx web form, and insert my user controls, I start getting extra blank space between my rows, even when I give an explicit "height" attribute. The problem seems to occur with tables that have rowspans and/or user...
3
8596
by: yanakal | last post by:
Hi, I'm using isql to query data and output the same to a flat file. The isql has the following command options ' -h-1 -w500 -n -b -s"" '. In the SQL_CODE, the first two lines before the select statement are use dbname set nocount on go When I run this, an additional blank line is put into the output file. Actually, there are two lines after the last result set in the output file. This file is being fed into another system and the...
5
1296
by: Elhanan | last post by:
hi.. i don't exactly how to calls this , but is there a property for xalan, to output xml with crarriage return? i'm trying out xslt and the output can't be read with a normal editor, i know when serliaze xml with java, you can place a parameter saying the xml string will be output in human readble form.
2
3996
by: Olveres | last post by:
Hi, I have managed to work out how to add new lines into a calculated text box. However in this text box some of the outcome fields are empty however when previewing the report it includes the blank fields, so each section of the report is the same size, my field is set to can grow/shrink, but I think my inclusion in the code for the calculated box of all 15 outcomes (I have no choice) is what's causing each calculated box to be the...
0
889
by: ShapeMan | last post by:
Hi there, I'm writing a log file using File.AppendText in C# like so: swLog.WriteLine(msg); The file is closed and re-opened when another result is ready to be logged. In the output file I get the following: <Blank line> msg
6
3729
by: John Larson | last post by:
Hi All, I am some information from INSPEC database records in XML to build a relational database of my own. I am currently trying to extract information by doing an XSLT transform of the XML files into a tab-separated text file that I want to import into the database. I have run into the following problem: in some documents there are missing elements, for instance the volume and issue number of an article is not there (i.e. it is defined...
0
9377
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10136
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8814
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6640
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5266
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5405
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3913
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3509
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2788
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.