为什么XSLT在IE10中添加换行符(回车) [英] Why does XSLT add newline (carriage return) in IE10

查看:108
本文介绍了为什么XSLT在IE10中添加换行符(回车)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

据我所知,这只是从IE10开始的.

This just started with IE10 as far as I can tell.

在FF中这不是问题.

使用XMLHttpRequest获取一些xml数据,然后使用xslt显示它...

Using XMLHttpRequest to get some xml data, then display it using xslt...

xslt转换会在元素数据中似乎有空格的随机位置自动添加新行.

The xslt transformation is automatically adding new lines in seemingly random locations where there is a space in an elements data.

如果我使用getElementsByTagName预览数据,则数据是完整的,没有空格已转换为换行符.

If I preview the data using getElementsByTagName, the data is intact, no spaces have been converted to a newlines.

由于html将换行视为空格,因此,如果仅将文本显示或将其放置在输入框中(但将BUT)存储在隐藏元素中,并使用警报显示它,则不会看到该问题函数,您会看到输出数据中有换行符/回车符.

Since html treats new lines as a space, you can not see the issue if you simply display the text or place it in an input box, BUT, if the data is stored in a hidden element and you show it using the alert function you can see that there is a newline/carriage return in the output data.

如果原始xml数据中的元素后面没有回车符,则问题似乎更加严重.实际上,在某些情况下,我可以通过在每个元素后添加回车符来解决此问题.有些情况,并非全部.

The issue seems to be worse if there are NOT carriage returns after the elements in the original xml data. I've actually been able to get around this issue in some cases by adding a carriage return after each element. Some cases, not all.

几乎就像转换器在尝试读取数据时一样,它会将数据分割成多个空格以提高可读性,并自动添加回车符.

It's almost as if when the transformer is trying to read the data it splits the data at spaces for readability and it automatically adds a carriage return.

示例:

<!DOCTYPE html>
<html>
<head>
    <title>xslt test</title>
    <script type="text/javascript" language="javascript">

        function loadFile(f) {
            xhttp = new window.XMLHttpRequest
            xhttp.open("GET", f, false)
            xhttp.setRequestHeader("Cache-Control", "no-cache");
            xhttp.setRequestHeader("pragma", "no-cache");
            xhttp.send("")

            //in IE10 this seems to show where the new lines get added
            alert(xhttp.responseText)

            var xml = xhttp.responseXML

            displayData(xml, 'xsltTest.xslt', 'DataDiv')

        }

        function displayData(xmlResp, xslFile, targetObj) {

            var xml = new ActiveXObject("MSXML2.DomDocument");
            xml.async = false;
            xml.load(xmlResp);

            var xsl = new ActiveXObject("MSXML2.FreeThreadedDomDocument");
            xsl.async = false;
            xsl.load(xslFile);

            xsl_template = new ActiveXObject("Msxml2.XSLTemplate")
            xsl_template.stylesheet = xsl;

            xslProc = xsl_template.createProcessor()
            xslProc.input = xml

            xslProc.transform()
            document.getElementById(targetObj).innerHTML = xslProc.output
        }

        function popData(idx,e) {
            alert(document.getElementById('data_'+idx+'_'+e).value)
        }

    </script>
</head>
<body>
<center>
<input type="button" id="WithCRButton" value="Load File WITH CarriageReturns" onclick="loadFile('DataWithCR.xml')" />
<input type="button" id="WithoutCRButton" value="Load File WITHOUT CarriageReturns" onclick="loadFile('DataWithoutCR.xml')" />
<div style="border:1px solid black;width:100%" id="DataDiv">
</div>
</center>
</body>
</html>

xsltTest.xslt

<?xml version="1.0" encoding="iso-8859-1"?>

<xsl:stylesheet version="1.0"
   xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
   xmlns:xs="http://www.w3.org/2001/XMLSchema">


    <xsl:template match="/">
        <table>
            <xsl:for-each select="TestData/field">
                <tr>
                    <td>
                        <xsl:value-of select="elementData1"/>
                    </td>
                    <td>
                        <input type="hidden">
                            <xsl:attribute name="value"><xsl:value-of select="elementData1"/></xsl:attribute>
                            <xsl:attribute name="id">data_<xsl:value-of select="position()"/>_1</xsl:attribute>
                        </input>
                        <input type="button" value="show hidden value">
                            <xsl:attribute name="onclick">popData('<xsl:value-of select="position()"/>','1')</xsl:attribute>
                        </input>
                    </td>
                    <td>
                        <xsl:value-of select="elementData2"/>
                    </td>
                    <td>
                        <input type="hidden">
                            <xsl:attribute name="value"><xsl:value-of select="elementData2"/></xsl:attribute>
                            <xsl:attribute name="id">data_<xsl:value-of select="position()"/>_2</xsl:attribute>
                        </input>
                        <input type="button" value="show hidden value">
                            <xsl:attribute name="onclick">popData('<xsl:value-of select="position()"/>','2')</xsl:attribute>
                        </input>
                    </td>
                </tr>
            </xsl:for-each>
        </table>
    </xsl:template>

</xsl:stylesheet>

DataWithCR.xml

<TestData>
<field>
<elementData1>ThisHasNoSpaces</elementData1>
<elementData2>ThisHasA Space</elementData2>
</field>
<field>
<elementData1>ThisHasNoSpaces</elementData1>
<elementData2>ThisHasA Space</elementData2>
</field>
<field>
<elementData1>ThisHasNoSpaces</elementData1>
<elementData2>ThisHasA Space</elementData2>
</field>
<field>
<elementData1>ThisHasNoSpaces</elementData1>
<elementData2>ThisHasA Space</elementData2>
</field>
</TestData>

DataWithoutCR.xml

<TestData><field><elementData1>ThisHasNoSpaces</elementData1><elementData2>ThisHasA Space</elementData2></field><field><elementData1>ThisHasNoSpaces</elementData1><elementData2>ThisHasA Space</elementData2></field><field><elementData1>ThisHasNoSpaces</elementData1><elementData2>ThisHasA Space</elementData2></field><field><elementData1>ThisHasNoSpaces</elementData1><elementData2>ThisHasA Space</elementData2></field></TestData>

推荐答案

我发现,如果我使用"ActiveXObject("Msxml2.XMLHTTP")"而不是"window.XMLHttpRequest",那么问题就消失了.

I found that if I use "ActiveXObject("Msxml2.XMLHTTP")" instead of "window.XMLHttpRequest" the issue goes away.

Microsoft似乎刚刚实现了浏览器本机的"window.XMLHttpRequest" ...但它具有错误和限制.

It seems that Microsoft just implemented the browser-native "window.XMLHttpRequest" ... but it has bugs AND limitations.

例如,"ActiveXObject("Msxml2.XMLHTTP")"具有transformNode方法,而"window.XMLHttpRequest"则没有.

For example, "ActiveXObject("Msxml2.XMLHTTP")" has the transformNode method, but "window.XMLHttpRequest" does not.

为了在我的javascript代码中实现浏览器兼容性,我按如下方式创建对象:

For browser compatibility in my javascript code I was creating the object as follows:

var XMLDoc = (window.XMLHttpRequest) ? (new XMLHttpRequest()) : (new ActiveXObject("Microsoft.XMLHTTP"))

...因此在IE中,只要不存在window.XMLHttpRequest,它就在使用ActiveXObject.但是现在它确实存在,正在使用它,以及它的所有错误和局限性.

...so in IE, as long as window.XMLHttpRequest did not exist it was using the ActiveXObject. But now that it does exist it's using it, along with all it's bugs and limitations.

我现在正在逆转该条件语句的过程,只要有条件,它就会默认调用ActiveXObject ...

I am now in the process of reversing that conditional statement everywhere its called to default to the ActiveXObject if it's available...

var XMLDoc = (window.ActiveXObject) ? (new ActiveXObject("Msxml2.XMLHTTP")) : (new XMLHttpRequest())

这篇关于为什么XSLT在IE10中添加换行符(回车)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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