undefined:调用XSLTProcessor.prototype.importStylesheet时发生未定义的错误 [英] undefined: undefined error when calling XSLTProcessor.prototype.importStylesheet

查看:242
本文介绍了undefined:调用XSLTProcessor.prototype.importStylesheet时发生未定义的错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想美化一些XML,在以下答案中找到了以下代码(

I want to prettify some XML, and I found following code (in this answer, JSFiddle). I modified it like this:

const xsltDoc = new DOMParser().parseFromString([
    // describes how we want to modify the XML - indent everything
    '<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform">',
    '  <xsl:output omit-xml-declaration="yes" indent="yes"/>',
    '    <xsl:template match="node()|@*">',
    '      <xsl:copy><xsl:apply-templates select="node()|@*"/></xsl:copy>',
    '    </xsl:template>',
    '</xsl:stylesheet>',
].join('\n'), 'application/xml');
function prettifyXml(sourceXml) {
    var xmlDoc = new DOMParser().parseFromString(sourceXml, 'application/xml');
    var xsltProcessor = new XSLTProcessor();
    // Error happens here:
    xsltProcessor.importStylesheet(xsltDoc);
    var resultDoc = xsltProcessor.transformToDocument(xmlDoc);
    var resultXml = new XMLSerializer().serializeToString(resultDoc);
    return resultXml;
};

我在运行代码时遇到错误,但该错误没有消息.在Firefox控制台中看起来像这样:

I get an error running the code, but the error has no message. It looks like this in Firefox console:

这是我在调试器中看到的:

And this is what I see in debugger:

原始答案中的小提琴中也确实发生了错误.我想知道这是什么错误以及如何解决.

The error does also happen in the Fiddle from the original answer. I would like to know what kind of error is this and how to fix it.

我包含Firefox标签,因为我认为这不是正常的浏览器行为.我的版本是61.0.2(64位).

I am including Firefox tag, because I think this is not a normal browser behavior. My version is 61.0.2 (64-bit).

推荐答案

对于XSLT,您需要在样式表的根元素上具有version属性,因此请尝试<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">.这似乎可以解决importStylesheet调用上的错误,请参见 https://jsfiddle.net/sgeryvyu/361 /.

In terms of XSLT you need a version attribute on the root element of the stylesheet so try <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">. This seems to fix the error on the importStylesheet call, see https://jsfiddle.net/sgeryvyu/361/.

另一方面,已知Firefox/Mozilla的XSLT处理器可以进行树到树的转换,因此使用transformToDocument和Mozilla时,您将不会应用任何xsl:output的序列化选项,这意味着尝试推送DOM XSLT中的树只是为您提供了另一棵DOM树,而没有所需的缩进.

On the other hand, Firefox/Mozilla's XSLT processor is known to do a tree to tree transformation so with transformToDocument and Mozilla you won't get any serialization options of xsl:output applied, meaning that attempt to push your DOM tree through an XSLT simply gives you another DOM tree without the wanted indentation.

这篇关于undefined:调用XSLTProcessor.prototype.importStylesheet时发生未定义的错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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