Solved

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

Posted on 2006-10-21
7
327 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
  • 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 500 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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
Styling your websites can become very complex. Here I'll show how SASS can help you better organize, maintain and reuse your CSS code.
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
The viewer will receive an overview of the basics of CSS showing inline styles. In the head tags set up your style tags: (CODE) Reference the nav tag and set your properties.: (CODE) Set the reference for the UL element and styles for it to ensu…

911 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

Need Help in Real-Time?

Connect with top rated Experts

25 Experts available now in Live!

Get 1:1 Help Now