[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
RE: MSXML XPath question
- From: "White, Solomon" <Solomon_White@amr-corp.com>
- To: 'Derek Denny-Brown' <derekdb@microsoft.com>,"White, Solomon" <Solomon_White@amr-corp.com>,"Xml-Dev Mailing List (E-mail)" <xml-dev@lists.xml.org>
- Date: Fri, 18 May 2001 16:52:16 -0600
Thanks a million!
This solution works, however, initially I get a one-celled table, so I
changed the initial selectNodes XPath to be:
/Index/document[starts-with(user,'')]
This matches everything, of course, so I get the table with rows and cells
again.
Thanks again!
Solomon
-----Original Message-----
From: Derek Denny-Brown [mailto:derekdb@microsoft.com]
Sent: Friday, May 18, 2001 4:24 PM
To: White, Solomon; Xml-Dev Mailing List (E-mail)
Subject: RE: MSXML XPath question
Rather than walking the children of the results from the selectNodes, you
should just iterate over the selection as a collection object.
I haven't tested this code, but it should work. Also note that I change for
your 'for' loop to a more efficient 'while' loop. For large datasets, using
the nextNode() method can be _much_ more efficient.
//add XML content to table
var oNode;
while ( null != (oNode = oDisplay.nextNode())) {
var oRow = tblXML.insertRow();
var oChildren = oNode.childNodes;
var oChild;
while ( null != (oChild = oChildren.nextNode())) {
var oCell = oRow.insertCell();
oCell.innerHTML = oChild.xml;
}
}
Derek Denny-Brown
-- Technical Lead: MSXML --
<http://msdn.microsoft.com/xml>
> -----Original Message-----
> From: White, Solomon [mailto:Solomon_White@amr-corp.com]
> Sent: Friday, May 18, 2001 12:56 PM
> To: Xml-Dev Mailing List (E-mail)
> Subject: MSXML XPath question
>
>
> Hello all--
>
> I have a question regarding the use of XPath in IE5+. What I
> am trying to
> do is build an html page that loads xml data from a file,
> then gives users
> the ability to filter/sort the data. The way I am attempting
> to go about
> this is as follows (see actual code below):
>
> 1) on page load, create MSXML2.DOMDocument object, load data,
> set selection
> language of object to XPath.
> 2) Select all nodes ("/") into a "display" object
> 3) Loop through display object, creating rows/cells in an HTML table
> 4) When the user specifies a filter, reset display object to have only
> selected records from original XML object, clear the table,
> then repeat step
> 3 to display only selected records.
>
> This all works except step 4. I do not get any errors, but
> all records are
> displayed instead of the selection. BTW, I tried my XPath
> query string in
> an XPath validation page, and it validates correctly.
>
> Any ideas?
>
> ------CODE SNIPPETS FOLLOW----
> //this code creates the necessary objects
> var oXML = new ActiveXObject("MSXML2.DOMDocument");
> oXML.async = false;
> oXML.validateOnParse=false;
> if (! oXML.load("test_small.xml")) {
> alert("unable to load test_small.xml");
> }
> oXML.setProperty("SelectionLanguage", "XPath");
> var oDisplay = oXML.selectNodes("/");
>
> function load_data() {
> //display all data in oDisplay in HTML table
> //empty the table
> while (tblXML.rows.length > 0) {
> tblXML.deleteRow();
> }
>
> //add XML content to table
> var root = oDisplay.context.firstChild;
> for (i=0; i<root.childNodes.length; i++) {
> var doc = root.childNodes(i);
> var oRow = tblXML.insertRow();
> for (j=0; j<doc.childNodes.length; j++) {
> var oCell = oRow.insertCell();
> oCell.innerHTML = doc.childNodes(j).xml;
> }
> }
> }
>
> function doFilter() {
> //oDisplay =
> oXML.selectNodes("/Index/document[starts-with(user,'J')]");
> oDisplay = oXML.selectNodes("/Index/document[user='Joe Blow']");
> load_data();
> }
>
>
> ------------------------------------------------------------------
> The xml-dev list is sponsored by XML.org, an initiative of OASIS
> <http://www.oasis-open.org>
>
> The list archives are at http://lists.xml.org/archives/xml-dev/
>
> To unsubscribe from this elist send a message with the single word
> "unsubscribe" in the body to: xml-dev-request@lists.xml.org
>