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! |
Quote:
|
Quote:
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); |
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"); |
Quote:
My current code is like : Code:
xmlTextWriterStartPI(writer, "xml-stylesheet"); <?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! |
Okay, the next problem is that processing instructions don't have attributes, just arbitrary contents.
Code:
xmlTextWriterWriteAttribute(writer, "type","text/xsl"); |
Quote:
Thank-you v v much ntubski...!! |
All times are GMT -5. The time now is 11:00 AM. |