0

Using Xml to parse an XML document in Google Apps Script : http://code.google.com/googleapps/appsscript/articles/XML_tutorial.html#HowItWorks

But this doesn't work (parse fails) if there is a colon in the element name. Even though it maybe the namespace, its a single namespace throughout the XML document.

<aws:elementname>...</aws:elementname>

Is this is an issue only with the google's Xml or is it generic ?

4
  • From the documentation I gather that it's namespace-aware. What's the question - whether XML allows colons in element names apart from namespaces?
    – user395760
    Nov 27, 2010 at 21:06
  • If google's Xml Class is namespace-aware then it should not have failed parsing. I took the same example "Oracle of Bacon" and added (prepended to each element) aws: to all elements and it failed.
    – anjanesh
    Nov 27, 2010 at 21:13
  • When you added aws: did you add a namespace declaration for it as well?
    – Don Roby
    Nov 28, 2010 at 11:29
  • This XML is actually Amazon's AWIS which starts with : <?xml version="1.0"?> <aws:UrlInfoResponse xmlns:aws="alexa.amazonaws.com/doc/2005-10-05/"><aws:Response xmlns:aws="awis.amazonaws.com/doc/…>
    – anjanesh
    Nov 29, 2010 at 5:08

3 Answers 3

1

Just don't send argument as true.

var oXML = Xml.parse(sXML, false);
var root = oXML.getElement();
var topElement = root.getElements("http://namespace-uri","topElement");
var childElement = topElement[0].getElements("http://namespace-uri","childElement");
0

It is definitely not a general issue. There certainly are XML parsers that handle namespaces.

I suspect it's a limitation of the tutorial code and that the google libraries actually can handle namespaces, but it's somewhat guesswork from looking at the API docs.

The tutorial code is using calls like

var movies = doc.html.head.getElements("movie");

which seems to be a non-namespace-aware version.

There is an overload of this method that takes a namespace URL as well, and which you might need to use if there's a namespace involved.

1
  • "There is an overload of this method that takes a namespace URL as well, and which you might need to use if there's a namespace involved" - This fails at the parse level. var oXML = Xml.parse(content);
    – anjanesh
    Nov 28, 2010 at 8:25
0

If you want to use some namespace, you have to first declare it. E.g.:

<root xmlns:aws="some-uri">
  <aws:elementname />
</root>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.