[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

using <xsl:preserve-space elements="Client_Comment" /> is preserving font

Posted on 2006-10-21
7
Medium Priority
?
334 Views
Last Modified: 2013-12-03
a snippet of the code im using in an xsl stylesheet is:
<xsl:preserve-space elements="Client_Comment" />
    <xsl:output method="html"/>

and then:
<pre><xsl:value-of select="./@Client_Comment"/></pre>

I have done this to preserve carriage returns etc but it seems to be preserving the font and ignroing my style changes.

Is there anyway around this?
0
Comment
Question by:scm0sml
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
7 Comments
 
LVL 60

Expert Comment

by:Geert Bormans
ID: 17780014
Hi scm0sml,
> I have done this to preserve carriage returns

you can't preserve carriage returns in an attribute value.
According to the XML specification,
each XML attribute must be normalized before being sent to the application
Normalisation includes replacing each white-space character with a space
The carriage return is gone before it reaches the XSLT processor

If you want to maintain carriage returns you have to make the string element content

Cheers!
0
 

Author Comment

by:scm0sml
ID: 17780792
so in english lol?
0
 
LVL 60

Accepted Solution

by:
Geert Bormans earned 2000 total points
ID: 17780861
well,

the XML specification says that a parser must normalize an attribute
before sending it to an application
This means that the parser that gives the parsed XML to the XSLT processor
must do some normalization steps on the attribute value
one of these steps is that every white-space character (tab, carriage return, line-feed, space,...)
will be replaced bay a space
(for a reference read http://www.w3.org/TR/REC-xml/#AVNormalize)

this means that the XSLT processor will never know that a space in an attribute value,
used to be a carriage return,
so whatever you do in your XSLT program, you can't bring the carriage return back

that is why there is a
<xsl:preserve-space elements="..." />
and not
<xsl:preserve-space attributes="..." />
because that makes no sense

the spaces that remain are automatically preserved

So, if you really want to preserve the carriage-returns
you will have to put them in an element instead of an attribute

hope this now explains it well enough

I hope you have access to the source XML file
and you can change it at the source
If you need to convert the attributes to elements
you will have to do that using text processing techniques,
because if you use XML techniques (whatever, DOM, SAX, XSLT,...)
there will be a parser first, and the parser will normalize the attribute value
... back to square root one

I can write you a small Ruby program that does the transform for you,
if you want to

cheers

Geert

Geert
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:scm0sml
ID: 17796371
is there any resource u recommend for converting the attributes to elements?
0
 

Author Comment

by:scm0sml
ID: 17811401
how would i go about implementing this ruby program?

i'm viewing the xml in xmlspy incase thats of any use.
0
 
LVL 60

Expert Comment

by:Geert Bormans
ID: 17815876
I will show you over the weekend, if that is OK for you
0
 
LVL 60

Expert Comment

by:Geert Bormans
ID: 17850742
Hi,

switching machines took a bit longer than expected

I have a little Ruby program here that takes the attribute
and puts it in an element with the same name, immedeatly after the opening tag
You can now preserve the spaces in XSLT

what you should do
- download ruby 1.8 and install it (http://www.ruby-lang.org/en/downloads/)
- save the below snippet as a file attr2elem.rb
- run this command-line: ruby attr2elem.rb source.xml > result.xml

note that the source and result xml files can have other names

Here is what you can do then
- get the original XML, transform with the ruby program and then transform in a second step using XSLT with preserve spaces

The Ruby program:
#============
class ReplaceList
      def initialize
            @for_element = ""
      end
      def evaluate_tag_content(str)
            re = /((Client_Comment)=(["'])(.*?)\3)/m
            if md = re.match(str)
                  @for_element = "<" + md[2] + ">" + md[4] + "</" + md[2] + ">"
              "#{ unless md.pre_match.nil?; md.pre_match; else ''; end; }" +
                  "#{ unless md.post_match.nil?; md.post_match; else ''; end; }"
             else
                  @for_element = ""
                   str
            end
      end
      def extract_tags(str)
            re = /<([^>]+?)>/
            if md = re.match(str)
              "#{ unless md.pre_match.nil?; md.pre_match; else ''; end; }" +
                  '<' + evaluate_tag_content(md[1]) + '>' + @for_element +
                  "#{ unless md.post_match.nil?; extract_tags(md.post_match); else ''; end; }"
            end
      end
end

lines = Array.new
while line = gets
            lines << line
end

tagger = ReplaceList.new
puts tagger.extract_tags(lines.join)

0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

JavaScript has plenty of pieces of code people often just copy/paste from somewhere but never quite fully understand. Self-Executing functions are just one good example that I'll try to demystify here.
Without even knowing it, most of us are using web applications on a daily basis.  In fact, Gmail and Yahoo email, Twitter, Facebook, and eBay are used by most of us daily—and they are web applications. We generally confuse these web applications to…
HTML5 has deprecated a few of the older ways of showing media as well as offering up a new way to create games and animations. Audio, video, and canvas are just a few of the adjustments made between XHTML and HTML5. As we learned in our last micr…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

649 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question