You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

namespace.html 10 kB

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