cancel
Showing results for 
Search instead for 
Did you mean: 

Error Processing XML Content with the JavaScript API

stevewickii
Champ in-the-making
Champ in-the-making
RhinoScript supports ECMAscript for XML (E4X).  I created a document under Company Home using DM Forms (XForms and an XML Schema definition for ProductApplication).  In the JavaScript file for my Web Script I am trying to select Documents under Company Home that are of type text/xml and have an element within the node's XML content set to "Some Application".

I am getting an error when I try to parse the content into an XML object with this statement: var doc = new XML(node.content);

The error I get is:  org.mozilla.javascript.EcmaError - TypeError: Cannot parse XML: The processing instruction target matching "[xX][mM][lL]" is not allowed. (AlfrescoScript#14)

node.content is:
<?xml version="1.0" encoding="UTF-8"?>
<tns:ProductApplication xmlns:alf="http://www.alfresco.org" xmlns:chiba="http://chiba.sourceforge.net/xforms" xmlns:ev="http://www.w3.org/2001/xml-events" xmlns:null="http://www.w3.org/2001/XMLSchema" xmlns:tns="http://www.example.org/ProductApplication" xmlns:xf="http://www.w3.org/2002/xforms" xmlns:xhtml="http://www.w3.org/1999/xhtml" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<tns:application>Some Application</tns:application>
<tns:product>&lt;p&gt;Some Product&lt;/p&gt;</tns:product>
<tns:crossSellRank>1</tns:crossSellRank>
</tns:ProductApplication>

If I remove the processing-instruction <?xml …?> it works fine.  I don't want to code around this problem.  I set XML.ignoreProcessingInstructions=true, but it still fails to execute with the same error.

I am using Alfresco Community 2.9B with the out-of-the-box configuration.
3 REPLIES 3

stevewickii
Champ in-the-making
Champ in-the-making
Here's why!

Exerpt from the Mozilla Developer Center (https://developer.mozilla.org/en/E4X)
E4X doesn't support parsing XML declaration ( <?xml version=…?>) (see bug 336551 ). You may get SyntaxError "xml is a reserved identifier" (despite the XML being in a string).

Workaround:
var response = xmlhttprequest.responseText; // bug 270553
response = response.replace(/^<\?xml\s+version\s*=\s*(["'])[^\1]+\1[^?]*\?>/, ""); // bug 336551
var e4x = new XML(response);

Alfresco Workaround:
var content = new XML(node.content.replaceAll("(?s)<\\?xml .*?\\?>\\s*", "")); // Workaround: Mozilla Bug 336551

Edit 11/2/2008 10:14PM: Be careful that you don't include any whitespace/newline characters before the first XML element in the node.content, otherwise new XML("\r\n<test>Test Successful</test>") will create a blank XML text node, which can be very confusing, but it's part of the E4X specification.

HI

I've used this to remove XML first line.
var content = new XML(node.content.replaceAll("(?s)<\\?xml .*?\\?>\\s*", ""));
It works fine.

Now I need to remove the xmlns form my tag.



<nfeProc xmlns="http://www.portalfiscal.inf.br/nfe" versao="2.00">
<NFe xmlns="http://www.portalfiscal.inf.br/nfe">




I found some clues on forums but nothing works for me.
Any help with this please?


stevewickii
Champ in-the-making
Champ in-the-making
Here are some great Tutorials and Documents on XML Processing with E4X (ECMAScript for XML aka JavaScript for XML)

W3Schools E4X Tutorial: http://www.w3schools.com/e4x/default.asp
DevX Article: http://www.devx.com/webdev/Article/33393/1763/page/1
E4X Specification: http://www.ecma-international.org/publications/files/ECMA-ST/Ecma-357.pdf