2013-10-16 48 views
4

我想用XPath解析xml文檔。XPath出錯。節點不能用於創建它的文檔以外的其他文檔

該腳本適用於Chrome瀏覽器就好了,但給我下面的錯誤:

WrongDocumentError: Node cannot be used in a document other than the one in which it was created

有問題的代碼如下:

function StringToXML(oString) { 
    //code for IE 
    if (window.ActiveXObject) { 
     var oXML = new ActiveXObject("Microsoft.XMLDOM"); oXML.loadXML(oString); 
     return oXML; 
    } 
    // code for Chrome, Safari, Firefox, Opera, etc. 
    else { 
     return (new DOMParser()).parseFromString(oString, "text/xml"); 
    } 
} 

function parseGpx(xmlText){ 
    var pointArray = new Array(); 
    var $xml = StringToXML(xmlText); 
    var path = "//*[local-name()='trkpt']"; 
    var paragraphCount = document.evaluate(path, $xml, null, XPathResult.ORDERED_NODE_ITERATOR_TYPE, null); 
    //this last line is what is causing the problems. 

我想知道什麼可能是這個問題,這是我的console.logs結果:

的console.log(XMLTEXT):

<?xml version="1.0" encoding="iso-8859-1"?> 
<gpx version="1.0" creator="http://www.bikecityguide.org" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns="http://www.topografix.com/GPX/1/0" 
    xsi:schemaLocation="http://www.topografix.com/GPX/1/0 http://www.topografix.com/GPX/1/0/gpx.xsd"> 
    <name></name> 
    <desc></desc> 
    <time>2013-10-10T17:54:09Z</time> 
    <bounds minlat="47.068677710511" minlon="15.423460100401" maxlat="47.074318908518" maxlon="15.431141947021"/> 
    <trk> 
     <trkseg> 
      <trkpt lon= "15.429682825318" lat="47.074318908518" /> 
      <trkpt lon= "15.428824518436" lat="47.071922203043" /> 
      <trkpt lon= "15.424404237975" lat="47.071366853467" /> 
      <trkpt lon= "15.423460100401" lat="47.068677710511" /> 
      <trkpt lon= "15.427107904664" lat="47.068970015231" /> 
      <trkpt lon= "15.428781603085" lat="47.070957644813" /> 
      <trkpt lon= "15.430755708927" lat="47.071016103385" /> 
      <trkpt lon= "15.430326555481" lat="47.072068346707" /> 
      <trkpt lon= "15.431141947021" lat="47.072068346707" /> 
      <trkpt lon= "15.430884454953" lat="47.074114315917" /> 
     </trkseg> 
    </trk> 
<wpt lon="15.429682825318" lat="47.074318908518"></wpt> 
<wpt lon="15.428824518436" lat="47.071922203043"></wpt> 
<wpt lon="15.424404237975" lat="47.071366853467"></wpt> 
<wpt lon="15.423460100401" lat="47.068677710511"></wpt> 
<wpt lon="15.427107904664" lat="47.068970015231"></wpt> 
<wpt lon="15.428781603085" lat="47.070957644813"></wpt> 
<wpt lon="15.430755708927" lat="47.071016103385"></wpt> 
<wpt lon="15.430326555481" lat="47.072068346707"></wpt> 
<wpt lon="15.431141947021" lat="47.072068346707"></wpt> 
<wpt lon="15.430884454953" lat="47.074114315917"></wpt> 

</gpx> 

的console.log($ XML):

URL 
    "http://localhost/trackeditor/" 

activeElement 
    gpx 

async 
    true 

baseURI 
    "http://localhost/trackeditor/" 

characterSet 
    "UTF-8" 

childNodes 
    NodeList[gpx] 

compatMode 
    "CSS1Compat" 

contentType 
    "text/xml" 

currentScript 
    null 

defaultView 
    null 

dir 
    "" 

doctype 
    null 

documentElement 
    gpx 

documentURI 
    "http://localhost/trackeditor/" 

firstChild 
    gpx 

hidden 
    true 

implementation 
    DOMImplementation { hasFeature=hasFeature(), createDocumentType=createDocumentType(), createDocument=createDocument(), more...} 

inputEncoding 
    "UTF-8" 

lastChild 
    gpx 

lastModified 
    "10/16/2013 13:15:31" 

lastStyleSheetSet 
    null 

localName 
    null 

location 
    null 

mozFullScreen 
    false 

mozFullScreenElement 
    null 

mozFullScreenEnabled 
    false 

mozHidden 
    true 

mozPointerLockElement 
    null 

mozVisibilityState 
    "hidden" 

namespaceURI 
    null 

nextSibling 
    null 

nodeName 
    "#document" 

nodeType 
    9 

nodeValue 
    null 

ownerDocument 
    null 

parentElement 
    null 

parentNode 
    null 

preferredStyleSheetSet 
    "" 

prefix 
    null 

previousSibling 
    null 

readyState 
    "complete" 

referrer 
    "" 

selectedStyleSheetSet 
    "" 

styleSheetSets 
    Object { length=0, item=item(), contains=contains()} 

styleSheets 
    Object { length=0, item=item()} 

textContent 
    null 

title 
    "" 

visibilityState 
    "hidden" 

__proto__ 
    XMLDocumentPrototype { ELEMENT_NODE=1, ATTRIBUTE_NODE=2, TEXT_NODE=3, more...} 

我很奇怪,爲什麼會發生這種事,幾乎所有的谷歌搜索結果這個問題是TinyMCE的關係,而不是非常有幫助。

回答

3

取而代之的是:

var paragraphCount = document.evaluate(path, $xml, null, 
         XPathResult.ORDERED_NODE_ITERATOR_TYPE, null); 

你應該使用:

var paragraphCount = $xml.evaluate(path, $xml, null, 
         XPathResult.ORDERED_NODE_ITERATOR_TYPE, null); 
相關問題