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 13th, 2007, 05:09 AM
Authorized User
 
Join Date: Apr 2007
Posts: 18
Thanks: 1
Thanked 0 Times in 0 Posts
Default preserving whitespaces in attribute values?

Hi,
Am converting HTML to XML using java, XSLT. The HTML contains element having attribute value with white spaces, for example
<span name="There is double space here">some text</span>
The name value contains two white spaces. When I convert this to XSLT, the extra white space is stripped. I tried to use the
Code:
<xsl:preserve-space elements="*"/>
but this is not working.
Actually I want to preserve whitespace only for attribute values. How can I preserve extra whitespaces for attribute value for all elements.
Thanks,
Kapil
 
Old August 13th, 2007, 05:49 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

You haven't said how you are converting the HTML so it's hard to say where it's going wrong. Handling of whitespace in XML attributes depends on the DTD-defined attribute type - for attributes declared as CDATA (which is the default) newlines and tabs are converted to spaces, but multiple spaces aren't collapsed. For some other attribute types, for example ID and NMTOKENS, multiple spaces are collapsed to a single space. Perhaps this is happening because your attribute <span name="..."> is defined as an NMTOKENS attribute. It's not clear to me that an English sentence is an appropriate value of the "name" attribute.

xsl:preserve-space does something completely different. Please don't try to guess what a construct does from its name - look it up in the spec. Programming by trial and error will get you nowhere.

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old August 13th, 2007, 06:15 AM
Authorized User
 
Join Date: Apr 2007
Posts: 18
Thanks: 1
Thanked 0 Times in 0 Posts
Default

Thanks for your reply Michael.
I am converting HTML to XML by passing html string to java XALAN transformer. There is no DTD associated.
Actually I had used the name attribute just as an example. Am getting data in the form of XML from a third party application and I am converting it to HTML for user editing and then transforming back the HTML to XML. There are some attributes which I have to preserve during this round trip transformation. Some of these may have double white spaces as value. I have to preserve value of these attributes.
I tried this transformation using XML spy and it correctly preserves the white spaces but not with java based transformation.
Thanks,
Kapil

 
Old August 13th, 2007, 07:28 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

Then I think Xalan is getting it wrong. Raise a bug report - or try Saxon ;)

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old August 13th, 2007, 07:49 AM
Authorized User
 
Join Date: Apr 2007
Posts: 18
Thanks: 1
Thanked 0 Times in 0 Posts
Default

Could there be any alternate way to overcome this problem.
Thanks,
Kapil

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

I've given you two suggestions, how many more do you want?

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old August 13th, 2007, 08:39 AM
Authorized User
 
Join Date: Apr 2007
Posts: 18
Thanks: 1
Thanked 0 Times in 0 Posts
Default

Ok, I will try to use Saxon for java.
Thanks for the help Michael.

 
Old August 13th, 2007, 11:48 AM
Authorized User
 
Join Date: Apr 2007
Posts: 18
Thanks: 1
Thanked 0 Times in 0 Posts
Default

Michael, I read the specification
http://www.w3.org/TR/REC-xml/#AVNormalize and according to this the attribute value should not contain multiple spaces.
Does Saxon still convert multiple spaces as it is?
Thanks,
Kapil






Similar Threads
Thread Thread Starter Forum Replies Last Post
HOW TO: xlate attribute values? Philibuster XSLT 4 August 24th, 2006 12:17 PM
DOM Attribute name/values BrendonMelville XML 2 February 15th, 2006 06:05 PM
Access to attribute values from class of attribute jacob C# 1 October 28th, 2005 01:11 PM
how to get values depending on the attribute vidhya XSLT 1 July 8th, 2005 03:55 AM
different values with the same attribute name srini XSLT 0 January 21st, 2004 05:21 AM





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