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