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'))"/> ...

Get XSLT, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.