0

According to PythonCentral :

QWebView ... allows you to display web pages from URLs, arbitrary HTML, XML with XSLT stylesheets, web pages constructed as QWebPages, and other data whose MIME types it knows how to interpret

However, the xml contents are displayed as if it were interpreted as html, that is, the tags filtered away and the textnodes shown w/o line breaks.

Question is: how do I show xml in QWebView with the xsl style sheet applied?

The same xml-file opened in any stand-alone webbrowser shows fine. The html-file resulted from the transformed xml (by lxml.etree) also displays well in QWebView.

Here is my (abbreviated) xml file:

<?xml version='1.0' encoding='UTF-8'?>
<?xml-stylesheet type="text/xsl" href="../../page.xsl"?>
<specimen>
    ...
</specimen>
user508402
  • 496
  • 1
  • 4
  • 19

2 Answers2

0

OK, I found part of the solution. It's a multi-step approach using QXmlQuery:

path = base + "16000-16999/HF16019"

xml = os.path.join(path, "specimen.xml")
xsl = os.path.join(path, "../../page.xsl")

app = QApplication([])

query = QXmlQuery(QXmlQuery.XSLT20)
query.setFocus(QUrl("file:///" + xml));
query.setQuery(QUrl("file:///" + xsl));
out = query.evaluateToString();

win = QWebView()
win.setHtml(out);
win.show()
app.exec_()

Evidently, the xslt is applied this way. What's still wrong is that the css style sheets referenced in the xslt are not applied/found.

user508402
  • 496
  • 1
  • 4
  • 19
0

I came across your question because I had a similar problem. I thought I'd post the solution I found to the problem as well, because it works without QXmlQuery and is rather simple.

For my solution, my xml file was also interpreted as HTML, so I just worked with that and replaced every < with &lt;, every > with &gt; and every & with &amp; as mentioned in this answer.

So, for your xmlString, just do:

xmlString.replace("<","&lt;").replace(">","&gt;").replace("&", "&amp;")

This way, if your xml file gets interpreted as html it will at least show the text correctly with all the tags.

Freya W
  • 487
  • 3
  • 11