Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > XML > unwanted blank lines in output when using xalan

Reply
Thread Tools

unwanted blank lines in output when using xalan

 
 
Jeff Calico
Guest
Posts: n/a
 
      02-07-2006
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:stylesheet version="2.0"
xmlnssl="http://www.w3.org/1999/XSL/Transform">

<xslutput 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="@anneName"/>
</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

 
Reply With Quote
 
 
 
 
Joe Kesselman
Guest
Posts: n/a
 
      02-08-2006
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.
 
Reply With Quote
 
 
 
 
Jeff Calico
Guest
Posts: n/a
 
      02-08-2006

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

 
Reply With Quote
 
Joe Kesselman
Guest
Posts: n/a
 
      02-09-2006
>>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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Preserve blank lines when add multiple lines of text to a cell Cah Sableng Javascript 0 04-23-2007 04:46 AM
Hint: nullPointer in org.apache.xalan.xsltc.runtime.output.TransletOutputHandlerFactory.getSerializationHandler Meikel XML 0 01-17-2005 04:33 PM
Xalan transform output to string Rene van Hoek XML 1 11-23-2004 10:47 AM
Upgrade of Xalan 1.2.2 and Xerces 1.4.4 to Xalan 2.6 and Xerces 2.6.2 cvissy XML 0 11-16-2004 07:06 AM
XSL & XML newbie: How to get rid of blank lines in output?? ricky XML 1 10-17-2004 08:28 PM



Advertisments