Help - I'm losing newline when performing XSL Transform

J

jjouett

I'm trying to preserve the newline characters in my transformed XML
(read from a file) to provide a meaningful line number when validation
errors occur. However, my current method which performs an XSL
transform prior to validation is removing all the newlines. The XSL is
simply adding a namespace:


<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0"
xmlns="urn:RegisteredUser-schema">

<!-- This will just set the namespace of the topmost object -->
<xsl:template match="*">
<xsl:element name="{local-name()}">
<xsl:apply-templates select="@* | node()" />
</xsl:element>
</xsl:template>

<xsl:template match="@*">
<xsl:copy-of select="." />
</xsl:template>

</xsl:stylesheet>

The problem is that an input XML string which has newlines goes from:


<user>
<firstname>Joe</firstname>
<lastname>User</lastname>
</user>


to a string without newlines:


<user
xmlns="urn:RegisteredUserschema"><firstname>Joe</firstname><lastname>User</­lastname></user>



So any subsequent validation failure states that an error occurred on
line 1. I would like to be able to preserve the newlines in the
original string representation of the file, so that the error message
displayed to the user is meaningful.


Thanks in advance
 
H

Harrie

jjouett said the following on 2005-10-01 23:40 +0200:
I'm trying to preserve the newline characters in my transformed XML
(read from a file) to provide a meaningful line number when validation
errors occur. However, my current method which performs an XSL
transform prior to validation is removing all the newlines. The XSL is
simply adding a namespace:

I'm fairly new to XML and XSL(T), but have you tried adding:

<xsl:eek:utput method="xml" indent="yes>

... to your xsl file?

http://www.w3.org/TR/xslt#output

There are also some notes on white space handling for xsl and xml which might be of interest, but I think the indent attribute solves your newline problem:

http://www.w3.org/TR/xslt#strip
http://www.w3.org/TR/REC-xml/#sec-white-space
 
R

Richard Tobin

jjouett said:
However, my current method which performs an XSL
transform prior to validation is removing all the newlines.

Your stylesheet looks ok. What XSLT processor are you using?

-- Richard
 
J

jjouett

That did it! I don't know why I didn't think of that. Thanks to
everyone for their response.
 
J

jjouett

That did it! I don't know why I didn't think of that. Thanks to
everyone for their response.
 
H

Harrie

Harrie said the following on 2005-10-02 00:16 +0200:
<xsl:eek:utput method="xml" indent="yes>

To correct myself, I forgot the closing double quotes at the end, so it should read:

<xsl:eek:utput method="xml" indent="yes">
 
H

Harrie

jjouett said the following on 2005-10-02 04:45 +0200:
That did it! I don't know why I didn't think of that. Thanks to
everyone for their response.

I had the same problem some time ago, therefor I remembered that it defaults to not indent.

I'm not sure why it only preserves newlines and not indentation when <xsl:eek:utput method="html" />, or maybe that's just my xml processor (xsltproc).
 
H

Harrie

Harrie said the following on 2005-10-02 15:07 +0200:
Harrie said the following on 2005-10-02 00:16 +0200:

To correct myself, I forgot the closing double quotes at the end, so it
should read:

<xsl:eek:utput method="xml" indent="yes">

Bugger, it's an empty element, it should read:

<xsl:eek:utput method="xml" indent="yes" />

I'll drink some more coffee now ..
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top