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.

antlib_namespaces.xml 5.8 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <document>
  3. <properties>
  4. <index value="2"/>
  5. <author email="antoine@apache.org">Antoine Levy-Lambert</author>
  6. <title>Antlib Namespaces</title>
  7. </properties>
  8. <body>
  9. <section name="J.Pietschmann 03.05.2003 17:25">
  10. <p>
  11. Nicola Ken Barozzi wrote:
  12. &lt; This seems interesting, and brings up what XML namespaces can be used for.
  13. </p><p>
  14. XML namespaces are indented to disambiguate short local element
  15. and attribute names. Any sematic associated to XML namespaces
  16. beside this has to be weighted carefully.
  17. </p><p>
  18. Lets take an example. There are two projects, Foo and Bar,
  19. each providing a task, lets call them &lt;foo&gt; and &lt;bar&gt;
  20. respectively. Both tasks take a &lt;part&gt; child, by coincidence.
  21. Of course, because the projects act uncoordinated, the &lt;part&gt;
  22. child element has a different semantic. In order to make this
  23. clearer, let's say the Foo &lt;part&gt; takes an optional &lt;mumble&gt;
  24. child while the Bar &lt;part&gt; takes three mandatory &lt;xonx&gt;
  25. children.
  26. </p><p>
  27. Someone finds both the &lt;foo&gt; and the &lt;bar&gt; task exciting and
  28. wants to use both in an Ant build file. No problem so far:
  29. because ot the way Ant elements get their child elements and
  30. create associated Java objects, this should work.
  31. Now said someone got a super-duper schema directed XML editor
  32. and wants to use it for editing the build.xml file. He asks
  33. all projects for a schema (DTD, XSD, RNG, whatever) for this
  34. purpose and merges them in order to get a schema for his build
  35. file. At this point the two &lt;part&gt; elements are likely to clash
  36. (at least for DTDs, where element names are global). While
  37. it is possible to merge the content models so that &lt;part&gt; now
  38. takes either an optional &lt;mumble&gt; or three &lt;xonx&gt; children, this
  39. would allow the user to put &lt;xonx&gt; children into the &lt;part&gt; of
  40. the &lt;foo&gt; task. This is only a minor inconvenience for most
  41. people, but an unthinkable horror for true purists.
  42. </p><p>
  43. Introduce namespaces: the Foo projects names its namespace
  44. "http://www.fooproject.org/anttask" while the Bar project uses
  45. "URI:bar" or whatever. For the XML parser it is only really
  46. important that two different strings are used. You see, the
  47. longer the strings the less tha chance they will clash, and
  48. they probably won't clash if they start with the URLs of the
  49. project's homepages (the intent behind the recommendation to
  50. use URLs, because it's the closest thing to a global registry
  51. you can get short of actually creating a global registry).
  52. Anyway, because the expanded names of the &lt;part&gt; elements are
  53. now "{http://www.fooproject.org/anttask}part" and "{URI:bar}part"
  54. respectively they obviously no longer clash.
  55. BTW you can write this as
  56. </p>
  57. <source><![CDATA[
  58. <target name="foo">
  59. <foo xmlns="http://www.fooproject.org/anttask">
  60. <part>
  61. <mumble>
  62. </part>
  63. </foo>
  64. <bar xmlns="URI:bar">
  65. <part><xonx/><xonx/><xonx/></part>
  66. </bar>
  67. <target>
  68. ]]></source>
  69. <p>
  70. or as
  71. </p>
  72. <source><![CDATA[
  73. <target name="foo"
  74. xmlns:foo="http://www.fooproject.org/anttask"
  75. xmlns:bar="URI:bar">
  76. <foo:foo>
  77. <foo:part>
  78. <foo:mumble>
  79. </foo:part>
  80. </foo:foo>
  81. <bar:bar>
  82. <bar:part><bar:xonx/><bar:xonx/><bar:xonx/></bar:part>
  83. </bar:bar>
  84. <target>
  85. ]]></source>
  86. <p>
  87. take your pick (if you think the "foo" and "bar" prefixes are too
  88. long, use "a" and "b" instead, it doesn't matter).
  89. </p><p>
  90. So far, the namespace names should only be different for different
  91. projects, so why is it dangerous to associate some semantic with it,
  92. like letting them point to a jar file? The problem is again that
  93. general purpose XML tools, like the above mentioned super-duper XML
  94. editor may associate their own semantics with the namespace, like
  95. how to auto-format certain elements. This information will be stored
  96. in some config files, and it requires that the namespace name is
  97. the same until the semantics of the elements in it have changed
  98. enough that it warrants assigning a new namespace name.
  99. </p><p>
  100. Summary:
  101. </p>
  102. <ol>
  103. <li>
  104. XML namespaces are there to facilitate aggregation of XML adhering
  105. to schemas (content models) of different, uncoordinated origin.
  106. </li><li>
  107. XML Namespaces should be used in a way that no end user action
  108. can result in two namespace names becoming unintentionally the
  109. same.
  110. </li><li>
  111. XML Namespace names should preferably be assigned by the people
  112. or project which specifies the semantics of the XML elemnets and
  113. attributes therein.
  114. </li><li>
  115. XML Namespace names should be kept unchanged until a change of
  116. the semantic of the elements warrants a change.
  117. </li><li>
  118. Good tools should not monopolize XML namespace syntax for its
  119. own semantics.
  120. </li>
  121. </ol>
  122. <p>
  123. The schema directed editor should provide an example hoe tools
  124. can take advantage of XML namespaces: use them as a key into a
  125. DB/config to get it's own associated semantic.
  126. In particular for Ant/Antlib I can imagine that each library
  127. provides a factory object associated to the XML namespace for
  128. the library.
  129. </p><p>
  130. The FOP parser uses such a two stage lookup: first the namespace
  131. is used to get a factory object from a hash table, then the factory
  132. is used with the local XML element name to create a Java object
  133. which is inserted into the FO tree. The hash table with the factories
  134. is initialized at startup, the associations between namespace name
  135. and factory class name is read from a Services file. Want to add
  136. a FOP extension? Get the default Services file, add a line with
  137. your namespace-to-factoryclassname mapping put it into the jar with
  138. all the classes and drop the jar as first into the classpath. If the
  139. user wants to use multiple extensions, well, edit the main Services
  140. instead, dead easy.
  141. </p><p>
  142. HTH
  143. J.Pietschmann
  144. </p>
  145. </section>
  146. </body>
  147. </document>