XML node Query

Source: Internet
Author: User
Tags xml parser xsl
Analysis of browser support for XML

Although the XML parser in ie4.0 can do a lot of things, there are limits on it. First, there is no way to find a specified data in the data source. To discover the amount of data you are interested in, you must traverse the entire XML data tree and manually compare it. Second, these primary parser cannot provide any filtering function, so you have to manually filter data.

But the good news is that ie5.0 provides enhanced XML support to address these users. Ie5.0 integrates the parser into the browser, so that the two separated components are not required. In addition, the new query language allows you to query and filter XML data. In ie5.0, the query language supports matching by an XSL pattern. Ie5.0 also supports the XML data island concept. To use the XML Parser of ie5.0 in your web application, you only need to embed an XML data island in the web page, you can use the script to operate Dom interfaces such as ixmldomdocument.

In the XML data islands, you can traverse all sub-nodes and use the following VBScript statement:

<Script language = VBScript>
Sub traversechildnodes (nodelist)
For each node in nodelist
'Perform some operations on the node
If node. haschildnodes then
Traversechildnodes node. childnodes
End if
Next
End sub

Sub window_onload ()
Traversechildnodes xmldata. childnodes
End sub
</SCRIPT>

In ie5.0, two new Dom methods are added: selectnodes and selectsinglenode. Both methods use a pattern string as the unique input parameter. selectnodes returns a reference to ixmldomnodelist, while selectsinglenode returns a reference to ixmldomnode. The only difference between the two methods is that selectnodes returns all matched nodes, while selectsinglenode returns only the first node that matches the matching mode.

The following example shows how to use selectnodes to traverse all ordering items in XML data island:

<Script language = VBScript>
Sub traverseallorderitems (ordernode)
Set nodelist = ordernode. selectnodes ("// item ")
For each node in nodelist
'Here we can perform some processing on the node
Next
End sub

Sub window_onload ()
Traverseallorderitems xmldata.doc umentelement
End sub
</SCRIPT>

The string/item passed to the selectnodes method is an example of the pattern. In fact, you can call the selectnodes or selectsinglenode Method on any ixmldomnode object.

During specific queries, you can apply the new query language to the XSL template. The mode can be used to match and select attributes in XSL. Note that the default XSL template specifies the root node (/) that matches the XML document (/). The aboveCodeFor-each loops only operate on order items.

We know that XML is designed to describe the data structure in a hierarchical format. an XML document contains a single root node, which can contain multiple subnodes, any root node in the XML document can also contain more child nodes, but it can also contain no child nodes. Therefore, XML organizes data in the form of a tree.

The XSL pattern matching language provides semantics for traversing the tree structure of an XML document. The XSL pattern matching language is a bit like traversing the file system under the file system shell. For example, in the root directory of drive C, type:

CD windows \ system32

The current directory will be converted to c: \ windows \ system32. The XSL pattern matching language basically works in the same way. For example, orders/order/item indicates the item element under the order element under the orders element. This match is parsed in the context of the current node.

The node that calls selectnodes and selectsinglenode becomes the current node. As mentioned earlier in the XSL standard, adding "/" before a matching pattern indicates that pattern matching begins with a cluster root node ". /"indicates matching from the current node. Use "//" to match all the child nodes under the node, regardless of the layer on which it is located. You can also use "*" to match all elements. You can also use the "@" symbol to execute the attributes of a given element. For example, to identify the type attribute of a project element, the following matching mode can be used:

File: // item/@ Type

Generally, you can regard attributes as child nodes of elements. Unless the symbol "@" is added before the name. The basic matching pattern discussed here can recognize the set of elements. For example, the matching pattern // item specifies a set of project elements, which may exist in any location of the document. Because you need to find specific elements in a set, pattern matching provides a standard operator "[]" to indicate the index of a set. The index starts from 0. For example, the matching mode // item/price [0] indicates the first price element of each item element. Mode // item [0]/price [0] indicates the first price element of the first project element in all the project sets. In addition, you can use parentheses to group operations.

Next we will discuss the filtering mode. The filtering mode also contains expressions used to specify how to filter items in a set. There are actually many operators that you can use to create filter expressions. These operators allow you to create standard comparison expressions, logical expressions, and set expressions. Generally, the start and end of an operator are represented by the "$" symbol, many of which are represented by the symbols in C ++. For example, the following two expressions are equivalent:

File: // item [index ()> = 3]
File: // item [index () $ Ge $3]

Next we will analyze a relatively complex filter expression:

(Orders // order [customer/lastname $ ieq $ "skonnard" & @ itemcount> 1]/item [price> 20]) [index () <10]

This expression indicates selecting the first 10 orders from the set that meet the conditions. The condition is that the price is greater than $20, the customer's surname is skonnard, and his number of orders exceeds one. From this expression, you can find that using the filter expression you can perform very complex and accurate queries.

It must be noted that IE is the first browser to provide advanced XML support. To fully utilize the performance of IE, you must be familiar with the semantics of the XSL pattern matching language and use them when performing complex queries and filtering operations.

reference: http://spaces.msn.com/members/itxing/PersonalSpace.aspx

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.