Link to home
Start Free TrialLog in
Avatar of scm0sml
scm0sml

asked on

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

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?
Avatar of Gertone (Geert Bormans)
Gertone (Geert Bormans)
Flag of Belgium image

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!
Avatar of scm0sml
scm0sml

ASKER

so in english lol?
ASKER CERTIFIED SOLUTION
Avatar of Gertone (Geert Bormans)
Gertone (Geert Bormans)
Flag of Belgium image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of scm0sml

ASKER

is there any resource u recommend for converting the attributes to elements?
Avatar of scm0sml

ASKER

how would i go about implementing this ruby program?

i'm viewing the xml in xmlspy incase thats of any use.
I will show you over the weekend, if that is OK for you
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)