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 9.5 KiB

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