Namespaces.xhtml 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  1. <?xml version="1.0" encoding="UTF-8" standalone="no"?>
  2. <!DOCTYPE html><html xmlns="http://www.w3.org/1999/xhtml" xmlns:epub="http://www.idpf.org/2007/ops" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:pls="http://www.w3.org/2005/01/pronunciation-lexicon" xmlns:ssml="http://www.w3.org/2001/10/synthesis" xmlns:svg="http://www.w3.org/2000/svg">
  3. <head>
  4. <title>Namespaces and compound symbols</title>
  5. <link rel="stylesheet" type="text/css" href="docbook-epub.css"/>
  6. <link rel="stylesheet" type="text/css" href="kawa.css"/>
  7. <script src="kawa-ebook.js" type="text/javascript"/>
  8. <meta name="generator" content="DocBook XSL-NS Stylesheets V1.79.1"/>
  9. <link rel="prev" href="Simple-symbols.xhtml" title="Simple symbols"/>
  10. <link rel="next" href="Keywords.xhtml" title="Keywords"/>
  11. </head>
  12. <body>
  13. <header/>
  14. <section class="sect1" title="Namespaces and compound symbols" epub:type="subchapter" id="Namespaces">
  15. <div class="titlepage">
  16. <div>
  17. <div>
  18. <h2 class="title" style="clear: both">Namespaces and compound symbols</h2>
  19. </div>
  20. </div>
  21. </div>
  22. <p>Different applications may want to use the same symbol
  23. to mean different things. To avoid such <em class="firstterm">name clashes</em>
  24. we can use <em class="firstterm">compound symbols</em>, which have two string parts:
  25. a <em class="firstterm">local name</em> and a <em class="firstterm">namespace URI</em>.
  26. The namespace-uri can be any string, but it is
  27. recommended that it have the form of an absolute
  28. <a class="ulink" href="http://en.wikipedia.org/wiki/Uniform_Resource_Identifier" target="_top">URI</a>.
  29. It would be too verbose to write the full URI all the
  30. time, so one usually uses a <em class="firstterm">namespace prefix</em>
  31. (namespace alias) as a short local alias to refer to a
  32. namespace URI.
  33. </p>
  34. <p>Compound symbols are usually written using the infix colon operator:
  35. </p>
  36. <pre class="screen"><em class="replaceable"><code>prefix</code></em><code class="literal"><span class="bold"><strong>:</strong></span></code><em class="replaceable"><code>local-name</code></em>
  37. </pre>
  38. <p>where <em class="replaceable"><code>prefix</code></em> is a namespace alias bound
  39. to some (lexically-known) namespace URI.
  40. </p>
  41. <p>Compound symbols are used for namespace-aware XML processing.
  42. </p>
  43. <section class="sect2" title="Namespace objects" epub:type="division" id="idm139667877679568">
  44. <div class="titlepage">
  45. <div>
  46. <div>
  47. <h3 class="title">Namespace objects</h3>
  48. </div>
  49. </div>
  50. </div>
  51. <p>A <em class="firstterm">namespace</em> is a mapping from strings to symbols.
  52. The string is the local-name of the resulting symbol.
  53. A namespace is similar to a Common Lisp <em class="firstterm">package</em>.
  54. </p>
  55. <p>A namespace has a namespace-uri, which a string;
  56. it is recommended that it have the form of an absolute URI.
  57. A namespace may optionally have a prefix, which is a string used
  58. when printing out symbols belonging to the namespace.
  59. (If you want “equivalent symbols” (i.e. those that have the same
  60. local-name and same uri) to be the identical symbol object, then
  61. you should use namespaces whose prefix is the empty string.)
  62. </p>
  63. <p class="synopsis" kind="Constructor"><span class="kind">Constructor</span><span class="ignore">: </span><a id="idm139667877676240" class="indexterm"/> <code class="function">namespace</code> <em class="replaceable"><code>name</code></em> [<em class="replaceable"><code>prefix</code></em>]</p>
  64. <div class="blockquote">
  65. <blockquote class="blockquote">
  66. <p>Return a namespace with the given <em class="replaceable"><code>name</code></em> and <em class="replaceable"><code>prefix</code></em>.
  67. If no such namespace exists, create it.
  68. The <em class="replaceable"><code>namespace-name</code></em> is commonly a URI, especially when working with XML,
  69. in which case it is called a <em class="replaceable"><code>namespace-uri</code></em>. However, any non-empty
  70. string is allowed.
  71. The prefix can be a string or a simple symbol.
  72. (If a symbol is used, then the symbol’s local-name is used.)
  73. The default for <em class="replaceable"><code>prefix</code></em> is the empty string.
  74. Multiple calls with the same arguments will yield the same namespace object.
  75. </p>
  76. </blockquote>
  77. </div>
  78. <p>The reader macro <code class="literal">#,namespace</code> is equivalent to the
  79. <code class="literal">namespace</code> function, but it is invoked at read-time:
  80. </p>
  81. <pre class="screen">#,(namespace "http://www.w3.org/1999/XSL/Transform" xsl)
  82. (eq? #,(namespace "foo") (namespace "foo")) ⇒ #t
  83. </pre>
  84. <p>The form <code class="literal">(,#namespace "" "")</code> returns the default <em class="firstterm">empty namespace</em>,
  85. which is used for simple symbols.
  86. </p>
  87. <p class="synopsis" kind="Procedure"><span class="kind">Procedure</span><span class="ignore">: </span><a id="idm139667877666544" class="indexterm"/> <code class="function">namespace-uri</code> <em class="replaceable"><code>namespace</code></em></p>
  88. <div class="blockquote">
  89. <blockquote class="blockquote">
  90. <p>Return the namespace-uri of the argument <em class="replaceable"><code>namespace</code></em>, as a string.
  91. </p>
  92. </blockquote>
  93. </div>
  94. <p class="synopsis" kind="Procedure"><span class="kind">Procedure</span><span class="ignore">: </span><a id="idm139667877662688" class="indexterm"/> <code class="function">namespace-prefix</code> <em class="replaceable"><code>namespace</code></em></p>
  95. <div class="blockquote">
  96. <blockquote class="blockquote">
  97. <p>Return the namespace prefix of the argument <em class="replaceable"><code>namespace</code></em>, as a string.
  98. </p>
  99. </blockquote>
  100. </div>
  101. </section>
  102. <section class="sect2" title="Compound symbols" epub:type="division" id="idm139667877658832">
  103. <div class="titlepage">
  104. <div>
  105. <div>
  106. <h3 class="title">Compound symbols</h3>
  107. </div>
  108. </div>
  109. </div>
  110. <p>A compound symbol is one that belongs to a namespace other than the
  111. default empty namespace, and (normally) has a non-empty namespace uri.
  112. (It is possible for a symbol to belong to a non-default namespace
  113. and have an empty namespace uri, but that is not recommended.)
  114. </p>
  115. <p class="synopsis" kind="Constructor"><span class="kind">Constructor</span><span class="ignore">: </span><a id="idm139667877657088" class="indexterm"/> <code class="function">symbol</code> <em class="replaceable"><code>local-name</code></em> <em class="replaceable"><code>namespace-spec</code></em></p>
  116. <p class="synopsis" kind="Constructor"><span class="kind">Constructor</span><span class="ignore">: </span><a id="idm139667877653840" class="indexterm"/> <code class="function">symbol</code> <em class="replaceable"><code>local-name</code></em> [<em class="replaceable"><code>uri</code></em> [<em class="replaceable"><code>prefix</code></em>]]</p>
  117. <div class="blockquote">
  118. <blockquote class="blockquote">
  119. <p>Construct a symbol with the given <em class="replaceable"><code>local-name</code></em> and namespace.
  120. If <em class="replaceable"><code>namespace-spec</code></em> is a namespace object, then find (or, if needed,
  121. construct) a symbol with the given <em class="replaceable"><code>local-name</code></em> belonging to the
  122. namespace. Multiple calls to <code class="literal">symbol</code> with the same namespace
  123. and <em class="replaceable"><code>local-name</code></em> will yield the same symbol object.
  124. </p>
  125. <p>If uri is a string (optionally followed by a prefix),
  126. then:
  127. </p>
  128. <pre class="screen">(symbol lname uri [prefix])
  129. </pre>
  130. <p>is equivalent to:
  131. </p>
  132. <pre class="screen">(symbol lname (namespace uri [prefix]))
  133. </pre>
  134. <p>Using <code class="literal">#t</code> for the <em class="replaceable"><code>namespace-spec</code></em> is equivalent to
  135. using the empty namespace <code class="literal">#,(namespace "")</code>.
  136. </p>
  137. <p>Using <code class="literal">#!null</code> or <code class="literal">#f</code> for the <em class="replaceable"><code>namespace-spec</code></em>
  138. creates an <em class="replaceable"><code>uninterned</code></em> symbol, which does not belong to
  139. any namespace.
  140. </p>
  141. </blockquote>
  142. </div>
  143. <p class="synopsis" kind="Procedure"><span class="kind">Procedure</span><span class="ignore">: </span><a id="idm139667877641824" class="indexterm"/> <code class="function">symbol-local-name</code> <em class="replaceable"><code>symbol</code></em></p>
  144. <div class="blockquote">
  145. <blockquote class="blockquote">
  146. <p>Return the local name of the argument symbol, as an immutable string.
  147. (The string is interned, except in the case of an uninterned symbol.)
  148. </p>
  149. </blockquote>
  150. </div>
  151. <p class="synopsis" kind="Procedure"><span class="kind">Procedure</span><span class="ignore">: </span><a id="idm139667877638320" class="indexterm"/> <code class="function">symbol-prefix</code> <em class="replaceable"><code>symbol</code></em></p>
  152. <div class="blockquote">
  153. <blockquote class="blockquote">
  154. <p>Return the prefix of the argument symbol, as an immutable
  155. (and interned) string.
  156. </p>
  157. </blockquote>
  158. </div>
  159. <p class="synopsis" kind="Procedure"><span class="kind">Procedure</span><span class="ignore">: </span><a id="idm139667877634880" class="indexterm"/> <code class="function">symbol-namespace-uri</code> <em class="replaceable"><code>symbol</code></em></p>
  160. <div class="blockquote">
  161. <blockquote class="blockquote">
  162. <p>Return the namespace uri of the argument symbol, as an immutable
  163. (and interned) string.
  164. </p>
  165. </blockquote>
  166. </div>
  167. <p class="synopsis" kind="Procedure"><span class="kind">Procedure</span><span class="ignore">: </span><a id="idm139667877631424" class="indexterm"/> <code class="function">symbol-namespace</code> <em class="replaceable"><code>symbol</code></em></p>
  168. <div class="blockquote">
  169. <blockquote class="blockquote">
  170. <p>Return the namespace object (if any) of the argument symbol.
  171. Returns <code class="literal">#!null</code> if the symbol is uninterned.
  172. </p>
  173. </blockquote>
  174. </div>
  175. <p class="synopsis" kind="Procedure"><span class="kind">Procedure</span><span class="ignore">: </span><a id="idm139667877627536" class="indexterm"/> <code class="function">symbol=?</code> <em class="replaceable"><code><em class="replaceable"><code>symbol</code></em></code></em><em class="replaceable"><code><sub>1</sub></code></em> <em class="replaceable"><code><em class="replaceable"><code>symbol</code></em></code></em><em class="replaceable"><code><sub>2</sub></code></em> <em class="replaceable"><code><em class="replaceable"><code>symbol</code></em></code></em><em class="replaceable"><code><sub>3</sub></code></em> <em class="replaceable"><code>…</code></em></p>
  176. <div class="blockquote">
  177. <blockquote class="blockquote">
  178. <p>Return <code class="literal">#t</code> if the symbols are equivalent as symbols,
  179. i.e., if their local-names and namespace-uris are the same.
  180. They may have different values of <code class="literal">symbol-prefix</code> and <code class="literal">symbol-namespace</code>.
  181. If a symbol is uninterned (or is <code class="literal">#!null</code>) then <code class="literal">symbol=?</code>
  182. returns the same result as <code class="literal">eq?</code>.
  183. </p>
  184. </blockquote>
  185. </div>
  186. <p>Two symbols are <code class="literal">equal?</code> or <code class="literal">eqv?</code> if they’re <code class="literal">symbol=?</code>.
  187. </p>
  188. </section>
  189. <section class="sect2" title="Namespace aliases" epub:type="division" id="idm139667877616896">
  190. <div class="titlepage">
  191. <div>
  192. <div>
  193. <h3 class="title">Namespace aliases</h3>
  194. </div>
  195. </div>
  196. </div>
  197. <p>A namespace is usually referenced using a shorter <em class="firstterm">namespace alias</em>,
  198. which is is a lexical definition that binds a namespace prefix
  199. to a namespace object (and thus a namespace uri).
  200. This allows using compound symbols as identifiers in Scheme programs.
  201. </p>
  202. <p class="synopsis" kind="Syntax"><span class="kind">Syntax</span><span class="ignore">: </span><a id="idm139667877614752" class="indexterm"/> <code class="function">define-namespace</code> <em class="replaceable"><code>name</code></em> <em class="replaceable"><code>namespace-name</code></em></p>
  203. <div class="blockquote">
  204. <blockquote class="blockquote">
  205. <p>Defines <em class="replaceable"><code>name</code></em> as a <em class="firstterm">namespace prefix</em> - a lexically scoped
  206. "nickname" for the namespace
  207. whose full name is <em class="replaceable"><code>namespace-name</code></em>, which should be a non-empty
  208. string literal.
  209. It is customary for the string have syntactic form of
  210. an absolute <a class="ulink" href="http://en.wikipedia.org/wiki/Uniform_Resource_Identifier" target="_top">URI</a>,
  211. but any non-empty string is acceptable and is used without
  212. further interpretation.
  213. </p>
  214. <p>Any symbols in the scope of this definitions that contain a colon, and
  215. where the part before the colon matches the <em class="replaceable"><code>name</code></em> will be
  216. treated as being in the package/namespace whose global unique name
  217. is the <em class="replaceable"><code>namespace-name</code></em>.
  218. </p>
  219. <p>Has mostly the same effect as:
  220. </p>
  221. <pre class="screen">(define-constant <em class="replaceable"><code>name</code></em> #,(namespace <em class="replaceable"><code>namespace-name</code></em>)
  222. </pre>
  223. <p>However, using <code class="literal">define-namespace</code> (rather than <code class="literal">define-constant</code>)
  224. is recommended if you want to use compound symbols as names of
  225. variables, especially local variables, or if you want to quote
  226. compound symbols.
  227. </p>
  228. <p>Note that the prefix is only visible lexically: it is not
  229. part of the namespace, or thus indirectly the symbols, and
  230. so is not available when printing the symbol.
  231. You might consider using <code class="literal">define-xml-namespace</code> as an alternative.
  232. </p>
  233. <p>A namespace is similar to a Common Lisp package,
  234. and the <em class="replaceable"><code>namespace-name</code></em> is like the name of the package.
  235. However, a namespace alias belongs to the lexical scope,
  236. while a Common Lisp package nickname is global
  237. and belongs to the package itself.
  238. </p>
  239. <p>If the namespace-name starts with the string <code class="literal">"class:"</code>, then the
  240. <em class="replaceable"><code>name</code></em> can be used for invoking Java methods
  241. (see <a class="link" href="Method-operations.xhtml" title="Calling Java methods from Scheme">Method operations</a>) and accessing fields (see <a class="link" href="Field-operations.xhtml" title="Accessing object fields">Field operations</a>).
  242. </p>
  243. <p>You can use a namespace as an abbreviation or renaming of a
  244. class name, but as a matter of style <code class="literal">define-alias</code> is preferred.
  245. </p>
  246. </blockquote>
  247. </div>
  248. <p class="synopsis" kind="Syntax"><span class="kind">Syntax</span><span class="ignore">: </span><a id="idm139667877598240" class="indexterm"/> <code class="function">define-private-namespace</code> <em class="replaceable"><code>name</code></em> <em class="replaceable"><code>namespace-name</code></em></p>
  249. <div class="blockquote">
  250. <blockquote class="blockquote">
  251. <p>Same as <code class="literal">define-namespace</code>, but the prefix <em class="replaceable"><code>name</code></em>
  252. is local to the current module.
  253. </p>
  254. </blockquote>
  255. </div>
  256. <p>For example, you might have a set of a geometry definitions
  257. defined under the namespace-uri <code class="literal">"http://foo.org/lib/geometry"</code>:
  258. </p>
  259. <pre class="screen">(define-namespace geom "http://foo.org/lib/geometry")
  260. (define (geom:translate x y)
  261. (java.awt.geom.AffineTransform:getTranslateInstance x y))
  262. (define geom:zero (geom:translate 0 0))
  263. geom:zero
  264. ⇒ AffineTransform[[1.0, 0.0, 0.0], [0.0, 1.0, 0.0]]
  265. </pre>
  266. <p>You could have some other definitions for complex math:
  267. </p>
  268. <pre class="screen">(define-namespace complex "http://foo.org/lib/math/complex")
  269. (define complex:zero +0+0i)
  270. </pre>
  271. <p>You can use a namespace-value directly in a compound name:
  272. </p>
  273. <pre class="screen">(namespace "http://foo.org/lib/geometry"):zero
  274. ⇒ AffineTransform[[1.0, 0.0, 0.0], [0.0, 1.0, 0.0]]
  275. </pre>
  276. <p>The variation <code class="literal">define-xml-namespace</code> is used for <a class="link" href="Creating-XML-nodes.xhtml" title="Creating XML nodes">Creating XML nodes</a>.
  277. </p>
  278. <p class="synopsis" kind="Syntax"><span class="kind">Syntax</span><span class="ignore">: </span><a id="idm139667877588240" class="indexterm"/> <code class="function">define-xml-namespace</code> <em class="replaceable"><code>prefix</code></em> <em class="replaceable"><code>"namespace-uri"</code></em></p>
  279. <div class="blockquote">
  280. <blockquote class="blockquote">
  281. <p>Defines a namespace with prefix <em class="replaceable"><code>prefix</code></em> and URI <em class="replaceable"><code>namespace-uri</code></em>.
  282. This is similar to <code class="literal">define-namespace</code> but with two important differences:
  283. </p>
  284. <div class="itemizedlist" epub:type="list">
  285. <ul style="list-style-type: disc; " class="itemizedlist">
  286. <li class="listitem" epub:type="list-item">
  287. <p>Every symbol in the namespace automatically maps to
  288. an element-constructor-type, as with the <code class="literal">html</code> namespace.
  289. </p>
  290. </li>
  291. <li class="listitem" epub:type="list-item">
  292. <p>The <em class="replaceable"><code>prefix</code></em> is a component of the namespace object, and
  293. hence indirectly of any symbols belongining to the namespace.
  294. </p>
  295. </li>
  296. </ul>
  297. </div>
  298. <p>Thus the definition is roughly equivalent to:
  299. </p>
  300. <pre class="screen">(define-constant <em class="replaceable"><code>name</code></em> #,(namespace <em class="replaceable"><code>namespace-name name</code></em>)
  301. </pre>
  302. <p>along with an infinite set of definitions, for every possible <em class="replaceable"><code>tag</code></em>:
  303. </p>
  304. <pre class="screen">(define (name:<em class="replaceable"><code>tag</code></em> . rest) (apply make-element 'name:<em class="replaceable"><code>tag</code></em> rest))
  305. </pre>
  306. </blockquote>
  307. </div>
  308. <pre class="screen">$ kawa --output-format xml
  309. <span class="prompt">#|kawa:1|# </span><strong class="userinput"><code>(define-xml-namespace na "Namespace1")</code></strong>
  310. <span class="prompt">#|kawa:2|# </span><strong class="userinput"><code>(define-xml-namespace nb "Namespace1")</code></strong>
  311. <span class="prompt">#|kawa:3|# </span><strong class="userinput"><code>(define xa (na:em "Info"))</code></strong>
  312. <span class="prompt">#|kawa:4|# </span><strong class="userinput"><code>xa</code></strong>
  313. &lt;na:em xmlns:na="Namespace1"&gt;Info&lt;/na:em&gt;
  314. <span class="prompt">#|kawa:5|# </span><strong class="userinput"><code>(define xb (nb:em "Info"))</code></strong>
  315. <span class="prompt">#|kawa:6|# </span><strong class="userinput"><code>xa</code></strong>
  316. &lt;nb:em xmlns:nb="Namespace1"&gt;Info&lt;/nb:em&gt;
  317. </pre>
  318. <p>Note that the prefix is part of the qualified name
  319. (it is actually part of the namespace object),
  320. and it is used when printing the tag.
  321. Two qualified names (symbols) that have the same
  322. local-name and the same namespace-name are considered
  323. equal, even if they have different prefix. You can think of
  324. the prefix as annotation used when printing, but not
  325. otherwise part of the “meaning” of a compound symbol.
  326. They are the same object if they also have the same prefix.
  327. This is an important difference from traditional Lisp/Scheme symbols,
  328. but it is how XML QNames work.
  329. </p>
  330. <pre class="screen">#|kawa:7|# (instance? xb na:em)
  331. true
  332. #|kawa:8|# (eq? 'na:em 'nb:em)
  333. false
  334. #|kawa:9|# (equal? 'na:em 'nb:em)
  335. true
  336. #|kawa:10|# (eqv? 'na:em 'nb:em)
  337. true
  338. </pre>
  339. <p>(Note that <code class="literal">#t</code> is printed as <code class="literal">true</code> when using XML formatting.)
  340. </p>
  341. <p>The predefined <code class="literal">html</code> prefix could be defined thus:
  342. </p>
  343. <pre class="screen">(define-xml-namespace html "http://www.w3.org/1999/xhtml")
  344. </pre>
  345. </section>
  346. </section>
  347. <footer>
  348. <div class="navfooter">
  349. <ul>
  350. <li>
  351. <b class="toc">
  352. <a href="Namespaces.xhtml#idm139667877679568">Namespace objects</a>
  353. </b>
  354. </li>
  355. <li>
  356. <b class="toc">
  357. <a href="Namespaces.xhtml#idm139667877658832">Compound symbols</a>
  358. </b>
  359. </li>
  360. <li>
  361. <b class="toc">
  362. <a href="Namespaces.xhtml#idm139667877616896">Namespace aliases</a>
  363. </b>
  364. </li>
  365. </ul>
  366. <p>
  367. Up: <a accesskey="u" href="Symbols-and-namespaces.xhtml">Symbols and namespaces</a></p>
  368. <p>
  369. Previous: <a accesskey="p" href="Simple-symbols.xhtml">Simple symbols</a></p>
  370. <p>
  371. Next: <a accesskey="n" href="Keywords.xhtml">Keywords</a></p>
  372. </div>
  373. </footer>
  374. </body>
  375. </html>