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 | <h2>History</h2>
|
---|
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:<prefix>'
|
---|
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 | <h2>Motivation</h2>
|
---|
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 <typedef> 'resource' attribute, the
|
---|
40 | build file writer has no chance to override or even prefix the names
|
---|
41 | supplied by the library. </p>
|
---|
42 | <h2>Assigning Namespaces</h2>
|
---|
43 |
|
---|
44 | <p>
|
---|
45 | Adding a 'prefix' attribute to <typedef> 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 <typedef> and <taskdef>
|
---|
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> <typedef resource="org/example/tasks.properties" uri="<a href="http://example.org/tasks">http://example.org/tasks</a>"/>
|
---|
51 | <my:task xmlns:my="<a href="http://example.org/tasks">http://example.org/tasks</a>">
|
---|
52 | ...
|
---|
53 | </my:task>
|
---|
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> <project name="test" xmlns:my="<a href="http://example.org/tasks">http://example.org/tasks</a>">
|
---|
61 | <typedef resource="org/example/tasks.properties" uri="<a href="http://example.org/tasks">http://example.org/tasks</a>"/>
|
---|
62 | <my:task>
|
---|
63 | ...
|
---|
64 | </my:task>
|
---|
65 | </project>
|
---|
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> <project name="test">
|
---|
71 | <typedef resource="org/example/tasks.properties" uri="<a href="http://example.org/tasks">http://example.org/tasks</a>"/>
|
---|
72 | <task xmlns="<a href="http://example.org/tasks">http://example.org/tasks</a>">
|
---|
73 | ...
|
---|
74 | </task>
|
---|
75 | </project>
|
---|
76 | </pre>
|
---|
77 | <p>
|
---|
78 | Here, the namespace is set as the default namespace for the <task>
|
---|
79 | element and all its descendants.
|
---|
80 | </p>
|
---|
81 | <h2>Default namespace</h2>
|
---|
82 | <p>
|
---|
83 | The default namespace used by Ant is "antlib:org.apache.tools.ant".
|
---|
84 | </p>
|
---|
85 | <pre>
|
---|
86 | <typedef resource="org/example/tasks.properties" uri="antlib:org.apache.tools.ant"/>
|
---|
87 | <task>
|
---|
88 | ....
|
---|
89 | </task>
|
---|
90 | </pre>
|
---|
91 |
|
---|
92 |
|
---|
93 |
|
---|
94 | <h2>Namespaces and Nested Elements</h2>
|
---|
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> <typedef resource="org/example/tasks.properties" uri="<a href="http://example.org/tasks">http://example.org/tasks</a>"/>
|
---|
102 | <my:task xmlns:my="<a href="http://example.org/tasks">http://example.org/tasks</a>">
|
---|
103 | <my:config a="foo" b="bar"/>
|
---|
104 | ...
|
---|
105 | </my:task>
|
---|
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> <typedef resource="org/example/tasks.properties" uri="<a href="http://example.org/tasks">http://example.org/tasks</a>"/>
|
---|
111 | <task xmlns="<a href="http://example.org/tasks">http://example.org/tasks</a>">
|
---|
112 | <config a="foo" b="bar"/>
|
---|
113 | ...
|
---|
114 | </task>
|
---|
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> <typedef resource="org/example/tasks.properties"
|
---|
121 | uri="<a href="http://example.org/tasks">http://example.org/tasks</a>"/>
|
---|
122 | <my:task xmlns:my="<a href="http://example.org/tasks">http://example.org/tasks</a>">
|
---|
123 | <config a="foo" b="bar"/>
|
---|
124 | ...
|
---|
125 | </my:task>
|
---|
126 | </pre>
|
---|
127 |
|
---|
128 | <h2>Namespaces and Attributes</h2>
|
---|
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> <my:task xmlns:my="<a href="http://example.org/tasks">http://example.org/tasks</a>">
|
---|
147 | <my:config a="foo" b="bar"/>
|
---|
148 | ...
|
---|
149 | </my:task>
|
---|
150 | </pre>
|
---|
151 | <p>
|
---|
152 | and
|
---|
153 | </p>
|
---|
154 | <pre> <my:task xmlns:my="<a href="http://example.org/tasks">http://example.org/tasks</a>">
|
---|
155 | <my:config my:a="foo" my:b="bar"/>
|
---|
156 | ...
|
---|
157 | </my:task>
|
---|
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 | <h2>Mixing Elements from Different Namespaces</h2>
|
---|
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 | <condition> 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 <typedef> 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> <typedef resource="org/example/conditions.properties" uri="<a href="http://example.org/conditions">http://example.org/conditions</a>"/>
|
---|
182 | <condition property="prop" xmlns="<a href="http://example.org/conditions">http://example.org/conditions</a>">
|
---|
183 | <and>
|
---|
184 | <available file="bla.txt"/>
|
---|
185 | <my:condition a="foo"/>
|
---|
186 | </and>
|
---|
187 | </condition>
|
---|
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 | <h2>Namespaces and Antlib</h2>
|
---|
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 © 2003-2004 The Apache Software
|
---|
205 | Foundation. All rights Reserved.</p>
|
---|
206 |
|
---|
207 | </body>
|
---|
208 | </html>
|
---|