PDA

View Full Version : how to select this xpath/xml record?



illuminum
4 Jun 2008, 2:58 PM
<choices>
<address>
<state>AK</state>
<state>AL</state>
<state>AR</state>
<state>AS</state>
<state>AZ</state>
<state>CA</state>
<state>CO</state>
<state>CT</state>
<state>DC</state>
<state>DE</state>
<state>FL</state>
<state>FM</state>
<state>GA</state>
<state>GU</state>
</address>
</choices>


How can I grab the repeating XML state node's inner text with XML reader? What combination of the 'record' and 'mapping' attributes would get the text value?

The following does not work:


var reader = new Ext.data.XmlReader(
{ record: 'address/state' },
[{name: 'foo', mapping: '.'}]
);

illuminum
4 Jun 2008, 3:20 PM
This does not work either, though it selects the rows it does not map the inner text.


var reader = new Ext.data.XmlReader(
{ record: 'address/state' },
[{name: 'state', mapping: ''}]
);


This also does not work, it does find the inner text but it selects only a single row


var reader = new Ext.data.XmlReader(
{ record: 'address' },
[{name: 'state', mapping: 'state'}]
);


Finally, this doesn't work either, even though it is apparently valid xpath as my first example (using '.') was also.


var reader = new Ext.data.XmlReader(
{ record: 'address/state' },
[{name: 'state', mapping: 'text()'}]
);


So, is this an ExtJS deficiency? Should I quit trying to do with with Ext and hack my solution or find a different/complementary lib? Or am I simply doing this wrong?

illuminum
4 Jun 2008, 3:27 PM
in selecting extjs. i should have picked something that doesn't try to mooch money for support, and doesn't have a deceptive license that camouflages itself under gnu but in reality attempts to usurp its derivates as its own.

Animal
5 Jun 2008, 12:46 AM
Oh, I was just about to dive in and help you with your seemingly random code. Oh well.

illuminum
8 Jun 2008, 8:47 AM
Here is the solution:


var reader = new Ext.data.XmlReader(
{ record: 'address/state' },
[{name: 'state', mapping: '/'}]
);

gbs1230
11 Oct 2009, 1:45 AM
I am not sure if there is something in ExtJS 3 that provides xpath functionality on documents that are not in the DOM but I am using the following function which seems to work on IE 8 and FF.

xPathSelect: function(elem,xpath) {
if(IE) {
xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
//xmlDoc.async=false;
//xmlDoc.load("xml/xprocdefs.xml");
xmlDoc = elem;
var nodes=xmlDoc.selectNodes(xpath)
var xmlText = serializeXML(nodes[0]);
debug(xmlText);

}
else {

var xmlDoc = elem;
var nsResolver = xmlDoc.createNSResolver( xmlDoc.ownerDocument == null ? xmlDoc.documentElement : xmlDoc.ownerDocument.documentElement);
//nsResolver = document.createNSResolver(node);
debug("nsResolver: " +nsResolver.lookupNamespaceURI("p"));
var headings = xmlDoc.evaluate(xpath, xmlDoc, nsResolver, XPathResult.UNORDERED_NODE_ITERATOR_TYPE , null);
debug("headings.resultType: " +headings.resultType);
var thisHeading = headings.iterateNext();
var xmlText = serializeXML(thisHeading);
debug(xmlText);
}
return xmlText;
},

I call this as follows which seems to work:

Ext.Ajax.request({
method: 'GET',
url: "xml/xprocdefs.xml",
success: function(xhr, options) {
var result = workflow.draw.xPathSelect(xhr.responseXML,xpath);
}
});

Roland.Bouman
3 Jan 2011, 3:26 PM
I realize I'm late to the party, but if anybody still needs full XPath capabilities for their XmlReader, I wrote one. Details and source are here on the Sencha User and Plugins forum:

http://www.sencha.com/forum/showthread.php?120011-Ext.ux.XPathReader-an-Ext.data.DataReader-with-true-XPath-1.0-support

I hope it helps.