Reputation: 101
I am attempting to import a XML into Google Apps Script for parsing, and I am getting the following:
Exception: The prefix "xsi" for attribute "xsi:schemaLocation" associated with an element type "Document" is not bound.
This is the code I ran:
function combinePaths() {
//1. Get xml:
var earthFile = DriveApp.getFileById("1Pa994sHM-B8gAFxmrQk0Q3CWeQm1-I61");
//2. Get xml string from xml:
/*We must change the type of the file to pull it in as an editable set of data.*/
var earthFileName = earthFile.getName();
var earthBlob = earthFile.getAs("text/xml");
var earthString = earthBlob.getDataAsString();
var earthContent = XmlService.parse(earthString);
The error is thrown on the last line. The error seems to have something to do with the construction of the particular XML I am attempting to import. however upon comparison of an XML that doesn't have this error thrown and one that does, there are no differences at least in the order of the overarching structure of the document, i.e.:
<Xml><Document><name><description> </Xml></Document></name></description>
etc...
Has anyone run into anything similar? Are there any resources one can point me to do determine the meaning of this error any more clearly?
Upvotes: 0
Views: 79
Reputation: 25054
Yes, pretty much everyone who has used XML much in the last twenty years has run into something similar. There is a start-tag in the document of the form
<Document xsi:schemaLocation="...">
and it almost certainly appears at the very top of the file. But there is no in-scope namespace declaration binding the prefix xsi
to a namespace. So neither the parser nor any downstream app knows what namespace the 'schemaLocation' attribute is in; most XML parsers treat this as a fatal error.
Since 'xsi' is a conventional prefix for the XSD Instance namespace, and 'schemaLocation' is a frequently used attribute in that namespace, it is likely (but not completely certain) that the missing namespace declaration should have the form
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
Contact whoever is generating the XML document you are trying to consume, tell them their output is violating the rules of the XML namespaces spec, and ask them politely to fix it. If you are generating the XML document yourself, spend a little time searching for, reading, and understanding some tutorial information on XML namespaces and how to declare namespace prefixes. (Actually, you probably want to do this anyway, since your inability to interpret the error message signals that you have a gap in your XML knowledge where namespace declarations should be.)
On another topic ... At first glance your code appears to be asking to load the data to a variable as XML, and then asking that it be re-serialized and re-parsed. Is this re-serialization and re-parsing necessary? If so, the error might possibly lie with the serialization of the Blob as XML, and you might wish to report a bug in the serialization method.
Upvotes: 1