# Following sibling position and dating

### RE: preceding/following sibling

Subject: XSL following-sibling, how to select / query just the next node. Author: Mike Kelly Date: 25 Jun AM. Hi, this may be a pretty basic one, but. Context element in the following part is persName, since it is inside foreach loop following::opener/dateline[1]/date"/>. [email protected]; Date: Wed, 11 Apr + is a result of using the preceding-sibling:: and following-sibling:: axes which only return nodes and use the position() function to reference the previous and next sections.

Select X children that have at least three attributes. Select X children whose attributes sum to a value less than 7. Select X children that have no attributes named a. Select X children that have no attributes. Select X children that have an attribute named a with value '10'. X[ a eq '10']: Select X children that have a child named Z with value '10'. Select X children that have a child named Z with value not equal to '10'. Select X children if they have at least one child text node.

Select X children if they have a text node with at least one non-whitespace character. Select X children if they have any child node. Select X children if they contain a comment node. Select X children if they have an a whose numerical value is less than This expression will work equally well in XPath 1. Select X if it has at least one preceding sibling named Z with an attribute y that is not equal to Select X children whose string-value consist of a single space character.

An odd way of getting an empty sequence! X elements with exactly 5 children elements. X elements with exactly 5 children nodes including element, text, comment, and PI nodes but not attribute nodes. X[count node eq 5]: X elements with exactly 5 nodes of any kind.

The first X child, provided it has the value 'some text'; empty otherwise. Select all X children with the value 'some text' and return the first or empty if there is no such child. In simpler words, the first X child element that has the string-value 'some text'.

However, mastering the themes exemplified above should help you develop almost any filtering expression you desire. Also consider that one can create more complex conditions using the logical operators and, or and the function not. Select the first Y child of every X child of the context node. This expression can result in a sequence of more than one Y. This expression can at most select one Y.

### SSDN - XSL following-sibling, how to select / query just the next node

Working with Sequences Problem You want to manipulate collections of arbitrary nodes and atomic values derived from an XML document or documents. There is an idiomatic way to construct the empty node sets using XSLT 1. The empty node set: The empty sequence constructor. Sequence consisting of the single atomic item 1. Use the comma operator to construct a sequence.

Here we build a sequence of all X children of the context, followed by Y children, followed by Z children. Use the to operator to construct ranges. Here we combine comma with several ranges. Variables and functions can be used as well. Sequences do not nest so the following two sequences are the same. The to operator cannot create a decreasing sequence directly.

This sequence is empty! You can accomplish the intended effect with the following. Remove duplicates from a sequence. Return the size of a sequence. Test if a sequence is empty. Locate the positions of an item in a sequence. Index-of produces a sequence of integers for every item in the first arg that is eq to the second. If you need to remove several elements, you might consider an expression like the following. Thus, the atomic value 1 is just as much a sequence as the result of the expression 1 to Another way of saying this is that every XPath 2.

A sequence can contain from zero or more values, and these values can be nodes, atomic values, or mixtures of each. Order is significant when comparing sequences. Node sets are not as tidy a concept as sequence, but in many cases, the distinction is irrelevant.

For example, any XPath 1. The advantage of XPath 2. The recipes in this section introduce many important sequence idioms, and you will find many others sprinkled through the recipes of this book.

These tricks rely on the fact that false converts to 0 and true to 1 when used in a mathematical context. So, for example, min, max, and absolute value can be calculated directly in XPath 1.

For other simple conditionals, use the new conditional if expression. Default the value of a missing attribute to Default the value of a missing element to 'unauthorized'.

Guard against division by zero. A para elements text if it contains at least one non-whitespace character; otherwise, a single space.

This is not because such code is more complicated or inefficient in XSLT but rather because it is so darn verbose. On first exposure, one may get the impression that XPath was somehow bastardized via the introduction of what procedural programmers call flow of control statements. However, once you begin to use XPath 2. Further, the XPath 2. As an illustration, compare the following snippets: One important fact about the XPath conditional expression is that the else is not optional. C programmers can appreciate this by comparing it to the a?

Often one will use the empty sequence when there is no other sensible value for the else part of the expression. Conditional expressions are useful for defaulting in the absence of a schema that provides defaults. Defaulting the value of an optional attribute: Defaulting the value of an optional element: The following code that decodes an enumerated list if size eq 'XXL' then 50 else if size eq 'XL' then 45 else if size eq 'L' then 40 else if size eq 'M' then 34 else if size eq 'S' then 32 else if size eq 'XS' then 29 else -1 However, in this case, you might find a solution using sequences to be cleaner especially if you replace the literal sequences with variables that might be initialized from an external XML file.

We are also relying on the fact that index-of returns an empty sequence when the search item is not found which we concatenate with 7 to handle the else case. Eliminating Recursion with for Expressions Problem You want to derive an output sequence from an input sequence where each item in the output is an arbitrarily complex function of the input and the sizes of each sequence are not necessarily the same.

Solution Not applicable in 1. Use a recursive XSLT template. Here we show four cases demonstrating how the for expression can map sequences of differing input and output sizes. Map a sequence of words in all paragraphs to a sequence of word lengths. Map a sequence of words in a paragraph to a sequence of word lengths for words greater than three letters. Same as above but with a condition on the input sequence. Generate a sequence of squares of the first integers.

Generate a sequence of squares in reverse order. Map a sequence of paragraphs to a duped sequence of paragraphs. Map words to word followed by word length. For each customer, output an id and the total of all the customers orders. You will quickly overcome your doubts, however, when you experience the liberating power of these XPath 2.

## RE: preceding/following sibling

This is especially true for the XPath 2. The power of for becomes most apparent when one considers how it can be applied to reduce many complicated recursive XSLT 1.

Consider the problem of computing sums in XSLT 1. If all you need is a simple sum, there is no problem because the built-in XPath 1. However, if you need to compute the sum of squares, you are forced to write a larger, more awkward, and less transparent recursive template. In fact, a good portion of the first edition of this book was recipes for canned solutions to these recursive gymnastics.

Think of the trees that I could have saved if this facility was in XPath 1. However, the for expression is hiding even more power. You are not limited to just one iteration variable. Several variables can be combined to create nested loops that create sequences from interrelated nodes in a complex document.

## Other Resources

Return a sequence consisting of para ids and the ids those para elements reference. You cannot use the position function as you would in an xsl: However, you can achieve the effect you want with the following expression: Taming Complex Logic Using Quantifiers Problem You need to test a sequence for the existence of a condition in some or all of its items. True if at least one section is referenced. True if all section elements are referenced by some ref element.

There exists a section that references every section except itself. This is because the operators evaluate to true if there is at least one pair of values from each side of the expression which compare according to the relation.

This follows from the fact that you cannot find a pair of items within each empty sequence that are equal. Using Set Operations You want to process sequences as if they were mathematical sets. In addition, intersect and except are added for intersection and set difference respectively. Unlike node sets, sequences are ordered and can contain duplicate items. However, when using the XPath 2. The result of a set operation will never contain duplicates even if the inputs did. The except operator is used in an XPath 2.

All attributes except a. All attributes except a and b. Atomic values are not allowed.

**Traci Braxton Chats The New Season Of "Braxton Family Values"**

This is because the set operations are over node identity and not value. One can get the effect of sets of values using the following XPath 2. Using Node Comparisons You want identify nodes or relate them by their position in a document.

Determine the number of articles written by Mr. These extended functions provide additional functionality that is useful to create behaviors, but does not conform to the XPath standard. OSM supports XPath 1. The XPath function library is divided into four groups, each of which is described in more detail, below: Node set functions - for working with node-sets, either the implicit current node set or one passed as a parameter.

For working with strings and include type coercions. For working with Booleans, including type coercions. For working with numbers, including type coercions.

Example - id "article. Returns the non-namespace portion of the node name of either a node set passed as a parameter or the current node in the current node set.

Returns the namespace URI of the node name of either a node set passed as a parameter or the current node in the current node set. Returns the complete textual node name of either a node set passed as a parameter or the current node in the current node set.

Envelope"] node-set evaluate string Returns the node set resulting from the Xpath expression defined by the provided argument. Allows XPath expressions to be dynamically created.