XSLT output formatting using single quote instead of double quotes for attributes


Does anyone know how to force the use of single quotes (instead of double quotes) around attributes in the XSLT ouput e.g.:

<name age='30'>Bill</name>
instead of
<name age="30">Bill</name>

Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Gertone (Geert Bormans)Information ArchitectCommented:
That can't be done.
In pure XML terms that is not important (both forms are considered equal)
And basically the application using teh XML should not care as well.
So you have to wonder why you want that
The serialiser in your processor will pick whatever he wants, and mostly that will be double quote for most processors

If you really need it to be single quotes instead of double quotes,
you will have to post process the XML using some regular expression tools, but be carefull doing a "dumb" find and replace
since if there is a single quote inside the attribute value, you will need an &apos; replacement...
so the regulare expression post processor needs some XML intelligence

Why do you need it? Basically, you should not need this

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
DavidDunnAuthor Commented:
Thanks for the reply.

Yes, what you says it true. Unfortunately I do need it to be single quotes.

Basically the tool I am using stores an XML message in a variable. It runs C# script to call an external assembly and, in effect, does a find/replace of a variable name with the contents of the one stored - to give an example:
the tool holds the an message internally in it's own variable 'msg'. When the script is called the contents of msg are substituted for {msg}:

string xml;
xml = "{msg}";

if msg = <test a='1'/>, then no problem but <test a="1"/> would thow an error. Hope this makes sense.

Gertone (Geert Bormans)Information ArchitectCommented:
This makes sense indeed, but you should about tacking XML with non XML tools

So, as I suggested earlier, you could use some scripting language to replace the double quotes with single quotes
be carefull with that action, as I also said before.

Eventualy you could use XSLT do do the trick,
reconstruct the XML as text,
but I would not do that.
Are you familiar with languages such as Ruby, Python or OmniMark?
It would be really easy to make a filter for you in Ruby for instance

Or you make a pre filter in C# actually

Do you understand what the prefilter needs to do?

<foo bar="blabla"> should become <foo bar='blabla'>
<foo bar="bla'bla"> should become <foo bar='bla&apos;bla'>
C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

Hi Gertone,
I'm very interested to find out how you would do this with the C# prefilter that you mentioned, are you pre-filtering the message the is passed to the xmlserializer or xmlwriter?  

I'm actually not sure what you mean by prefilter.

Anyhow, to bring this question back onto topic, one other way you could do this (if you use saxon to do your transform and your not afraid to do a little java and recompiling) is to override the XMLEmitter class (XMLEmitter.java)

You would need to do two things that I can think of (there maybe more, but maybe someone else can add to this):
1.  override the writeAttribute method to output ' instead of " as the delimiter
2.  override the writeEscape method change this:

else if (c=='\"') {

else if (c=='\'') {

Then you need find in the code everywhere that XMLEmitter is being instantiated and replace it with your class and recompile saxon.  I haven't tried this myself, but it should be possible.  I think saxon is open source, so I think you are able to do this.

Hope this helps.
Gertone (Geert Bormans)Information ArchitectCommented:
I would have a filter operate on the serialised XML, one that replaces in a smart way all the attribute delimiters,
meaning that
1. each delimiter needs to be replaced from " into '
2. each ' inside attributes needs to be replaced by &apos;
That would be a post-filter to the XSLT or a pre-filter to the XML consuming process (if "pre" and post" filters are actually English words, I don't know , sorry for that)

Changing the way Saxon does the serialisation is an option, but 9.2PE and 9.2EE are not open source, so you are bound to 6.5.5 or 9.2HE. I suspect that since C# is mentioned, msxml is used and there is no .net version of 6.5.5 Saxon.
Problem with that approach is that you need to recompile every time there is a new Saxon, for your own special serialiser.
If I had the choice, I would simply manipulate the data after serialisation, but that is a matter of taste
Thanks Gertone for the clarification (I was mistakenly thinking that you meant pre-filtering the windows based message events and so thought you wanted to override the message between the application and the xmlserializer - sorry for the confusion).

I also think that you are correct in your approach of editting the pre-consumed xml and is much less complex than the method I suggested.

Hi DavidDunn,

Correct me if I'm wrong, but you always receive xml with double-quoted attribute value?
If so, than why not solve the problem like this:

string xml;
xml = '{msg}';

You can assign strings with single-quotes as outer most quotes aswell. Or am I far of the track now?

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Web Languages and Standards

From novice to tech pro — start learning today.