O'Reilly logo

XSLT, 2nd Edition by Doug Tidwell

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Name

[2.0] insert-before()

Allows you to create a new sequence by inserting items into an existing sequence.

Syntax

item()* insert-before($target as item()*, $position as xs:integer, 
                      $inserts as item()*)

Inputs

A sequence, an xs:integer (a position), and a second sequence.

Outputs

A new sequence in which the second sequence has been inserted into the first sequence before the item at the requested position.

The rules for insert-before() are what you’d expect. If the first sequence is empty, the second sequence is returned unchanged. If the second sequence is empty, the first sequence is returned unchanged. If the position is less than 1, the processor inserts the second sequence at the start of the first. Finally, if the position is greater than the number of items in the first sequence, the second sequence is inserted at the end of the first.

Defined in

XQuery 1.0 and XPath 2.0 Functions and Operators section 15.1, “General Functions and Operators on Sequences.”

Example

Here’s a stylesheet that uses insert-before() in several ways:

<?xml version="1.0"?>
<!-- insert-before.xsl --> <xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema"> <xsl:output method="text"/> <xsl:template match="/"> <xsl:variable name="longSequence" as="item()*"> <xsl:sequence select="(3, 4, 5, current-date(), current-time(), 8, 'blue')"/> </xsl:variable> <xsl:variable name="shortSequence" as="item()*" select="(current-dateTime(), xs:yearMonthDuration('P3Y8M'))"/> ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required