Solved

Remove Version and encoding tag from xml using XSLT

Posted on 2008-10-28
9
4,038 Views
Last Modified: 2013-11-18
Remove Version and encoding tag from xml using XSLT. Even though I have this code in the XSLT, It does not seem to get rid of the xml-declaration.
<xsl:output method="xml" indent="no" omit-xml-declaration="yes" />

Please help.

Thanks,
Sundeep.

0
Comment
Question by:sundeepgopal
[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
  • 4
9 Comments
 
LVL 60

Expert Comment

by:Geert Bormans
ID: 22822361
So, that means that your XSLT process will be removing the XSLT whilst serialising.
But it could be that the serialisation takes place in the architecture you wrapped around the XSLT process.
How do you execute the XSLT process?
I assume that the serialisation process outside the XSLT feels a need to explicitely output the declaration, because it encounters some issues with the encoding, or simply because it always does.
Note that you can only leave the encoding out if the XML is to be serialised with an UTF-8 encoding
0
 
LVL 12

Expert Comment

by:jkmyoung
ID: 22822452
What processor are you using?
Do you have any template that copies a node? You may be reading in the processing instruction and copying it.

The starting part of your code would also help.
0
 
LVL 3

Author Comment

by:sundeepgopal
ID: 22822476
I XML serialized  an object in C#. Everytime I do that i end up with  this
<?xml version="1.0" encoding="utf-16"?> , which i really want to get rid off.  Is there any other way to get rid off it other than serializing the objects with utf-8 encoding?  
0
Technology Partners: 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!

 
LVL 60

Expert Comment

by:Geert Bormans
ID: 22822531
So, your C# is putting the encoding there, because of the encoding.
C# is fond of UTF-16.
If a document does not have an XML declaration, the encoding is defaulted to UTF-8
If you can't force the C# to serialise as UTF-8, it is impossible to drop the declaration, because someone has to tell the consumer of your data that the encoding is not UTF-8.
In short. If the encoding is not UTF-8, you NEED a declaration, full stop.
0
 
LVL 3

Author Comment

by:sundeepgopal
ID: 22822915
I changed the xml format UTF-8 . Now i have a string XML showing
<?xml version="1.0" encoding="utf-8"?>   but it is throwing  XMLExeception saying "data at the root level  1 is invalid".

Any ideas?

0
 
LVL 3

Author Comment

by:sundeepgopal
ID: 22822924
I changed the xml format UTF-8 . Now i have a string XML showing
<?xml version="1.0" encoding="utf-8"?>   but it is throwing  XMLExeception saying "data at the root level  1 is invalid"  when i try to XML read and do XSL transform.

Any ideas?
0
 
LVL 60

Expert Comment

by:Geert Bormans
ID: 22822952
Maybe it is wise to force the encoding to be UTF-8 in the XSLT as well, in order to make sure that the XSLT result  CAN be serialised as UTF-8 encoded (you migt get issues with the Byte Order MArk (BOM) if you start from a UTF-16 source
    <xsl:output indent="no" omit-xml-declaration="yes"  encoding="UTF-8"/>
0
 
LVL 60

Accepted Solution

by:
Geert Bormans earned 125 total points
ID: 22823007
Ah well, it sounds as if you just changed the encoding in a serialised source XML.
This you cannot do. If it is UTF-16, you just can say it is UTF-8... that would give you an error.
Leave the source as it is, but let teh XSLT make it UTF-8 and let teh C# source maintain the UTF-8 state,
by also telling in the C# code to set the output encoding to UTF-8
(these are settings in the C# serialisation, I don't have available, but the .Net resources on teh web explain that pretty well)
0
 
LVL 3

Author Comment

by:sundeepgopal
ID: 22823309
The issue is after serializing i get a "." infront of the <?xml version="1.0" encoding="utf-8 ?> there is some problem with the that . Here is the eroor when i try to load that string to xml document

System.Xml.XmlException: Data at the root level is invalid. Line 1, position 1.
   at System.Xml.XmlTextReaderImpl.Throw(Exception e)
   at System.Xml.XmlTextReaderImpl.Throw(String res, String arg)
   at System.Xml.XmlTextReaderImpl.ParseRootLevelWhitespace()
   at System.Xml.XmlTextReaderImpl.ParseDocumentContent()
   at System.Xml.XmlTextReaderImpl.Read()
   at System.Xml.XmlLoader.Load(XmlDocument doc, XmlReader reader, Boolean preserveWhitespace)
   at System.Xml.XmlDocument.Load(XmlReader reader)
   at System.Xml.XmlDocument.LoadXml(String xml)
0

Featured Post

Technology Partners: 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!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Word files to Wordpress Posts 2 73
Re-position the objects 7 126
Problem to page 4 100
Xsl-fo show PDF when no results are returned 2 23
I will show you how to create a ASP.NET Captcha control without using any HTTP HANDELRS or what so ever. you can easily plug it into your web pages. For Example a = 2 + 3 (where 2 and 3 are 2 random numbers) Session("Answer") = 5 then we…
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.
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)
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

697 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