The W3C has standardized an API for accessing XML documents known as XML DOM. The DOM API represents an XML document as a tree of nodes. Because an XML document is hierarchical in structure, you can build a tree of nodes and subnodes to represent an entire XML document. You can get to any arbitrary node by starting at the root node and traversing the child nodes of the root node. If you don’t find the node you are looking for, you can traverse the grandchild nodes of the root node. You can continue this process until you find the node you are looking for. |
The DOM API provides other services in additional to document traversal. You can find the full W3C XML DOM specification at http://www.w3.org/DOM. The following list shows some of the capabili- ties provided by the DOM API: |
Find the root node in an XML document. |
Find a list of elements with a given tag name. |
Get a list of children of a given node. |
Get the parent of a given node. |
Get the tag name of an element. |
Get the data associated with an element. |
Get a list of attributes of an element. |
Get the tag name of an attribute. |
Get the value of an attribute. |
Add, modify, or delete an element in the document. |
Add, modify, or delete an attribute in the document. |
Copy a node in a document (including subnodes). |
The DOM API provides a rich set of functionality to programmers as is shown in the previous list. The .NET Framework provides excellent support for the XML DOM API through the classes contained in the namespace System.Xml, which you will see later in this book. The DOM API is well suited for traversing and modifying an XML document, but, it provides little support for finding an arbitrary element or attribute in a document. Fortunately another XML technology is available to provide this support: XML Path Language (XPath). |
XPath |
XML is technically limited in that it is impossible to query or navigate through an XML document using XML alone. XPath language overcomes this limitation. XPath is a navigational query language specified by the W3C for locating data within an XML document. You can use XPath to query an XML document much as you use SQL to query a database. An XPath query expression can select on document parts, or types, such as the document’s elements, attributes, and text. It was created for use with XSLT and XPointer, as well as other components of XML such as the upcoming XQuery specification. All of these technologies require some mechanism that enables querying and navigation within the structure of an XML document. |