1<?xml version="1.0"?> 2<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> 3 4 <!-- FileName: variable09 --> 5 <!-- Document: http://www.w3.org/TR/xslt --> 6 <!-- DocVersion: 19991116 --> 7 <!-- Section: 11.2 Values of Variables and Parameters --> 8 <!-- Purpose: Test for handling of number used as positional index by setting 9 variable using select attribute. Reference as [$n] --> 10 <!-- Author: Paul Dick --> 11 <!-- Note: When a variable is used to select nodes by position, 12 be careful not to do: 13 <xsl:variable name="n">2</xsl:variable> 14 ... 15 <xsl:value-of select="item[$n]"/> 16 This will output the value of the first item element, 17 because the variable n will be bound to a result tree fragment, 18 not a number. Instead, do either 19 20 <xsl:variable name="n" select="2"/> 21 ... 22 <xsl:value-of select="item[$n]"/> 23 or 24 <xsl:variable name="n">2</xsl:variable> 25 ... 26 <xsl:value-of select="item[position()=$n]"/> --> 27 28<xsl:template match="doc"> 29 <xsl:variable name="n" select="2"/> 30 <out> 31 <xsl:value-of select="item[$n]"/> 32 </out> 33</xsl:template> 34 35 36 <!-- 37 * Licensed to the Apache Software Foundation (ASF) under one 38 * or more contributor license agreements. See the NOTICE file 39 * distributed with this work for additional information 40 * regarding copyright ownership. The ASF licenses this file 41 * to you under the Apache License, Version 2.0 (the "License"); 42 * you may not use this file except in compliance with the License. 43 * You may obtain a copy of the License at 44 * 45 * http://www.apache.org/licenses/LICENSE-2.0 46 * 47 * Unless required by applicable law or agreed to in writing, software 48 * distributed under the License is distributed on an "AS IS" BASIS, 49 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 50 * See the License for the specific language governing permissions and 51 * limitations under the License. 52 --> 53 54</xsl:stylesheet> 55