## [saxon] Summary: Understanding the Relationship of Nodes, Sequences, and Trees

 [saxon] Summary: Understanding the Relationship of Nodes, Sequences, and Trees From: Roger L. Costello - 2004-04-06 12:26:35 Attachments: Message as HTML ``` Hi Folks, Over the last few days I have been having some excellent exchanges with Michael and have learned a lot. I thought that I would summarize what I learned, so that others can benefit as well. Below is my summary. Thanks Michael! /Roger Understanding the Relationship of Nodes, Sequences, and Trees (1) A node can belong to only one tree. (2) A node may belong to any number of sequences. (3) Axes always apply to the tree that the node is in. Axes never apply to the sequence that the node is in. (4) When xsl:sequence is used in a sequence which has no parent node then the sequence contains the original node referenced by xsl:sequence and not a copy. (5) When xsl:sequence is used in a sequence which has a parent node then the element that is referenced by xsl:sequence is copied. Thus, the sequence is comprised of a copy and not the original. (6) The preceding-sibling and following-sibling axes can only be used in a tree. That is, they cannot be used in a sequence that does not have a parent node. (Nodes are "siblings" iff they have a common ancestor) To understand these rules, let's consider an example. Below is the XML document that my stylesheet operates upon: Jeff David Roger In my stylesheet I have created this variable: Sally Linda Note that this variable contains a mix of elements - the first element (the David Member) comes from the FitnessCenter. The second and third elements (Stacey and Linda) are defined within the variable itself. Further, note that this sequence does not have a parent node (due to the presence of as="element()+". A characteristic of xsl:sequence when used in a sequence that does not have a parent node is that it does not create a copy of the node that it references; instead, it uses the original node. Thus, \$members[1] is referencing the original node: David from the FitnessCenter. Now let's consider the above rules in the context of this example. (1) A node can belong to only one tree. \$members[1] references this node: David This node belongs to the FitnessCenter tree. \$members[2] references this node: Stacey This node belongs to the \$members sequence. (\$members does not create a tree. It is only creating a sequence of nodes.) (2) A node may belong to any number of sequences. This node: David belongs to both the FitnessCenter sequence as well as the \$members sequence. (3) Axes always apply to the tree that the node is in. Axes never apply to the sequence that the node is in. Consider this XSLT statement which uses the preceding-sibling axis: \$members[1] references the David node, which is in the FitnessCenter tree. Therefore, it is referencing David's preceding-sibling in the FitnessCenter tree: Jeff Likewise, this is referencing David's following-sibling in the FitnessCenter tree: Output: Roger Note that you cannot use preceding-sibling nor following-sibling on \$member[2] or \$member[3] because these axes only apply to nodes in a tree. The Stacey Member and Linda Member are not in a tree - they are only in a sequence. (4) When xsl:sequence is used in a sequence which has no parent node then the sequence contains the original node referenced by xsl:sequence and not a copy. Consider this variable declaration: Sally Linda This variable is comprised of a sequence of nodes. The sequence does not have a parent node. Therefore, the sequence is comprised of the original node. (5) When xsl:sequence is used in a sequence which has a parent node then the element that is referenced by xsl:sequence is copied. Thus, the sequence is comprised of a copy and not the original. Consider this variable declaration: Sally Linda Note the absence of as="element()+". Thus, this Member sequence has a document node as its parent. Consequently, a *copy* of /FitnessCenter/Member[2] is made and used in the sequence. So this statement produces an empty output: (6) The preceding-sibling and following-sibling axes can only be used in a tree. That is, they cannot be used in a sequence that does not have a parent node. (Nodes are "siblings" iff they have a common parent) Therefore, for example, you cannot use following-sibling to get the member that follows Sally: Sally Linda This statement produces an empty output: However, in this version the sequence does have a parent (document) node, so you can use following-sibling to retrieve the member that follows Sally: Sally Linda This statement: yields this output: Linda An alternate form that will also work is this: Sally Linda Note that the member sequence has a parent node (). Therefore, the following-sibling and preceding-sibling axes can be used on the member sequence, e.g., This statement: yields this output: Linda ```

 [saxon] Summary: Understanding the Relationship of Nodes, Sequences, and Trees From: Roger L. Costello - 2004-04-06 12:26:35 Attachments: Message as HTML ``` Hi Folks, Over the last few days I have been having some excellent exchanges with Michael and have learned a lot. I thought that I would summarize what I learned, so that others can benefit as well. Below is my summary. Thanks Michael! /Roger Understanding the Relationship of Nodes, Sequences, and Trees (1) A node can belong to only one tree. (2) A node may belong to any number of sequences. (3) Axes always apply to the tree that the node is in. Axes never apply to the sequence that the node is in. (4) When xsl:sequence is used in a sequence which has no parent node then the sequence contains the original node referenced by xsl:sequence and not a copy. (5) When xsl:sequence is used in a sequence which has a parent node then the element that is referenced by xsl:sequence is copied. Thus, the sequence is comprised of a copy and not the original. (6) The preceding-sibling and following-sibling axes can only be used in a tree. That is, they cannot be used in a sequence that does not have a parent node. (Nodes are "siblings" iff they have a common ancestor) To understand these rules, let's consider an example. Below is the XML document that my stylesheet operates upon: Jeff David Roger In my stylesheet I have created this variable: Sally Linda Note that this variable contains a mix of elements - the first element (the David Member) comes from the FitnessCenter. The second and third elements (Stacey and Linda) are defined within the variable itself. Further, note that this sequence does not have a parent node (due to the presence of as="element()+". A characteristic of xsl:sequence when used in a sequence that does not have a parent node is that it does not create a copy of the node that it references; instead, it uses the original node. Thus, \$members[1] is referencing the original node: David from the FitnessCenter. Now let's consider the above rules in the context of this example. (1) A node can belong to only one tree. \$members[1] references this node: David This node belongs to the FitnessCenter tree. \$members[2] references this node: Stacey This node belongs to the \$members sequence. (\$members does not create a tree. It is only creating a sequence of nodes.) (2) A node may belong to any number of sequences. This node: David belongs to both the FitnessCenter sequence as well as the \$members sequence. (3) Axes always apply to the tree that the node is in. Axes never apply to the sequence that the node is in. Consider this XSLT statement which uses the preceding-sibling axis: \$members[1] references the David node, which is in the FitnessCenter tree. Therefore, it is referencing David's preceding-sibling in the FitnessCenter tree: Jeff Likewise, this is referencing David's following-sibling in the FitnessCenter tree: Output: Roger Note that you cannot use preceding-sibling nor following-sibling on \$member[2] or \$member[3] because these axes only apply to nodes in a tree. The Stacey Member and Linda Member are not in a tree - they are only in a sequence. (4) When xsl:sequence is used in a sequence which has no parent node then the sequence contains the original node referenced by xsl:sequence and not a copy. Consider this variable declaration: Sally Linda This variable is comprised of a sequence of nodes. The sequence does not have a parent node. Therefore, the sequence is comprised of the original node. (5) When xsl:sequence is used in a sequence which has a parent node then the element that is referenced by xsl:sequence is copied. Thus, the sequence is comprised of a copy and not the original. Consider this variable declaration: Sally Linda Note the absence of as="element()+". Thus, this Member sequence has a document node as its parent. Consequently, a *copy* of /FitnessCenter/Member[2] is made and used in the sequence. So this statement produces an empty output: (6) The preceding-sibling and following-sibling axes can only be used in a tree. That is, they cannot be used in a sequence that does not have a parent node. (Nodes are "siblings" iff they have a common parent) Therefore, for example, you cannot use following-sibling to get the member that follows Sally: Sally Linda This statement produces an empty output: However, in this version the sequence does have a parent (document) node, so you can use following-sibling to retrieve the member that follows Sally: Sally Linda This statement: yields this output: Linda An alternate form that will also work is this: Sally Linda Note that the member sequence has a parent node (). Therefore, the following-sibling and preceding-sibling axes can be used on the member sequence, e.g., This statement: yields this output: Linda ```

No, thanks