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 KiB

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