Finding Elements by Id in a DOM Document Using XPath

XPath is an expression language for selecting nodes in an XML file. See Finding Elements by Absolute Location in a DOM Document Using XPath for common XPath expression for selecting elements. This example adds to those examples by demonstrating the ability to select elements based on their id.

The DTD of an XML file may declare that a certain attribute of an element is a unique id. An attribute designated as such can contain any value but no two id attributes can have the same value.

This example demonstrates some common uses of expressions that use element ids; for more information on XPath, see the specification at http://www.w3c.org/TR/xpath. In the example, the result of an XPath expression is shown next to the expression; the numbers are ids of elements in the sample file shown at the end of the example.

// Get element id 3
String xpath = "id('3')";                // 3

// Get all e elements directly under element id 3
xpath = "id('two')/e";                   // 3 4 6

// Get elements with id='two', id='3', or id='seven'
xpath = "id('two 3 seven the fifth')";   // two 3 seven
// Note that this method of finding elements does not work
// if the id value contains a space

// Get a non-existent element
xpath = "id('100')";                     // (none)
To execute an XPath expression, see Finding Elements by Absolute Location in a DOM Document Using XPath. Here is the sample XML file used in the example:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE root [ <!ELEMENT e (e*) >
                 <!ATTLIST e  id    ID    #REQUIRED>
]>
<root>
    <e id="1">
        <e id="two">
            <e id="3"/>
            <e id="4">
                <e id="the fifth"/>
            </e>
            <e id="6"/>
        </e>
    </e>
    <e id="seven"/>
</root>

Comments

14 May 2012 - 8:09pm by adidas jeremy scott shoes (not verified)

However, in 1996, clover logo is specifically used in the classic series Original products.When we carefully think about the meaning and spirit of Adidas sports shoes, we will find out the special meaning of the three leaves.We need pray, hope and love in every aspects of our life and work, and we also need a pair of Adidas sports shoes like Adidas classic clover.Adidas sports shoes make the entire Adidas series more fashionable, including footwear, apparel and bags and other accessories.The shape of Clover opened like the earth three-dimensional.Yes, with the pray, hope and love, everything will be better for us.The well-acclaimed worldwide Adidas shoes mark is unparalleled in quality, reliability, and a top-class image that you'll feel assured to Adidas Porsche Bounce SL trust.Adidas classic clover: clover became to be the symbol of Adidas from in 19At that time; this symbol was used in all Adidas products.Many people like Adidas sports shoes, but do you know the special and profound meaning of Adidas sports shoes?Adidas sports shoes "clover" stands for pray, hope and love As we know, Adidas sports shoes is Adidas Porsche Bounce S2 one of the Adidas Porsche Bounce S most popular and famous brands of sports shoes all over the world.And released after Adidas Porsche Design Bounce some slightly re-designed for the conduct of their fabrics and styles.What a significant and profound Adidas Porsche Design Golf meaning the three leaves represent.More Adidas classic clover series are at www.

Post a comment

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
Image CAPTCHA
Enter the characters shown in the image. Ignore spaces and be careful about upper and lower case.