无法使用XSLT样式表将XML文件转换为HTML。

huangapple go评论88阅读模式
英文:

Cannot transform XML file to html using XSLT stylesheet

问题

client side - react js
server side - dot net

XSLT version - 2.0

hi, requirement is to transform an XML file to an HTML file using an XSLT stylesheet to display to the user on the client side. But the problem is I could not find a way to transform it properly.

What I tried so far,

  1. Tried linking the stylesheet in the XML file and opening it in the browser so that the transformation will be done by the browser automatically but this did not work as expected. In Chrome, it's just a blank window, and in Firefox, it displays the text with no styling. I also found out that browsers still do not support XSLT 2.0 transformation, so I assume that is the issue.

  2. Tried to transform it on the server side (.NET 7 / C#).

Above method did not give any error but no content in the resulting file. Later found out that XslCompiledTransform does not support XSLT 2.0; it only supports 1.0. So I tried a 3rd party library, Saxon-HE.

Above method gives an exception at the following line:

Could not find much related to this exception.

  1. Since transforming from the server-side doesn't look that promising at the moment, I moved back to client-side transformation. I am currently looking into Saxon-JS, but still no luck.

Anyone have an idea on how to go about this? Thanks.

英文:

client side - react js
server side - dot net

XSLT version - 2.0

hi, requirement is to transform an XML file to a html file using an XSLT stylesheet to display to the user in the client side. But problem is I could not find a way to transform it properly.

What I tried so far,

  1. Tried linking the stylesheet in the xml file and opening it in the browser so that the transformation will be done by the browser automatically but this did not work as expected. In chrome it's just a blank window and in firefox it displays the text with no styling. I also found out that browsers still do not support xslt 2.0 transformation so I assume that is the issue.

<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet type="text/xslt" href="stylesheet-ubl.xslt"?>
----------------------xml data--------------------------------

Above shows how I linked it. Tried both type="text/xslt" and type="text/xsl".

  1. Tried transform in the server side (.net 7 /c#).

    XslCompiledTransform transform = new XslCompiledTransform();
    using(XmlReader reader = XmlReader.Create(new StringReader(xsltString))) {
    transform.Load(reader);
    }
    StringWriter results = new StringWriter();
    using(XmlReader reader = XmlReader.Create(new StringReader(inputXml))) {
    transform.Transform(reader, null, results);
    }
    return results.ToString();

Above method did not give any error but no content in the resulting file. Later found out that XslCompiledTransform does not support XSLT 2.0, it only supports 1.0. So I tried a 3rd party library
Saxon-HE.

            var xslt = new FileInfo(@&quot;E:\xmltesting\stylesheet-ubl.xslt&quot;);
            var input = new FileInfo(@&quot;E:\xmltesting\invoice32.xml&quot;);
            var output = new FileInfo(@&quot;E:\xmltesting\test.html&quot;);

            var processor = new Processor();
            var compiler = processor.NewXsltCompiler();
            var executable = compiler.Compile(new Uri(xslt.FullName));

            var destination = new DomDestination();
            using (var inputStream = input.OpenRead())
            {
                var transformer = executable.Load();
                transformer.SetInputStream(inputStream, new Uri(input.DirectoryName));
                transformer.Run(destination);
            }

            destination.XmlDocument.Save(output.FullName);

Above method gives exception at below line,

var executable = compiler.Compile(new Uri(xslt.FullName));

System.TypeInitializationException: 'The type initializer for 'sun.util.calendar.ZoneInfoFile' threw an exception.'
Inner Exception
MissingMethodException: Method not found: 'Void System.IO.FileStream..ctor(System.String, System.IO.FileMode, System.Security.AccessControl.FileSystemRights, System.IO.FileShare, Int32, System.IO.FileOptions)'.

Could not find much related to this exception.

  1. Since transforming from the server-side doesn't look that promising atm moved back to client side transformation. I am currently looking into saxon-js...but still no luck.

Anyone have an idea on how to go about this?. Thanks.

答案1

得分: 1

Martin的回答已经展示了在.NET上使用Saxon来在服务器端运行转换的选项。

但您还询问了在浏览器中客户端运行转换的选项;对于这个问题,请查看SaxonJS。

英文:

Martin's answer has shown you the options for running the transformation server-side using Saxon on .NET.

But you also asked about the options for running the transformation client-side in the browser; for that, please take a look at SaxonJS.

答案2

得分: 0

I can help you with the Chinese translation. Here's the translated code portion:

如果您想要在.NET 7中运行XSLT 23样式表,您可以使用商业版的SaxonCS包https://www.nuget.org/packages/SaxonCS,最新版本为11.5和12.0),或者使用IKVM交叉编译版本的Saxon HE 11.5(https://www.nuget.org/packages/SaxonHE11s9apiExtensions);以下是在.NET 7中使用IKVM交叉编译的Saxon HE 11.5的示例代码:

using net.liberty_development.SaxonHE11s9apiExtensions;
using net.sf.saxon.s9api;

var processor = new Processor(false);

var xsltCompiler = processor.newXsltCompiler();

var xsltExecutable = xsltCompiler.Compile(new FileInfo("ubl.xslt"));

var xslt30Transformer = xsltExecutable.load30();

xslt30Transformer.Transform(new FileInfo("invoice-sample.xml"), processor.NewSerializer(new FileInfo("invoice-sample.html")));

I've translated the code while preserving the code-specific elements as requested.

英文:

If you want to run XSLT 2 or 3 stylesheets with .NET 7 you can do so using the commercial SaxonCS package (https://www.nuget.org/packages/SaxonCS, latest versions are 11.5 and 12.0) or using the IKVM cross compiled version of Saxon HE 11.5 (https://www.nuget.org/packages/SaxonHE11s9apiExtensions); the following is code using the IKVM cross compiled Saxon HE 11.5 in .NET 7:

using net.liberty_development.SaxonHE11s9apiExtensions;
using net.sf.saxon.s9api;

var processor = new Processor(false);

var xsltCompiler = processor.newXsltCompiler();

var xsltExecutable = xsltCompiler.Compile(new FileInfo(&quot;ubl.xslt&quot;));

var xslt30Transformer = xsltExecutable.load30();

xslt30Transformer.Transform(new FileInfo(&quot;invoice-sample.xml&quot;), processor.NewSerializer(new FileInfo(&quot;invoice-sample.html&quot;)));

huangapple
  • 本文由 发表于 2023年2月14日 19:18:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/75447054.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定