source: release-kits/lirk3/resources/gs3-release-maker/ant/docs/manual/CoreTypes/namespace.html@ 14982

Last change on this file since 14982 was 14982, checked in by oranfry, 16 years ago

initial import of LiRK3

File size: 9.2 KB
Line 
1<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML//EN">
2<html><head><title>XmlNamespaceSupport</title>
3<link rel="stylesheet" type="text/css" href="../stylesheets/antmanual.css">
4</head>
5 <body>
6 <h2><a name="namespace">XML Namespace Support</a></h2>
7 Ant 1.6 introduces support for XML namespaces.
8 <h3>History</h3>
9
10 <p>
11 All releases of Ant prior to Ant 1.6 do not support XML namespaces.
12 No support basically implies two things here:
13 </p>
14 <ul>
15 <li> Element names correspond to the "qname" of the tags, which is
16 usually the same as the local name. But if the build file writer uses
17 colons in names of defined tasks/types, those become part of the
18 element name. Turning on namespace support gives colon-separated
19 prefixes in tag names a special meaning, and thus build files using
20 colons in user-defined tasks and types will break.
21 </li>
22 <li> Attributes with the names 'xmlns' and 'xmlns:<code>&lt;prefix&gt;</code>'
23 are not treated specially, which means that custom tasks and types have
24 actually been able to use such attributes as parameter names. Again,
25 such tasks/types are going to break when namespace support is enabled
26 on the parser.
27 </li>
28 </ul>
29 <p>Use of colons in element names has been discouraged in the past
30 IIRC, and using any attribute starting with "xml" is actually strongly
31 discouraged by the XML spec to reserve such names for future use.
32 </p>
33 <h3>Motivation</h3>
34
35 <p>In build files using a lot of custom and third-party tasks, it is
36 easy to get into name conflicts. When individual types are defined, the
37 build file writer can do some name-spacing manually (for example, using
38 "tomcat-deploy" instead of just "deploy"). But when defining whole
39 libraries of types using the <code>&lt;typedef&gt;</code> 'resource' attribute, the
40 build file writer has no chance to override or even prefix the names
41 supplied by the library. </p>
42 <h3>Assigning Namespaces</h3>
43
44 <p>
45 Adding a 'prefix' attribute to <code>&lt;typedef&gt;</code> might have been enough,
46 but XML already has a well-known method for name-spacing. Thus, instead
47 of adding a 'prefix' attribute, the <code>&lt;typedef&gt;</code> and <code>&lt;taskdef&gt;</code>
48 tasks get a 'uri' attribute, which stores the URI of the XML namespace
49 with which the type should be associated:
50 </p><pre> &lt;typedef resource="org/example/tasks.properties" uri="<a href="http://example.org/tasks">http://example.org/tasks</a>"/&gt;
51 &lt;my:task xmlns:my="<a href="http://example.org/tasks">http://example.org/tasks</a>"&gt;
52 ...
53 &lt;/my:task&gt;
54</pre>
55 <p>As the above example demonstrates, the namespace URI needs to be
56 specified at least twice: one time as the value of the 'uri' attribute,
57 and another time to actually map the namespace to occurrences of
58 elements from that namespace, by using the 'xmlns' attribute. This
59 mapping can happen at any level in the build file:
60 </p><pre> &lt;project name="test" xmlns:my="<a href="http://example.org/tasks">http://example.org/tasks</a>"&gt;
61 &lt;typedef resource="org/example/tasks.properties" uri="<a href="http://example.org/tasks">http://example.org/tasks</a>"/&gt;
62 &lt;my:task&gt;
63 ...
64 &lt;/my:task&gt;
65 &lt;/project&gt;
66</pre>
67 <p>
68 Use of a namespace prefix is of course optional. Therefore
69 the example could also look like this:
70 </p><pre> &lt;project name="test"&gt;
71 &lt;typedef resource="org/example/tasks.properties" uri="<a href="http://example.org/tasks">http://example.org/tasks</a>"/&gt;
72 &lt;task xmlns="<a href="http://example.org/tasks">http://example.org/tasks</a>"&gt;
73 ...
74 &lt;/task&gt;
75 &lt;/project&gt;
76</pre>
77 <p>
78 Here, the namespace is set as the default namespace for the <code>&lt;task&gt;</code>
79 element and all its descendants.
80 </p>
81 <h3>Default namespace</h3>
82 <p>
83 The default namespace used by Ant is "antlib:org.apache.tools.ant".
84 </p>
85 <pre>
86&lt;typedef resource="org/example/tasks.properties" uri="antlib:org.apache.tools.ant"/&gt;
87&lt;task&gt;
88 ....
89&lt;/task&gt;
90 </pre>
91
92
93
94 <h3>Namespaces and Nested Elements</h3>
95
96 <p>
97 Almost always in Ant 1.6, elements nested inside a namespaced
98 element have the same namespace as their parent. So if 'task' in the
99 example above allowed a nested 'config' element, the build file snippet
100 would look like this:
101 </p><pre> &lt;typedef resource="org/example/tasks.properties" uri="<a href="http://example.org/tasks">http://example.org/tasks</a>"/&gt;
102 &lt;my:task xmlns:my="<a href="http://example.org/tasks">http://example.org/tasks</a>"&gt;
103 &lt;my:config a="foo" b="bar"/&gt;
104 ...
105 &lt;/my:task&gt;
106</pre>
107 <p>If the element allows or requires a lot of nested elements, the
108 prefix needs to be used for every nested element. Making the namespace
109 the default can reduce the verbosity of the script:
110 </p><pre> &lt;typedef resource="org/example/tasks.properties" uri="<a href="http://example.org/tasks">http://example.org/tasks</a>"/&gt;
111 &lt;task xmlns="<a href="http://example.org/tasks">http://example.org/tasks</a>"&gt;
112 &lt;config a="foo" b="bar"/&gt;
113 ...
114 &lt;/task&gt;
115 </pre>
116 <p>
117 From Ant 1.6.2, elements nested inside a namespaced element may also be
118 in Ant's default namespace. This means that the following is now allowed:
119 </p>
120 </p><pre> &lt;typedef resource="org/example/tasks.properties"
121 uri="<a href="http://example.org/tasks">http://example.org/tasks</a>"/&gt;
122 &lt;my:task xmlns:my="<a href="http://example.org/tasks">http://example.org/tasks</a>"&gt;
123 &lt;config a="foo" b="bar"/&gt;
124 ...
125 &lt;/my:task&gt;
126</pre>
127
128 <h3>Namespaces and Attributes</h3>
129
130 <p>
131 Attributes are only used to configure the element they belong to if:
132 </p>
133 <ul>
134 <li> they have no namespace (note that the default namespace does *not* apply to attributes)
135 </li>
136 <li> they are in the same namespace as the element they belong to
137 </li>
138 </ul>
139 <p>
140 Other attributes are simply ignored.
141 </p>
142 <p>
143 This means that both:
144 </p>
145 <p>
146 </p><pre> &lt;my:task xmlns:my="<a href="http://example.org/tasks">http://example.org/tasks</a>"&gt;
147 &lt;my:config a="foo" b="bar"/&gt;
148 ...
149 &lt;/my:task&gt;
150</pre>
151 <p>
152 and
153 </p>
154 <pre> &lt;my:task xmlns:my="<a href="http://example.org/tasks">http://example.org/tasks</a>"&gt;
155 &lt;my:config my:a="foo" my:b="bar"/&gt;
156 ...
157 &lt;/my:task&gt;
158</pre>
159 <p>
160 result in the parameters "a" and "b" being used as parameters to configure the nested "config" element.
161 </p>
162 <p>It also means that you can use attributes from other namespaces
163 to markup the build file with extra meta data, such as RDF and
164 XML-Schema (whether that's a good thing or not). The same is not true
165 for elements from unknown namespaces, which result in a error.
166 </p>
167 <h3>Mixing Elements from Different Namespaces</h3>
168
169 <p>Now comes the difficult part: elements from different namespaces can
170 be woven together under certain circumstances. This has a lot to do
171 with the Ant 1.6
172 <a href="../develop.html#nestedtype">add type introspection rules</a>:
173 Ant types and tasks are now free to accept arbritrary named types as
174 nested elements, as long as the concrete type implements the interface
175 expected by the task/type. The most obvioius example for this is the
176 <code>&lt;condition&gt;</code> task, which supports various nested conditions, all
177 of which extend the interface <tt>Condition</tt>. To integrate a
178 custom condition in Ant, you can now simply <code>&lt;typedef&gt;</code> the
179 condition, and then use it anywhere develop.html#nestedtypwhere conditions are allowed
180 (assuming the containing element has a generic <tt>add(Condition)</tt> or <tt>addConfigured(Configured)</tt>method):
181</p><pre> &lt;typedef resource="org/example/conditions.properties" uri="<a href="http://example.org/conditions">http://example.org/conditions</a>"/&gt;
182 &lt;condition property="prop" xmlns="<a href="http://example.org/conditions">http://example.org/conditions</a>"&gt;
183 &lt;and&gt;
184 &lt;available file="bla.txt"/&gt;
185 &lt;my:condition a="foo"/&gt;
186 &lt;/and&gt;
187 &lt;/condition&gt;
188</pre>
189 <p>
190 In Ant 1.6, this feature cannot be used as much as we'd all like to: a
191 lot of code has not yet been adapted to the new introspection rules,
192 and elements like the builtin Ant conditions and selectors are not
193 really types in 1.6. This is expected to change in Ant 1.7.
194 </p>
195 <h3>Namespaces and Antlib</h3>
196
197 <p>
198 The new <a href="antlib.html">AntLib</a>
199 feature is also very much integrated with the namespace support in Ant
200 1.6. Basically, you can "import" Antlibs simply by using a special
201 scheme for the namespace URI: the <tt>antlib</tt> scheme, which expects the package name in which a special <tt>antlib.xml</tt> file is located.
202 </p>
203<hr>
204<p align="center">Copyright &copy; 2003-2004 The Apache Software
205Foundation. All rights Reserved.</p>
206
207</body>
208</html>
Note: See TracBrowser for help on using the repository browser.