Previous (XmlNode function): Difference between revisions

From m204wiki
Jump to navigation Jump to search
m (1 revision)
m (1 revision)
Line 1: Line 1:
{{Template:XmlNode:Previous subtitle}}
{{Template:XmlNode:Previous subtitle}}


This function returns the previous XmlNode with the same parent as the method object.
This function returns the previous <var>XmlNode</var> with the same parent as the method object.
<ul>
<ul>
<li>If the method object is a non-Attribute node, the previous XmlNode is
<li>If the method object is a non-Attribute node, the previous <var>XmlNode</var> is
the previous sibling (in document order) of the method object.
the previous sibling (in document order) of the method object.
This is exactly the same as the node returned by SelectSingleNode
This is exactly the same as the node returned by SelectSingleNode
Line 9: Line 9:
This is demonstrated below in item [[??]] refid=nonatt page=no.
This is demonstrated below in item [[??]] refid=nonatt page=no.
in [[??]] refid=egprev..
in [[??]] refid=egprev..
<li>If the method object is an Attribute node, the previous XmlNode is the
<li>If the method object is an Attribute node, the previous <var>XmlNode</var> is the
preceding Attribute
preceding Attribute
node produced if the Element containing the method object
node produced if the Element containing the method object
were serialized in "normal" (that is, ''not'' ExclCanonical) order.
were serialized in "normal" (that is, ''not'' ExclCanonical) order.
<li>If no "previous" node exists, the Previous method returns a Null.
<li>If no "previous" node exists, the <var>Previous</var> method returns a Null.
</ul>
</ul>


The Previous function is new as of version 7.0 of the ''Sirius Mods''.
The <var>Previous</var> function is new as of version 7.0 of the ''Sirius Mods''.
==Syntax==
==Syntax==
{{Template:XmlNode:Previous syntax}}
{{Template:XmlNode:Previous syntax}}
Line 22: Line 22:
<table class="syntaxTable">
<table class="syntaxTable">
<tr><th>%nod</th>
<tr><th>%nod</th>
<td>An XmlNode, which will be set to point to the returned node. </td></tr>
<td>An <var>XmlNode</var>, which will be set to point to the returned node. </td></tr>
<tr><th>nod</th>
<tr><th>nod</th>
<td>An XmlNode expression.</td></tr>
<td>An <var>XmlNode</var> expression.</td></tr>
</table>
</table>


Line 32: Line 32:
the following-sibling and preceding-sibling axes are the empty nodeSet if
the following-sibling and preceding-sibling axes are the empty nodeSet if
the context node is an Attribute node.
the context node is an Attribute node.
The Next ([[??]] reftxt="Next function" refid=nexmeth.) and Previous methods
The Next ([[??]] reftxt="Next function" refid=nexmeth.) and <var>Previous</var> methods
are available if you want to traverse the Attribute nodes of an Element, as
are available if you want to traverse the Attribute nodes of an Element, as
a better approach than using the following&hyph.sibling and preceding&hyph.sibling
a better approach than using the following&hyph.sibling and preceding&hyph.sibling
Line 53: Line 53:
To select the node that precedes Element <tt>c</tt>, you can use
To select the node that precedes Element <tt>c</tt>, you can use
a sequence of statements like the following:
a sequence of statements like the following:
<p class="code">%n1 is object XmlNode
<p class="code">%n1 is object <var>XmlNode</var>
%n1 = %doc:SelectSingleNode('/*/*[3]')
%n1 = %doc:SelectSingleNode('/*/*[3]')
Print 'Here is node 3:'
Print 'Here is node 3:'
%n1:Print
%n1:Print
%n2 = %n1:Previous
%n2 = %n1:<var>Previous</var>
Print 'Here is the node previous to 3:'
Print 'Here is the node previous to 3:'
%n2:Print
%n2:Print
Line 99: Line 99:
To select the Attribute node that precedes Attribute <tt>b3</tt>, you can use
To select the Attribute node that precedes Attribute <tt>b3</tt>, you can use
a sequence of statements like the following:
a sequence of statements like the following:
<p class="code">%n1 = %doc:SelectSingleNode('/*/F/@b3'):Previous
<p class="code">%n1 = %doc:SelectSingleNode('/*/F/@b3'):<var>Previous</var>
Print 'Here is attribute previous to b3:'
Print 'Here is attribute previous to b3:'
%n1:Print
%n1:Print

Revision as of 17:46, 25 January 2011

Get node previous to this node (XmlNode class)


This function 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 ?? refid=nonatt page=no. in ?? refid=egprev..
  • 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.

The Previous function is new as of version 7.0 of the Sirius Mods.

Syntax

%outNod = nod:Previous

Syntax terms

%nod 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 (?? reftxt="Next function" refid=nexmeth.) and Previous methods are available if you want to traverse the Attribute nodes of an Element, as a better approach than using the following&hyph.sibling and preceding&hyph.sibling axes in an XPath argument to a SelectNodes or SelectSingleNode method. See example ?? refid=attprev..

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 :hp3.in document order:ehp3.. 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