File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes XML and Related Technologies and the fly likes Preserving whitespaces in attribute value during XSLT Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "Preserving whitespaces in attribute value during XSLT" Watch "Preserving whitespaces in attribute value during XSLT" New topic
Author

Preserving whitespaces in attribute value during XSLT

kapil Gupta
Ranch Hand

Joined: Dec 17, 2001
Posts: 89
Hi,
Am converting HTML to XML using java, XSLT. The HTML contains element having attribute value with white spaces, for example
<span attr1="There is double space here">some text</span>
The attr1 value contains two white spaces. When I convert this to Xml using XSLT, the extra white space is stripped.How can I preserve extra whitespaces for attribute value of an element.
Thanks,
Kapil
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18651
    
    8

The XML Recommendation is here. Read section 3.3.3 ("Attribute-Value Normalization") and you will see that an XML parser is required to do what you described there. There is nothing you can do about that.
kapil Gupta
Ranch Hand

Joined: Dec 17, 2001
Posts: 89
Thanks for the reply Paul. Does that mean there is no way to have two consecutive spaces in the attribute value (other than to implement custom implementation of parser)?
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18651
    
    8

Yes, that's what it means. And I wouldn't recommend anybody to write a non-compliant XML parser just to violate that rule. After all, you could modify your XML design to put text in text elements where it belongs.
kapil Gupta
Ranch Hand

Joined: Dec 17, 2001
Posts: 89
Hi Paul,
Am converting XML received from a third party application to Html for editing purpose and then converting it back to Xml. The xml is then returned to this thirdparty application. There are some custom attributes which have to be maintained during this transformation and these attribute may contain white spaces. I have no control over the structure of XML. Is there any way to preserve the spaces? I tested this with Xml Spy and it preserves the extra white spaces but not with XALAN.
Thanks,
Kapil
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18651
    
    8

You can't preserve the spaces if you use a compliant XML parser. Basically it was a bad design decision to put significant spaces like that into an attribute. It's not your fault that the parser does what it is supposed to do. If there's somebody in a position of authority who thinks it is a problem, they should act to get the XML design fixed.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Preserving whitespaces in attribute value during XSLT