[英]how to use QWebView of PyQt to display xml witl xslt applied
According to PythonCentral : 根据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
QWebView ...允许您显示URL,任意HTML, 带有XSLT样式表的XML,构造为QWebPages的网页以及其知道如何解释MIME类型的其他数据的网页
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. 但是,显示的xml内容就像被解释为html一样,也就是说,标签被过滤掉,并且显示的textnode没有换行符。
Question is: how do I show xml in QWebView with the xsl style sheet applied? 问题是:如何在应用了xsl样式表的QWebView中显示xml?
The same xml-file opened in any stand-alone webbrowser shows fine. 在任何独立的Web浏览器中打开的相同xml文件都可以正常显示。 The html-file resulted from the transformed xml (by lxml.etree) also displays well in QWebView.
转换后的xml(通过lxml.etree)生成的html文件也可以在QWebView中很好地显示。
Here is my (abbreviated) xml file: 这是我的(缩写)xml文件:
<?xml version='1.0' encoding='UTF-8'?>
<?xml-stylesheet type="text/xsl" href="../../page.xsl"?>
<specimen>
...
</specimen>
OK, I found part of the solution. 好的,我找到了解决方案的一部分。 It's a multi-step approach using QXmlQuery:
这是使用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. 显然,xslt是通过这种方式应用的。 What's still wrong is that the css style sheets referenced in the xslt are not applied/found.
仍然有问题的是,没有应用/找到xslt中引用的css样式表。
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.
我以为我也会将发现的解决方案发布到该问题上,因为它不需要QXmlQuery就可以工作,而且非常简单。
For my solution, my xml file was also interpreted as HTML, so I just worked with that and replaced every <
with <
对于我的解决方案,我的xml文件也被解释为HTML,因此我就使用了该文件,并用
<
替换了每个<
, every >
with >
,每个
>
>
and every &
with &
和每一个
&
与&
as mentioned in this answer. 如该答案中所述。
So, for your xmlString, just do: 因此,对于您的xmlString,只需执行以下操作:
xmlString.replace("<","<").replace(">",">").replace("&", "&")
This way, if your xml file gets interpreted as html it will at least show the text correctly with all the tags. 这样,如果您的xml文件被解释为html,它将至少正确显示带有所有标签的文本。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.