简体   繁体   English

Javascript XMLSerializer区分大小写

[英]Javascript XMLSerializer case sensitive

I'm generating a KML document in Javascript and i'm trying to use XMLSerializer to generate the XML file but it's generating all lower case tags even though i create the tags in capital in the DOM. 我正在使用Javascript生成一个KML文档,我正在尝试使用XMLSerializer来生成XML文件,但即使我在DOM中创建了大写标签,它也会生成所有小写标签。

Is it the DOM that mangles the capitalization or the XMLSerializer? 是DOM会破坏大小写还是XMLSerializer? Is there any way to get around it or am I missing something? 有没有办法解决它或我错过了什么? I've tried this in both Chrome and Firefox. 我在Chrome和Firefox上都试过这个。

The KML document is to be imported into Google Earth and it seems it doesn't accept lower case tags. KML文档将导入Google地球,似乎不接受小写标记。

The following works for me (preserving case) in FF 5 beta in an XHTML page: 以下在XHTML页面中为FF 5 beta中的(保留案例)工作:

   <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <title>test</title>
        <script type="text/javascript">
            function test() {
                var kml = document.getElementsByTagName("kml").item(0);
                window.alert (new XMLSerializer().serializeToString(kml));
            }
        </script>
    </head>
    <body onload="test()">
<kml id="kml" xmlns="http://www.opengis.net/kml/2.2">
  <Document>
    <name>KML Samples</name>
    <open>1</open>
    <description>samples</description>
    <Style id="downArrowIcon">
      <IconStyle>
        <Icon>
          <href>http://maps.google.com/mapfiles/kml/pal4/icon28.png</href>
        </Icon>
      </IconStyle>
    </Style>
  </Document>
</kml>
    </body>
</html>

Based on testing in FF4, the following will work: 基于FF4中的测试,以下内容将起作用:

  1. Use document.createElementNS ("http://www.opengis.net/kml/2.2", elementName) instead of document.createElement(elementName) . 使用document.createElementNS ("http://www.opengis.net/kml/2.2", elementName)而不是document.createElement(elementName)

  2. Use elt.appendChild (document.createTextNode (text)) instead of elt.innerHTML = text . 使用elt.appendChild (document.createTextNode (text))而不是elt.innerHTML = text

It doesn't matter if you add elements with capital letters, the DOM manages them always in lower case. 如果你用大写字母添加元素没关系,DOM总是以小写形式管理它们。 Just check it with firebug, you won't see uppercase tags. 只需用firebug检查它,你就不会看到大写标签。

In case your doctype is set to XHTML it even breaks standard compliance. 如果您的doctype设置为XHTML,它甚至会违反标准合规性。

in XHTML attributes and elements must be all lower-case 在XHTML中,属性和元素必须全部为小写

UPDATE: just checked following: 更新:刚刚检查过以下内容:

var test = document.createElement("DIV");
// test.outerHTML returns "<div></div>"

So already when you create the element, it's being parsed and converted to lowercase. 因此,当您创建元素时,它正在被解析并转换为小写。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM