LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices



Reply
 
Search this Thread
Old 05-24-2012, 11:34 AM   #1
peacemission
LQ Newbie
 
Registered: May 2012
Location: india
Distribution: Ubuntu 11.10
Posts: 11

Rep: Reputation: Disabled
how to add xml-stylesheet tag in a XML File using libxml2 ?


Hi there..
This is my first post/question here.Please ignore if I've broken any rules of this forum as i'm new to this forum/LINUX.

I'd like to know how I could insert the special tag
<?xml-stylesheet type="text/xsl" href="XYZ.xslt"?>
in a document, just after the `<?xml version="1.0" ... ?>`.and before the Doc root element ??

Something like..

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="xyz.xslt"?>
<rootElement>
other stuff
</rootElement>

There are APIs like xmlTextWriterWriteElement(), xmlTextWriterWriteAttribute() etc...
but when i'm trying to create the xml-stylesheet tag before the root element, it throws an error which says-"Parse PI: PI xml-stylesheet never end".


I am using libxml2 version 2.7.8.

Thanks!
 
Old 05-24-2012, 08:44 PM   #2
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian
Posts: 2,541

Rep: Reputation: 878Reputation: 878Reputation: 878Reputation: 878Reputation: 878Reputation: 878Reputation: 878
Quote:
Originally Posted by peacemission View Post
There are APIs like xmlTextWriterWriteElement(), xmlTextWriterWriteAttribute() etc...
but when i'm trying to create the xml-stylesheet tag before the root element, it throws an error which says-"Parse PI: PI xml-stylesheet never end".
It's kind of hard to say what you've done wrong without seeing what you've done. It would be helpful to post the code. My guess from what you have said is that you need to call xmlTextWriterEndPI().

Last edited by ntubski; 05-24-2012 at 08:44 PM. Reason: extra, comma
 
Old 05-25-2012, 12:59 AM   #3
peacemission
LQ Newbie
 
Registered: May 2012
Location: india
Distribution: Ubuntu 11.10
Posts: 11

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by ntubski View Post
It's kind of hard to say what you've done wrong without seeing what you've done. It would be helpful to post the code. My guess from what you have said is that you need to call xmlTextWriterEndPI().
Thanks for responding..
Here is the code... (Though, this is not the full code, but i think it'll give you a fair idea what em doing.)
I think the red part of the code is causing the problem, but i donno what is the correct way of putting the <?xml-stylesheet> tag before the root element.

xmlTextWriterStartDocument(writer, NULL, MY_ENCODING, NULL);
xmlTextWriterStartElement(writer, "?xml-stylesheet");
xmlTextWriterWriteAttribute(writer, BAD_CAST "type",BAD_CAST "text/xsl");
xmlTextWriterWriteAttribute(writer, BAD_CAST "href",BAD_CAST "PMData.xslt");
xmlTextWriterEndElement(writer);

xmlTextWriterStartElement(writer, "ABCD"); [root element]
xmlTextWriterWriteAttribute(writer,"xmlns","xyz");
xmlTextWriterWriteAttribute(writer,"ZeroSuppression", "false");
xmlTextWriterStartElement(writer, "FileHeader");
xmlTextWriterWriteElement(writer, "Format", "123");
xmlTextWriterWriteElement(writer, "IPAddr", "ipaddr");
xmlTextWriterWriteElement(writer, "books", "linux");
xmlTextWriterWriteElement(writer, "BookName", "Linux Shell");
xmlTextWriterWriteElement(writer, "SenderSystemType", "0");
xmlTextWriterEndElement(writer);
xmlTextWriterEndElement(writer);
xmlTextWriterEndDocument(writer);
xmlFreeTextWriter(writer);

Last edited by peacemission; 05-25-2012 at 01:00 AM.
 
Old 05-25-2012, 10:22 AM   #4
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian
Posts: 2,541

Rep: Reputation: 878Reputation: 878Reputation: 878Reputation: 878Reputation: 878Reputation: 878Reputation: 878
I think the problem is that you should be using xmlTextWriterStartPI() instead of xmlTextWriterStartElement(). Otherwise, you're just writing an element with a "?" in its name.
Code:
    xmlTextWriterStartElement(writer, "?xml-stylesheet");
    xmlTextWriterStartPI(writer, "xml-stylesheet");
	xmlTextWriterWriteAttribute(writer, BAD_CAST "type",BAD_CAST "text/xsl");
	xmlTextWriterWriteAttribute(writer, BAD_CAST "href",BAD_CAST "PMData.xslt");
    xmlTextWriterEndElement(writer);
    xmlTextWriterEndPI(writer);
PS use [CODE][/CODE] tags to preserve indentation.
 
1 members found this post helpful.
Old 05-25-2012, 11:08 AM   #5
peacemission
LQ Newbie
 
Registered: May 2012
Location: india
Distribution: Ubuntu 11.10
Posts: 11

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by ntubski View Post
I think the problem is that you should be using xmlTextWriterStartPI() instead of xmlTextWriterStartElement(). Otherwise, you're just writing an element with a "?" in its name.
Code:
    xmlTextWriterStartElement(writer, "?xml-stylesheet");
    xmlTextWriterStartPI(writer, "xml-stylesheet");
	xmlTextWriterWriteAttribute(writer, BAD_CAST "type",BAD_CAST "text/xsl");
	xmlTextWriterWriteAttribute(writer, BAD_CAST "href",BAD_CAST "PMData.xslt");
    xmlTextWriterEndElement(writer);
    xmlTextWriterEndPI(writer);
PS use [CODE][/CODE] tags to preserve indentation.
Almost there....
My current code is like :
Code:
 xmlTextWriterStartPI(writer, "xml-stylesheet");
	xmlTextWriterWriteAttribute(writer, "type","text/xsl");
	xmlTextWriterWriteAttribute(writer, "href","PMData.xslt");
 xmlTextWriterEndPI(writer);
OUTPUT XML:
<?xml-stylesheet?> [no attributes ??]

Now, the problem is that ,above code is not generating any attributes (type = text/xsl and href=xyz).
I guess for generating attributes, i have to use some other APIs instead of xmlTextWriterWriteAttribute().
May be you can guide me a bit more
Thanks!
 
Old 05-25-2012, 07:29 PM   #6
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian
Posts: 2,541

Rep: Reputation: 878Reputation: 878Reputation: 878Reputation: 878Reputation: 878Reputation: 878Reputation: 878
Okay, the next problem is that processing instructions don't have attributes, just arbitrary contents.

Code:
	xmlTextWriterWriteAttribute(writer, "type","text/xsl");
	xmlTextWriterWriteAttribute(writer, "href","PMData.xslt");

	xmlTextWriterWriteString(writer, "type='text/xsl'");
	xmlTextWriterWriteString(writer, " ");
	xmlTextWriterWriteString(writer, "href='PMData.xslt'");
 
1 members found this post helpful.
Old 05-26-2012, 03:20 AM   #7
peacemission
LQ Newbie
 
Registered: May 2012
Location: india
Distribution: Ubuntu 11.10
Posts: 11

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by ntubski View Post
Okay, the next problem is that processing instructions don't have attributes, just arbitrary contents.

Code:
	xmlTextWriterWriteAttribute(writer, "type","text/xsl");
	xmlTextWriterWriteAttribute(writer, "href","PMData.xslt");

	xmlTextWriterWriteString(writer, "type='text/xsl'");
	xmlTextWriterWriteString(writer, " ");
	xmlTextWriterWriteString(writer, "href='PMData.xslt'");
Done!
Thank-you v v much ntubski...!!
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Adding new tag to an xml file using sed nano2 Linux - General 6 11-23-2011 06:47 AM
how to find the end tag in xml file. mariakumar Linux - General 1 12-21-2010 11:12 AM
[SOLVED] Is *libqt4-xml* equivalent of *libxml2* with respect to *XML* parsing ? TheIndependentAquarius Linux - Software 1 11-25-2010 08:34 PM
How To get the data from a tag in XML File kingmaker2003 Programming 7 12-05-2008 12:12 AM
nmap xml stylesheet lord-fu Linux - Software 1 08-06-2007 11:08 PM


All times are GMT -5. The time now is 11:46 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration