Click to return to the XML (Extensible Markup Language) home page    
context Method     Context Changing Operator...    
Web Workshop  |  XML (Extensible Markup Language)

ancestor Method


Finds the nearest ancestor matching the pattern. It returns either a single element result or null.

Syntax

ancestor(pattern)

Parameters

pattern
Pattern to be matched.

Remarks

The ancestor method changes the context of the query from the current node to one of the nodes containing the current node. The pattern parameter is compared against the current node's parent (the node that contains the current node) and if the match succeeds, that node is returned. If the match fails, the grandparent is tested in a similar fashion, and so on. If none of the nodes in the ancestry chain match the pattern, the context cannot be changed and the query will return an empty node-list.

The ancestor method can start a filter, but can't appear to the right of a / or // in a pattern.

This method always returns a single element result or null. If used in expressions, null evaluates to false.

Examples

Find the nearest book ancestor of the current element:

ancestor(book)

Find the nearest ancestor author element that is contained in a book element:

ancestor(book/author)

Find the nearest ancestor with a "style" attribute:

ancestor(*[@style])

Note that this does not by itself provide a complete mechanism for determining the values of inherited attributes, since a "style" attribute on the current node will not be returned by this query.



Back to topBack to top

Did you find this topic useful? Suggestions for other topics? Write us!

© 1999 Microsoft Corporation. All rights reserved. Terms of use.