LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   how to add xml-stylesheet tag in a XML File using libxml2 ? (https://www.linuxquestions.org/questions/programming-9/how-to-add-xml-stylesheet-tag-in-a-xml-file-using-libxml2-946645/)

peacemission 05-24-2012 10:34 AM

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!

ntubski 05-24-2012 07:44 PM

Quote:

Originally Posted by peacemission (Post 4686471)
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().

peacemission 05-24-2012 11:59 PM

Quote:

Originally Posted by ntubski (Post 4686868)
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);

ntubski 05-25-2012 09:22 AM

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.

peacemission 05-25-2012 10:08 AM

Quote:

Originally Posted by ntubski (Post 4687419)
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!

ntubski 05-25-2012 06:29 PM

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'");


peacemission 05-26-2012 02:20 AM

Quote:

Originally Posted by ntubski (Post 4687788)
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...!!


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