File: xsl_include_design.xml

package info (click to toggle)
libxalan2-java 2.7.1-5
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 19,468 kB
  • ctags: 26,006
  • sloc: java: 175,784; xml: 28,073; sh: 164; jsp: 43; makefile: 43; sql: 6
file content (131 lines) | stat: -rw-r--r-- 5,433 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
<?xml version="1.0" standalone="no"?>
<!DOCTYPE s1 SYSTEM "../../style/dtd/document.dtd">
<!--
 * Copyright 2001-2004 The Apache Software Foundation.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
-->
<!-- $Id: xsl_include_design.xml 337884 2004-02-17 19:29:35Z minchau $ -->
  <s1 title="&lt;xsl:include&gt; / &lt;xsl:import&gt;">

  <s2 title="Contents">
  <ul>
    <li><link anchor="functionality">Functionality</link></li>
    <li><link anchor="implementation">Implementation</link></li>    
  </ul>
  </s2>

  <anchor name="functionality"/>
  <s2 title="Functionality">

    <p><code>&lt;xsl:include&gt;</code> allows you to include one stylesheet
    into another. The includ<em>ed</em> stylesheet's templates will have the same
    default priorities and import precedence as the includ<em>ing</em> stylesheet.
    <code>&lt;xsl:import&gt;</code> offers the same, but the import precedence
    of elements in an import<em>ed</em> stylesheet is always less than that of
    the import<em>ing</em> stylesheet.</p>
   
  </s2>

  <anchor name="implementation"/>
  <s2 title="Implementation">
 
    <anchor name="include"/>
    <s3 title="&lt;xsl:include&gt;">

    <p>This is the simplest case, so we will look at that first. The algorithm
    for including another stylesheet is roughly:</p>

    <ul>
      <li>get the including stylesheet from the XSLT parser</li>
      <li>get the value of the "href" attribute from the
        <code>&lt;xsl:include&gt;</code> element and check for circular
        includes/imports</li>
      <li>check if there is a defined <code>SourceLoader</code> set either
        through the native or the TrAX API</li>
      <li>get an <code>InputSource</code> for the document to include, either
        from the <code>SourceLoader</code> or from the document's URI</li>
      <li>parse the input document using the compiler's XSLT parser</li>
      <li>set the import precedence of the included stylesheet to the same as
        the import precedence of the including stylesheet</li>
      <li>get the top-level stylesheet from the XSLT parser</li>
      <li>move all variables, parameters, and top-level elements (include
        templates) from the included stylesheet to the top-level stylesheet
        (all elements will keep their import precedence even after being moved
        to the top-level stylesheet)</li>
    </ul>
 
    </s3>    

    <anchor name="import"/>
    <s3 title="&lt;xsl:import&gt;">

    <p>This is very similar to <code>&lt;xsl:include&gt;</code>, but import
    precedence has to be handled differently. Looking at the code you'll find
    this fragment:</p><source>
    // Handle precedence for the including stylesheet
    final int currPrecedence = parser.getCurrentImportPrecedence();
    final int nextPrecedence = parser.getNextImportPrecedence();
    _imported.setImportPrecedence(currPrecedence);
    context.setImportPrecedence(nextPrecedence);</source>

    <p>The important thing here is that the imported stylesheet has import
    precedence <em>less</em> than the importing stylesheet. So the imported
    stylesheet gets the current import precedence, while the current stylesheet
    gets the next available (unused) import precedence. The
    <code>Stylesheet</code> class has a method
    <code>setImportPrecedence()</code> that ensures that the import precedence
    is set not only for the stylesheet itself, but that it is also propagated
    down to any included/imported stylesheets:</p><source>
    public void setImportPrecedence(final int precedence) {
	// Set import precedence for this stylesheet
	_importPrecedence = precedence;

	// Set import precedence for all included stylesheets
	final Enumeration elements = elements();
	while (elements.hasMoreElements()) {
	    SyntaxTreeNode child = (SyntaxTreeNode)elements.nextElement();
	    if (child instanceof Include) {
		Stylesheet included = ((Include)child).getIncludedStylesheet();
		if (included != null) included.setImportPrecedence(precedence);
	    }
	}

	// Set import precedence for the stylesheet that imported this one
	if (_importedFrom != null) {
	    if (_importedFrom.getImportPrecedence() &lt; precedence) {
		final Parser parser = getParser();
		final int nextPrecedence = parser.getNextImportPrecedence();
		_importedFrom.setImportPrecedence(nextPrecedence);
	    }
	}
	// Set import precedence for the stylesheet that included this one
	else if (_includedFrom != null) {
	    if (_includedFrom.getImportPrecedence() != precedence)
		_includedFrom.setImportPrecedence(precedence);
	}
    }</source>

    <p>This method has been carefully cluttered together, and it works, and it
    should not be touched.</p>

    </s3>

    <anchor name="apply-imports"/>
    <s3 title="&lt;xsl:apply-imports&gt;">

    </s3>

  </s2>

</s1>