How to add <?xml-stylesheet to QDomDocument? - xslt

I've created an xml document with QDomDocument class. Now I want to add a link to a style-sheet. Is it possible with QDomDocument?
I couldn't identify a method for it in the documentation.

According to the API documentation it looks like document.insertBefore(document.createProcessingInstruction("xml-stylesheet", "type='text/xsl' href='sheet.xsl'"), document.documentElement()) should do.

Related

How to add multiple attributes in same element in XML file in QT 4.8

I am looking add multiple attributes in my XML file using QT 4.8. It will be something like this:-
<Communication xmlns="http://tempuri.org/communication.xsd" xmlns:xs="http://www.w3.org/2001/XMLSchema">
For this use QXmlStreamWriter xw and create an element by this line:-
xw.writeStartElement(fileID);
Now I try to add my attributes:-
xw.writeAttribute("xmlns=","http://tempuri.org/communication.xsd");
xw.writeAttribute("xmlns:xs=","http://www.w3.org/2001/XMLSchema");
but unfortunately writeAttribute function can only work only just after creating element. Please read here about this. So, I am getting my output like this:-
<Communication xmlns:xs="http://tempuri.org/communication.xsd">
So, I wonder how can multiple attribute be added in same element. Please help. Thanks.

Informatica XML target Add xmlns and xsi to root and add custom tag

I loaded an XML file via the "Import XML definition" and everything worked fine. But the XML needs the following added to it
Here is my question,
I just have the XML but not the xsd.
How do I add the xmlns and xsi string into the root node?
For the elements, how do I add the tag like 'common' and 'udf' before the column name like common:abbreviation or udf:name or udf:value?
Basically is there an easy and quick way to do this? Do I need to reimport the files with new xsd?
Thanks
<MyRoot
xmlns:udf="http://www.url.com/xx/XXXX/type1"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:common="http://www.url.com/xx/XXXX/common"
xmlns="http://www.url.com/ws/v410/NewPerson"
xsi:schemaLocation="http://www.url.com/xx/XXXX/NewPerson NewPerson.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
<common:ID>NNNNNNNNNN</common:ID>
<UDF>
<udf:name>Name Content</udf:name>
<udf:value></udf:value>
</UDF>
</MyRoot>
I've written a post about adding ports to XML transformation. See if this helps: http://powercenternotes.blogspot.com/2013/03/adding-port-to-existing-xml-parser.html

Adding/removing specific elements from xml file, in Qt?

I have a XML Document, like this:
<?xml version="1.0" encoding="UTF-8"?>
<items>
<item s_no="1">
<title>title_1</title>
<path>path1</path>
<desc>descriptoion1</desc>
</item>
<item s_no="2">
<title>title_2</title>
<path>path2</path>
<desc>descriptoion2</desc>
</item>
This is generated from QXmlStreamWriter in Qt. I want a function to add <item> tag with all elements like <title>, <path> etc. and I want a function to remove an item tag by identifying s_no attributes. All this should be done, without affecting any other content in the file.
I've searched a lot,I know there are similar questions, I've tried some code but it didn't worked. Are there any functions that do this, in QDomDocument?
When I have looked into doing this in the past, it hasn't really been a trivial thing.
QDomDocument and QDomNode
I think you should be able to do it with QDomDocument and QDomNode. Sometimes it is hard to see all the possible functions just on the main page for the documentation of the class, because it can get so much from the abstract classes it is derived from... clicking "lists of all members" shows a complete list.
http://doc.qt.io/qt-5/qdomdocument-members.html
Some calls that look promising include: childNodes elementById elementsByTagName createNode insertBefore insertAfter removeChild.
UPDATE: A working example that shows a straight forward way how to delete and insert nodes on a QDomDocument.
https://github.com/peteristhegreat/xml_insert_remove
Note, that when adding QDomNodes/QDomElements, etc, every element needs to be created on the document, otherwise it doesn't stay in scope when you leave a function.
QXmlStreamReader and QXmlStreamWriter
A few documents I've seen (a few years ago) said that they highly recommend using the QXmlStream* classes since they are better supported, or have been maintained more recently. I think it has some better error handling and doesn't have to load the whole document to be useful.
So as far as editing the document and resaving it, the most direct way that I know of is to read in everything, and store it as nested C++ classes and then write them out.
QJson Example (similar to QXmlStream*
There is a similar example with Json, that really shows off the power of subclassing a read and a write function into your model.
http://doc.qt.io/qt-5/qtcore-json-savegame-example.html
I think a similar approach could be done with the stream reader and writer class for XML.
Hope that helps.

Using <CustomVisualizer> tag with natvis

I am trying to make use of the tag which is defined in natvis.xsd (The natvis schema file) in order to write C# or C++ code to visualize a type. I cannot find any documentation on this, so I'm not sure how it works. In particular I need to specify a GUID for the CustomVisualizer. What does this GUID map to? How do I export the class that is registered with this GUID? What interface do I need to implement? In short, how do I connect the GUID that I specify in the .natvis file to the code that formats the object?
The documentation for natvis does not mention the existence of the CustomVisualizer tag, but it's in the natvis schema file and shows up in autocomplete. I found that PyTools appears to implement natvis Custom Visualizers using this method, but it seems some of the code is hidden, and I'm unable to locate the source for the actual native visualizer using this method.
Here is an example natvis file I would like to make work:
<?xml version="1.0" encoding="utf-8" ?>
<AutoVisualizer xmlns="http://schemas.microsoft.com/vstudio/debugger/natvis/2010">
<Type Name="MyCustomVisualizer">
<CustomVisualizer VisualizerId="{387446F9-4B29-4EE7-A948-346BF6995603}"/>
</Type>
</AutoVisualizer>
And I'm asking specifically about how to write the C# and/or C++ code to make this work.
https://code.msdn.microsoft.com/Writing-graphical-debugger-a17e3d75
You want to use a UIVisualizer, instead of a CustomVisualizer. Although CustomVisualizer appears in the schema, there appears to be no documentation about it. With the UIVisualizer, you get the inspector window in the watch view, which will launch the visualizer. The usage of the GUIDs related to the UIVisualizer are also discussed in that link.
Figured this out. You can see source at https://github.com/chromium/vs-chromium
TL;DR - There's a ton of work involved, I probably did some things wrong, and it's not easy.
I don't how use CustomVisualizer, but maybe visualizer that use LegacyAddin can solve your problem https://stackoverflow.com/a/11545420/61505

use javascript (or JQuery) in a standalone HTML file to select an XML and transform

I need a way to transform XML to HTML (using XSL) but without a server. So, I want to create a standalone HTML file (with hardcodes XSL path and name).
Allow the user to select an XML
Transform it with the XSL and display results in browser
Original XML cannot be changed (so cannot just embed XSL in XML)
Is this possible? Everything I found requires post, but I'm not using a server
Regards
Mark
Yes, it's possible. And you don't need javascript to do it, but you can use javascript if you want.
Just look at the previous (XSLT question)[https://stackoverflow.com/questions/12964917]
Use a processing-instruction like...
<?xml-stylesheet type="text/xsl" href="soccer.xslt"?>
Refer:
Direct linkage through pi: http://www.w3.org/TR/xml-stylesheet/
Transform through javascript:
http://dev.ektron.com/kb_article.aspx?id=482
Calling XSLT from javascript