469,632 Members | 1,750 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,632 developers. It's quick & easy.

Help - I'm losing newline when performing XSL Transform

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

Oct 1 '05 #1
7 16589
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:output 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

--
Regards
Harrie
Oct 1 '05 #2
In article <11*********************@g14g2000cwa.googlegroups. com>,
jjouett <jj*****@necam.com> wrote:
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
Oct 1 '05 #3
That did it! I don't know why I didn't think of that. Thanks to
everyone for their response.

Oct 2 '05 #4
That did it! I don't know why I didn't think of that. Thanks to
everyone for their response.

Oct 2 '05 #5
Harrie said the following on 2005-10-02 00:16 +0200:
<xsl:output method="xml" indent="yes>


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

<xsl:output method="xml" indent="yes">

--
Regards
Harrie
Oct 2 '05 #6
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:output method="html" />, or maybe that's just my xml processor (xsltproc).

--
Regards
Harrie
Oct 2 '05 #7
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:output method="xml" indent="yes">


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

<xsl:output method="xml" indent="yes" />

I'll drink some more coffee now ..

--
Regards
Harrie
Oct 2 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Gordon Scott | last post: by
5 posts views Thread by Danny Anderson | last post: by
1 post views Thread by Rahul | last post: by
5 posts views Thread by Ward Germonpré | last post: by
3 posts views Thread by Simon Brooke | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.