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

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