Previous (XmlNode function)

From m204wiki
Revision as of 03:26, 29 May 2011 by Goff (talk | contribs) (edits, tags and links)
Jump to navigation Jump to search

Get node previous to this node (XmlNode class)

Previous returns the previous XmlNode with the same parent as the method object.

  • If the method object is a non-Attribute node, the previous XmlNode is the previous sibling (in document order) of the method object. This is exactly the same as the node returned by selectSingleNode with 'preceding-sibling::node()[1]' as its XPath argument. This is demonstrated below in item "example 1" .
  • If the method object is an Attribute node, the previous XmlNode is the preceding Attribute node produced if the Element containing the method object were serialized in "normal" (that is, not ExclCanonical) order.
  • If no "previous" node exists, the Previous method returns a Null.

Syntax

%outNod = nod:Previous

Syntax terms

%outNod An XmlNode, which will be set to point to the returned node.
nod An XmlNode expression.

Usage notes

  • The XPath recommendation specifies that the following-sibling and preceding-sibling axes are the empty nodeSet if the context node is an Attribute node. The Next and Previous methods are available if you want to traverse the Attribute nodes of an Element, as a better approach than using the following-sibling and preceding-sibling axes in an XPath argument to a selectNodes or SelectSingleNode method. See: "example 2".
  • Previous is new as of Sirius Mods Version 7.0.

Examples

  1. Given the following document:

    <top> <a/> <c/> <d/> <e/> </top>

    To select the node that precedes Element c, you can use a sequence of statements like the following:

    %n1 is object XmlNode %n1 = %doc:selectSingleNode('/*/*[3]') print 'Here is node 3:' %n1:print %n2 = %n1:previous print 'Here is the node previous to 3:' %n2:print

    The result is:

    Here is node 3: <c/> Here is the node previous to 3:

    Note: You might expect a statement sequence like this next one to also obtain the b node as above:

    %n2 = %n1:selectSingleNode('preceding-sibling::node()') print 'Here is the preceding-sib to 3:' %n2:print

    However, this is the result:

    Here is the preceding-sib to 3: <a/>

    The preceding-sibling axis generates resultant nodes in reverse document order (in this case, b, then a), which is probably what you intuitively expect. However, the SelectSingleNode statement selects the first of these two generated nodes in document order. To use SelectSingleNode to select the adjacent preceding sibling node, use a predicate so the XPath argument selects the (only) node you want:

    %n2 = %n1:selectSingleNode('preceding-sibling::node()[1]')

  2. Given the following document:

    <top> <F b1="b1" b2="b2" b3="b3" b4="b4"/> </top>

    To select the Attribute node that precedes Attribute b3, you can use a sequence of statements like the following:

    %n1 = %doc:selectSingleNode('/*/F/@b3'):Previous print 'Here is attribute previous to b3:' %n1:print

    The result is:

    Here is attribute previous to b3: b2="b2"

    Note: The preceding-sibling axis does not locate Attribute nodes. The following statement causes a request cancellation:

    %n2 = %n1:selectSingleNode('preceding-sibling::node()[1]')

See also