1<?xml version="1.0"?> 2<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> 3 4 <!-- FileName: idkey30 --> 5 <!-- Document: http://www.w3.org/TR/xslt --> 6 <!-- DocVersion: 19991116 --> 7 <!-- Section: 12.4 Miscellaneous Additional Functions --> 8 <!-- Creator: David Marston --> 9 <!-- Purpose: Test that 'generate-id()' on various kinds of nodes yields unique values for each --> 10 11<xsl:output method="xml" encoding="UTF-8" indent="no"/> 12 13<xsl:template match="doc"> 14 <out> 15 <!-- Get in position so we have nodes on the following axis. --> 16 <xsl:apply-templates select="side"/> 17 </out> 18</xsl:template> 19 20<xsl:template match="side"> 21 <!-- Build up a string containing generated IDs for nodes on the following axis, plus attributes they carry. --> 22 <xsl:variable name="accumulated"> 23 <!-- Since call-template doesn't change context, iterate by position number. --> 24 <xsl:call-template name="nextnode"> 25 <xsl:with-param name="this" select="1" /> 26 <xsl:with-param name="max" select="count(following::node()|following::*/@*)" /> 27 <xsl:with-param name="idset" select="'+'" /> 28 <!-- Use + as delimiter to avoid spoofs from adjacent strings. 29 Returned string from generate-id() can't contain +. --> 30 </xsl:call-template> 31 </xsl:variable> 32 <!-- Summary data, so we have output when we pass. --> 33 <xsl:text>Number of IDs accumulated: </xsl:text> 34 <xsl:value-of select="count(following::node()|following::*/@*)"/> 35 <!-- Now, take one node of each kind, whose generated ID should not be in the accumulated string, 36 surround generated ID by + to avoid substring matches, and see if it's in there. --> 37 <!-- Now see if we duplicated an ID with the root --> 38 <xsl:if test="contains($accumulated,concat('+',generate-id(/),'+'))"> 39 <xsl:text>FAIL on root node whose ID is </xsl:text> 40 <xsl:value-of select="generate-id(/)"/> 41 </xsl:if> 42 <!-- Now see if we duplicated an ID with the element --> 43 <xsl:if test="contains($accumulated,concat('+',generate-id(.),'+'))"> 44 <xsl:text>FAIL on side node whose ID is </xsl:text> 45 <xsl:value-of select="generate-id(.)"/> 46 </xsl:if> 47 <!-- Now see if we duplicated an ID with the attribute --> 48 <xsl:if test="contains($accumulated,concat('+',generate-id(./@att),'+'))"> 49 <xsl:text>FAIL on side/@att node whose ID is </xsl:text> 50 <xsl:value-of select="generate-id(./@att)"/> 51 </xsl:if> 52 <!-- Now see if we duplicated an ID with the text node --> 53 <xsl:if test="contains($accumulated,concat('+',generate-id(./text()),'+'))"> 54 <xsl:text>FAIL on side/text() node whose ID is </xsl:text> 55 <xsl:value-of select="generate-id(./text())"/> 56 </xsl:if> 57 <!-- Now see if we duplicated an ID with the comment node --> 58 <xsl:if test="contains($accumulated,concat('+',generate-id(./comment()),'+'))"> 59 <xsl:text>FAIL on side/comment() node whose ID is </xsl:text> 60 <xsl:value-of select="generate-id(./comment())"/> 61 </xsl:if> 62 <!-- Now see if we duplicated an ID with the PI node --> 63 <xsl:if test="contains($accumulated,concat('+',generate-id(./processing-instruction('s-pi')),'+'))"> 64 <xsl:text>FAIL on side/processing-instruction('s-pi') node whose ID is </xsl:text> 65 <xsl:value-of select="generate-id(./processing-instruction('s-pi'))"/> 66 </xsl:if> 67 <!-- Now see if we duplicated an ID with a namespace node --> 68 <xsl:if test="contains($accumulated,concat('+',generate-id(./namespace::*[1]),'+'))"> 69 <xsl:text>FAIL on side/namespace::*[1] node whose ID is </xsl:text> 70 <xsl:value-of select="generate-id(./namespace::*[1])"/> 71 </xsl:if> 72</xsl:template> 73 74<xsl:template name="nextnode"> 75 <xsl:param name="this"/> 76 <xsl:param name="max"/> 77 <xsl:param name="idset"/> 78 <!-- Params this and max are index numbers, idset is the string we're accumulating. --> 79 <xsl:variable name="this-id" select="generate-id((following::node()|following::*/@*)[position() = $this])"/> 80 <xsl:choose> 81 <xsl:when test="$this <= $max"> 82 <!-- Recurse, adding current ID to string of all IDs on "begin" sub-tree, with separators --> 83 <xsl:call-template name="nextnode"> 84 <xsl:with-param name="this" select="$this+1" /> 85 <xsl:with-param name="max" select="$max" /> 86 <xsl:with-param name="idset" select="concat($idset,$this-id,'+')" /> 87 </xsl:call-template> 88 </xsl:when> 89 <xsl:otherwise> 90 <!-- "return" the final idset --> 91 <xsl:value-of select="$idset"/> 92 </xsl:otherwise> 93 </xsl:choose> 94</xsl:template> 95 96 97 <!-- 98 * Licensed to the Apache Software Foundation (ASF) under one 99 * or more contributor license agreements. See the NOTICE file 100 * distributed with this work for additional information 101 * regarding copyright ownership. The ASF licenses this file 102 * to you under the Apache License, Version 2.0 (the "License"); 103 * you may not use this file except in compliance with the License. 104 * You may obtain a copy of the License at 105 * 106 * http://www.apache.org/licenses/LICENSE-2.0 107 * 108 * Unless required by applicable law or agreed to in writing, software 109 * distributed under the License is distributed on an "AS IS" BASIS, 110 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 111 * See the License for the specific language governing permissions and 112 * limitations under the License. 113 --> 114 115</xsl:stylesheet> 116