UWP:允许在WebView中进行XSL转换以获取本地内容 [英] UWP: allow XSL transformation in a WebView for local content

查看:136
本文介绍了UWP:允许在WebView中进行XSL转换以获取本地内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在WebView上显示本地XML文件. XML文件引用本地XSL文件(在同一文件夹中).但是没有应用样式表,其原因似乎是出于安全考虑.

I want to display a local XML file on a WebView. The XML file references a local XSL file (in the same folder). But the stylesheet is not applied and the reason for this seems to be security concerns.

如果我在Edge的控制台中打开文件,则会得到

If I open the file in the console of Edge I get

XSLT8690:系统无法找到指定的对象.

XSLT8690: The system cannot locate the object specified.

是否可以对本地文件进行XSL转换(例如,此处)?如何显示引用样式表的本地XML文件?

Is there a possibility to allow the XSL transformation of a local file (e.g. like here)? How can I display a local XML file, which references a stylesheet?

推荐答案

如果您的XML和XSL文件不在应用程序的资产文件夹中,则设置

If your XML and XSL file are not in application's assets folder, then setting the WebView.Navigate method or WebView.Source property to the XML file will not work. To solve this issue, we can use XsltProcessor class to transform xml according to the style sheet declaration specified in xslt file first and then using WebView.NavigateToString method to load the transformed HTML content.

例如:

var xmlFile = await ApplicationData.Current.LocalCacheFolder.GetFileAsync("test.xml");
var xsltFile = await ApplicationData.Current.LocalCacheFolder.GetFileAsync("test.xsl");

var doc = await Windows.Data.Xml.Dom.XmlDocument.LoadFromFileAsync(xmlFile);
var xsltDoc = await Windows.Data.Xml.Dom.XmlDocument.LoadFromFileAsync(xsltFile);

// Transform xml according to the style sheet declaration specified in xslt file
var xsltProcessor = new Windows.Data.Xml.Xsl.XsltProcessor(xsltDoc);
var transformedStr = xsltProcessor.TransformToString(doc);

//MyWebView is the WebView control in XAML
MyWebView.NavigateToString(transformedStr);


更新:

我收到错误从对COM组件的调用返回了HRESULT E_FAIL.在Windows.Data.Xml.Xsl.XsltProcessor.TransformToString(IXmlNod-e inputNode)中.

这里的问题是,在示例的XSL文件中,它正在使用XSLT document()函数.但是,出于安全原因,在UWP中禁止执行document()函数.因此,在执行XsltProcessor.TransformToString方法时会收到错误消息.

The problem here is that in your example's XSL file, it is using XSLT document() function. However for security reasons, execution of the document() function was prohibited in UWP. So you get the error when executing XsltProcessor.TransformToString method.

对于经典.NET Framework应用程序,我们可以设置 UserVoice 提交对此功能的请求> .

For classic .NET Framework apps, we can set XsltSettings.EnableDocumentFunction Property to enable support for the XSLT document() function. However there is no such class or property in UWP. You are welcome to submit a request for this feature through UserVoice.

对于您的特定XSL文件,它仅在两个位置使用了document()函数:

And for your specific XSL file, it only used document() function in two place:

<xsl:if test="$useexternalcss=1">
  <style type="text/css" media="screen">
    <xsl:value-of select="document($externalcssname)" />
  </style>
</xsl:if>
...
<p class="subtitle_create">
  <xsl:text>Angezeigt mit </xsl:text>
  <xsl:value-of select="document('')/xsl:stylesheet/@id" />
</p>

首先,因为它不使用外部CSS,所以不会执行document()函数.我们可以肯定地忽略它或将其注释掉.其次,使用document()函数获取stylesheetid,即"ELGA_Referenzstylesheet_1.04.009.1" ,我认为该值是一个常数.如果样式表未更改,则不会更改.因此,我认为您可以按以下方式进行更改:

For the first place, as it didn't use external css, so the document() function won't execute. We can ignore it or comment out it for sure. And in the second place, document() function is used to get the stylesheet's id which is "ELGA_Referenzstylesheet_1.04.009.1" and I think this value is a constant. It won't change if the stylesheet didn't change. So I think you can change it like following:

<p class="subtitle_create">
  <xsl:text>Angezeigt mit </xsl:text>
  <xsl:text>ELGA_Referenzstylesheet_1.04.009.1</xsl:text>
</p>

此后,您可以使用XsltProcessor.TransformToString方法重试,它应该可以工作.

After this, you can retry with XsltProcessor.TransformToString method, it should be able to work.

这篇关于UWP:允许在WebView中进行XSL转换以获取本地内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆