我正在制作一个UWP(通用Windows平台)应用程序。我试图将
.xml
和 .xsl
转换为 .html
文件以最终制作 pdf 文件,但是 XslCompiledTransform
在 Visual Studio 2022 上加载 .xsl
文件时抛出错误。我不知道在哪个上平台 XSLT 编译也不支持我现在所在的平台。我在Google上找不到相关信息。非常感谢任何帮助。
MainPage.xaml.cs
private async void ConvertXML2HTML(IStorageFile xmlFile, IStorageFile xslFile)
{
Windows.Storage.Streams.IRandomAccessStream xslStream = await xslFile.OpenAsync(Windows.Storage.FileAccessMode.Read);
System.IO.Stream xslStreamNet = xslStream.AsStream();
XmlReader xmlReaderXsl = System.Xml.XmlReader.Create(xslStreamNet);
XslCompiledTransform xslt = new XslCompiledTransform();
xslt.Load(xmlReaderXsl, settings, null); // This line throws
// System.PlatformNotSupportedException: 'Compilation of XSLT is not supported on this platform.'
}
包.appxmanifest
<?xml version="1.0" encoding="utf-8"?>
<Package
xmlns="http://schemas.microsoft.com/appx/manifest/foundation/windows10"
xmlns:mp="http://schemas.microsoft.com/appx/2014/phone/manifest"
xmlns:uap="http://schemas.microsoft.com/appx/manifest/uap/windows10"
xmlns:rescap="http://schemas.microsoft.com/appx/manifest/foundation/windows10/restrictedcapabilities"
IgnorableNamespaces="uap mp rescap">
...
<Dependencies>
<TargetDeviceFamily Name="Windows.Universal" MinVersion="10.0.0.0" MaxVersionTested="10.0.0.0" />
</Dependencies>
...
<Capabilities>
<Capability Name="internetClient" />
<rescap:Capability Name="broadFileSystemAccess" />
</Capabilities>
</Package>
上面代码中使用的xsl文件。
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<head>
<META http-equiv="X-UA-Compatible" content="IE=11" />
<title>document title</title>
<style type="text/css">
...
</style>
</head>
<body style="word-break:break-all;">
<table border="0" width="100%" style="center" >
<td align="center" style="vertical-align:top;">
<xsl:apply-templates/>
</td>
</table>
</body>
</html>
</xsl:template>
<xsl:template match="SomeString">
<xsl:if test="count(Text)!=0">
<xsl:call-template name="loop">
<xsl:with-param name="end_1" select="count(Text)" />
<xsl:with-param name="count_1" select="1" />
</xsl:call-template>
</xsl:if>
...
<xsl:template name="zero-padding">
<xsl:param name="str"/>
<xsl:choose>
<xsl:when test="$str!=''"><xsl:value-of select="number($str)"/></xsl:when>
<xsl:when test="$str=''">0</xsl:when>
</xsl:choose>
</xsl:template>
<xsl:template name="zero-padding2">
<xsl:param name="str"/>
<xsl:choose>
<xsl:when test="$str!=''"><xsl:value-of select="number($str)"/>.</xsl:when>
<xsl:when test="$str=''">0.</xsl:when>
</xsl:choose>
</xsl:template>
<xsl:template match="*">
<xsl:value-of select="." disable-output-escaping="yes" />
</xsl:template>
</xsl:stylesheet>
虽然我知道这不是 JScript 错误,但我尝试了下面的代码。没成功。
XslCompiledTransform xslt = new XslCompiledTransform();
var settings = new XsltSettings();
settings.EnableScript = true;
xslt.Load(xmlReaderXsl, settings, null);
您应该在 Windows.Data.Xml.Xsl 命名空间中使用类 XsltProcessor。在构造函数中传递对 XSLT 文件的引用。之后,调用方法 TransformToDocument 并向该方法传递对要转换的 XML 文件的引用。