• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Preserving whitespaces in attribute value during XSLT

 
kapil Gupta
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 21152
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 89
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 21152
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 89
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 21152
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic