namespaces.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854
  1. /*
  2. * namespaces.c: Implementation of the XSLT namespaces handling
  3. *
  4. * Reference:
  5. * http://www.w3.org/TR/1999/REC-xslt-19991116
  6. *
  7. * See Copyright for the status of this software.
  8. *
  9. * daniel@veillard.com
  10. */
  11. #define IN_LIBXSLT
  12. #include "libxslt.h"
  13. #include <string.h>
  14. #ifdef HAVE_SYS_TYPES_H
  15. #include <sys/types.h>
  16. #endif
  17. #ifdef HAVE_MATH_H
  18. #include <math.h>
  19. #endif
  20. #ifdef HAVE_FLOAT_H
  21. #include <float.h>
  22. #endif
  23. #ifdef HAVE_IEEEFP_H
  24. #include <ieeefp.h>
  25. #endif
  26. #ifdef HAVE_NAN_H
  27. #include <nan.h>
  28. #endif
  29. #ifdef HAVE_CTYPE_H
  30. #include <ctype.h>
  31. #endif
  32. #ifndef XSLT_NEED_TRIO
  33. #include <stdio.h>
  34. #else
  35. #include <trio.h>
  36. #endif
  37. #include <libxml/xmlmemory.h>
  38. #include <libxml/tree.h>
  39. #include <libxml/hash.h>
  40. #include <libxml/xmlerror.h>
  41. #include <libxml/uri.h>
  42. #include "xslt.h"
  43. #include "xsltInternals.h"
  44. #include "xsltutils.h"
  45. #include "namespaces.h"
  46. #include "imports.h"
  47. /************************************************************************
  48. * *
  49. * Module interfaces *
  50. * *
  51. ************************************************************************/
  52. #ifdef XSLT_REFACTORED
  53. static xsltNsAliasPtr
  54. xsltNewNsAlias(xsltCompilerCtxtPtr cctxt)
  55. {
  56. xsltNsAliasPtr ret;
  57. if (cctxt == NULL)
  58. return(NULL);
  59. ret = (xsltNsAliasPtr) xmlMalloc(sizeof(xsltNsAlias));
  60. if (ret == NULL) {
  61. xsltTransformError(NULL, cctxt->style, NULL,
  62. "Internal error in xsltNewNsAlias(): Memory allocation failed.\n");
  63. cctxt->style->errors++;
  64. return(NULL);
  65. }
  66. memset(ret, 0, sizeof(xsltNsAlias));
  67. /*
  68. * TODO: Store the item at current stylesheet-level.
  69. */
  70. ret->next = cctxt->nsAliases;
  71. cctxt->nsAliases = ret;
  72. return(ret);
  73. }
  74. #endif /* XSLT_REFACTORED */
  75. /**
  76. * xsltNamespaceAlias:
  77. * @style: the XSLT stylesheet
  78. * @node: the xsl:namespace-alias node
  79. *
  80. * Read the stylesheet-prefix and result-prefix attributes, register
  81. * them as well as the corresponding namespace.
  82. */
  83. void
  84. xsltNamespaceAlias(xsltStylesheetPtr style, xmlNodePtr node)
  85. {
  86. xmlChar *resultPrefix = NULL;
  87. xmlChar *stylePrefix = NULL;
  88. xmlNsPtr literalNs = NULL;
  89. xmlNsPtr targetNs = NULL;
  90. #ifdef XSLT_REFACTORED
  91. xsltNsAliasPtr alias;
  92. if ((style == NULL) || (node == NULL))
  93. return;
  94. /*
  95. * SPEC XSLT 1.0:
  96. * "If a namespace URI is declared to be an alias for multiple
  97. * different namespace URIs, then the declaration with the highest
  98. * import precedence is used. It is an error if there is more than
  99. * one such declaration. An XSLT processor may signal the error;
  100. * if it does not signal the error, it must recover by choosing,
  101. * from amongst the declarations with the highest import precedence,
  102. * the one that occurs last in the stylesheet."
  103. *
  104. * SPEC TODO: Check for the errors mentioned above.
  105. */
  106. /*
  107. * NOTE that the XSLT 2.0 also *does* use the NULL namespace if
  108. * "#default" is used and there's no default namespace is scope.
  109. * I.e., this is *not* an error.
  110. * Most XSLT 1.0 implementations work this way.
  111. * The XSLT 1.0 spec has nothing to say on the subject.
  112. */
  113. /*
  114. * Attribute "stylesheet-prefix".
  115. */
  116. stylePrefix = xmlGetNsProp(node, (const xmlChar *)"stylesheet-prefix", NULL);
  117. if (stylePrefix == NULL) {
  118. xsltTransformError(NULL, style, node,
  119. "The attribute 'stylesheet-prefix' is missing.\n");
  120. return;
  121. }
  122. if (xmlStrEqual(stylePrefix, (const xmlChar *)"#default"))
  123. literalNs = xmlSearchNs(node->doc, node, NULL);
  124. else {
  125. literalNs = xmlSearchNs(node->doc, node, stylePrefix);
  126. if (literalNs == NULL) {
  127. xsltTransformError(NULL, style, node,
  128. "Attribute 'stylesheet-prefix': There's no namespace "
  129. "declaration in scope for the prefix '%s'.\n",
  130. stylePrefix);
  131. goto error;
  132. }
  133. }
  134. /*
  135. * Attribute "result-prefix".
  136. */
  137. resultPrefix = xmlGetNsProp(node, (const xmlChar *)"result-prefix", NULL);
  138. if (resultPrefix == NULL) {
  139. xsltTransformError(NULL, style, node,
  140. "The attribute 'result-prefix' is missing.\n");
  141. goto error;
  142. }
  143. if (xmlStrEqual(resultPrefix, (const xmlChar *)"#default"))
  144. targetNs = xmlSearchNs(node->doc, node, NULL);
  145. else {
  146. targetNs = xmlSearchNs(node->doc, node, resultPrefix);
  147. if (targetNs == NULL) {
  148. xsltTransformError(NULL, style, node,
  149. "Attribute 'result-prefix': There's no namespace "
  150. "declaration in scope for the prefix '%s'.\n",
  151. stylePrefix);
  152. goto error;
  153. }
  154. }
  155. /*
  156. *
  157. * Same alias for multiple different target namespace URIs:
  158. * TODO: The one with the highest import precedence is used.
  159. * Example:
  160. * <xsl:namespace-alias stylesheet-prefix="foo"
  161. * result-prefix="bar"/>
  162. *
  163. * <xsl:namespace-alias stylesheet-prefix="foo"
  164. * result-prefix="zar"/>
  165. *
  166. * Same target namespace URI for multiple different aliases:
  167. * All alias-definitions will be used.
  168. * Example:
  169. * <xsl:namespace-alias stylesheet-prefix="bar"
  170. * result-prefix="foo"/>
  171. *
  172. * <xsl:namespace-alias stylesheet-prefix="zar"
  173. * result-prefix="foo"/>
  174. * Cases using #default:
  175. * <xsl:namespace-alias stylesheet-prefix="#default"
  176. * result-prefix="#default"/>
  177. * TODO: Has this an effect at all?
  178. *
  179. * <xsl:namespace-alias stylesheet-prefix="foo"
  180. * result-prefix="#default"/>
  181. * From namespace to no namespace.
  182. *
  183. * <xsl:namespace-alias stylesheet-prefix="#default"
  184. * result-prefix="foo"/>
  185. * From no namespace to namespace.
  186. */
  187. /*
  188. * Store the ns-node in the alias-object.
  189. */
  190. alias = xsltNewNsAlias(XSLT_CCTXT(style));
  191. if (alias == NULL)
  192. return;
  193. alias->literalNs = literalNs;
  194. alias->targetNs = targetNs;
  195. XSLT_CCTXT(style)->hasNsAliases = 1;
  196. #else /* XSLT_REFACTORED */
  197. const xmlChar *literalNsName;
  198. const xmlChar *targetNsName;
  199. if ((style == NULL) || (node == NULL))
  200. return;
  201. stylePrefix = xmlGetNsProp(node, (const xmlChar *)"stylesheet-prefix", NULL);
  202. if (stylePrefix == NULL) {
  203. xsltTransformError(NULL, style, node,
  204. "namespace-alias: stylesheet-prefix attribute missing\n");
  205. return;
  206. }
  207. resultPrefix = xmlGetNsProp(node, (const xmlChar *)"result-prefix", NULL);
  208. if (resultPrefix == NULL) {
  209. xsltTransformError(NULL, style, node,
  210. "namespace-alias: result-prefix attribute missing\n");
  211. goto error;
  212. }
  213. if (xmlStrEqual(stylePrefix, (const xmlChar *)"#default")) {
  214. literalNs = xmlSearchNs(node->doc, node, NULL);
  215. if (literalNs == NULL) {
  216. literalNsName = NULL;
  217. } else
  218. literalNsName = literalNs->href; /* Yes - set for nsAlias table */
  219. } else {
  220. literalNs = xmlSearchNs(node->doc, node, stylePrefix);
  221. if ((literalNs == NULL) || (literalNs->href == NULL)) {
  222. xsltTransformError(NULL, style, node,
  223. "namespace-alias: prefix %s not bound to any namespace\n",
  224. stylePrefix);
  225. goto error;
  226. } else
  227. literalNsName = literalNs->href;
  228. }
  229. /*
  230. * When "#default" is used for result, if a default namespace has not
  231. * been explicitly declared the special value UNDEFINED_DEFAULT_NS is
  232. * put into the nsAliases table
  233. */
  234. if (xmlStrEqual(resultPrefix, (const xmlChar *)"#default")) {
  235. targetNs = xmlSearchNs(node->doc, node, NULL);
  236. if (targetNs == NULL) {
  237. targetNsName = UNDEFINED_DEFAULT_NS;
  238. } else
  239. targetNsName = targetNs->href;
  240. } else {
  241. targetNs = xmlSearchNs(node->doc, node, resultPrefix);
  242. if ((targetNs == NULL) || (targetNs->href == NULL)) {
  243. xsltTransformError(NULL, style, node,
  244. "namespace-alias: prefix %s not bound to any namespace\n",
  245. resultPrefix);
  246. goto error;
  247. } else
  248. targetNsName = targetNs->href;
  249. }
  250. /*
  251. * Special case: if #default is used for
  252. * the stylesheet-prefix (literal namespace) and there's no default
  253. * namespace in scope, we'll use style->defaultAlias for this.
  254. */
  255. if (literalNsName == NULL) {
  256. if (targetNs != NULL) {
  257. /*
  258. * BUG TODO: Is it not sufficient to have only 1 field for
  259. * this, since subsequently alias declarations will
  260. * overwrite this.
  261. * Example:
  262. * <xsl:namespace-alias result-prefix="foo"
  263. * stylesheet-prefix="#default"/>
  264. * <xsl:namespace-alias result-prefix="bar"
  265. * stylesheet-prefix="#default"/>
  266. * The mapping for "foo" won't be visible anymore.
  267. */
  268. style->defaultAlias = targetNs->href;
  269. }
  270. } else {
  271. if (style->nsAliases == NULL)
  272. style->nsAliases = xmlHashCreate(10);
  273. if (style->nsAliases == NULL) {
  274. xsltTransformError(NULL, style, node,
  275. "namespace-alias: cannot create hash table\n");
  276. goto error;
  277. }
  278. xmlHashAddEntry((xmlHashTablePtr) style->nsAliases,
  279. literalNsName, (void *) targetNsName);
  280. }
  281. #endif /* else of XSLT_REFACTORED */
  282. error:
  283. if (stylePrefix != NULL)
  284. xmlFree(stylePrefix);
  285. if (resultPrefix != NULL)
  286. xmlFree(resultPrefix);
  287. }
  288. /**
  289. * xsltGetSpecialNamespace:
  290. * @ctxt: the transformation context
  291. * @invocNode: the invoking node; e.g. a literal result element/attr;
  292. * only used for error reports
  293. * @nsName: the namespace name (or NULL)
  294. * @nsPrefix: the suggested namespace prefix (or NULL)
  295. * @target: the result element on which to anchor a namespace
  296. *
  297. * Find a matching (prefix and ns-name) ns-declaration
  298. * for the requested @nsName and @nsPrefix in the result tree.
  299. * If none is found then a new ns-declaration will be
  300. * added to @resultElem. If, in this case, the given prefix is
  301. * already in use, then a ns-declaration with a modified ns-prefix
  302. * be we created. Note that this function's priority is to
  303. * preserve ns-prefixes; it will only change a prefix if there's
  304. * a namespace clash.
  305. * If both @nsName and @nsPrefix are NULL, then this will try to
  306. * "undeclare" a default namespace by declaring an xmlns="".
  307. *
  308. * Returns a namespace declaration or NULL.
  309. */
  310. xmlNsPtr
  311. xsltGetSpecialNamespace(xsltTransformContextPtr ctxt, xmlNodePtr invocNode,
  312. const xmlChar *nsName, const xmlChar *nsPrefix,
  313. xmlNodePtr target)
  314. {
  315. xmlNsPtr ns;
  316. int prefixOccupied = 0;
  317. if ((ctxt == NULL) || (target == NULL) ||
  318. (target->type != XML_ELEMENT_NODE))
  319. return(NULL);
  320. /*
  321. * NOTE: Namespace exclusion and ns-aliasing is performed at
  322. * compilation-time in the refactored code; so this need not be done
  323. * here (it was in the old code).
  324. * NOTE: @invocNode was named @cur in the old code and was documented to
  325. * be an input node; since it was only used to anchor an error report
  326. * somewhere, we can safely change this to @invocNode, which now
  327. * will be the XSLT instruction (also a literal result element/attribute),
  328. * which was responsible for this call.
  329. */
  330. /*
  331. * OPTIMIZE TODO: This all could be optimized by keeping track of
  332. * the ns-decls currently in-scope via a specialized context.
  333. */
  334. if ((nsPrefix == NULL) && ((nsName == NULL) || (nsName[0] == 0))) {
  335. /*
  336. * NOTE: the "undeclaration" of the default namespace was
  337. * part of the logic of the old xsltGetSpecialNamespace() code,
  338. * so we'll keep that mechanism.
  339. * Related to the old code: bug #302020:
  340. */
  341. /*
  342. * OPTIMIZE TODO: This all could be optimized by keeping track of
  343. * the ns-decls currently in-scope via a specialized context.
  344. */
  345. /*
  346. * Search on the result element itself.
  347. */
  348. if (target->nsDef != NULL) {
  349. ns = target->nsDef;
  350. do {
  351. if (ns->prefix == NULL) {
  352. if ((ns->href != NULL) && (ns->href[0] != 0)) {
  353. /*
  354. * Raise a namespace normalization error.
  355. */
  356. xsltTransformError(ctxt, NULL, invocNode,
  357. "Namespace normalization error: Cannot undeclare "
  358. "the default namespace, since the default namespace "
  359. "'%s' is already declared on the result element "
  360. "'%s'.\n", ns->href, target->name);
  361. return(NULL);
  362. } else {
  363. /*
  364. * The default namespace was undeclared on the
  365. * result element.
  366. */
  367. return(NULL);
  368. }
  369. break;
  370. }
  371. ns = ns->next;
  372. } while (ns != NULL);
  373. }
  374. if ((target->parent != NULL) &&
  375. (target->parent->type == XML_ELEMENT_NODE))
  376. {
  377. /*
  378. * The parent element is in no namespace, so assume
  379. * that there is no default namespace in scope.
  380. */
  381. if (target->parent->ns == NULL)
  382. return(NULL);
  383. ns = xmlSearchNs(target->doc, target->parent,
  384. NULL);
  385. /*
  386. * Fine if there's no default ns is scope, or if the
  387. * default ns was undeclared.
  388. */
  389. if ((ns == NULL) || (ns->href == NULL) || (ns->href[0] == 0))
  390. return(NULL);
  391. /*
  392. * Undeclare the default namespace.
  393. */
  394. xmlNewNs(target, BAD_CAST "", NULL);
  395. /* TODO: Check result */
  396. return(NULL);
  397. }
  398. return(NULL);
  399. }
  400. /*
  401. * Handle the XML namespace.
  402. * QUESTION: Is this faster than using xmlStrEqual() anyway?
  403. */
  404. if ((nsPrefix != NULL) &&
  405. (nsPrefix[0] == 'x') && (nsPrefix[1] == 'm') &&
  406. (nsPrefix[2] == 'l') && (nsPrefix[3] == 0))
  407. {
  408. return(xmlSearchNs(target->doc, target, nsPrefix));
  409. }
  410. /*
  411. * First: search on the result element itself.
  412. */
  413. if (target->nsDef != NULL) {
  414. ns = target->nsDef;
  415. do {
  416. if ((ns->prefix == NULL) == (nsPrefix == NULL)) {
  417. if (ns->prefix == nsPrefix) {
  418. if (xmlStrEqual(ns->href, nsName))
  419. return(ns);
  420. prefixOccupied = 1;
  421. break;
  422. } else if (xmlStrEqual(ns->prefix, nsPrefix)) {
  423. if (xmlStrEqual(ns->href, nsName))
  424. return(ns);
  425. prefixOccupied = 1;
  426. break;
  427. }
  428. }
  429. ns = ns->next;
  430. } while (ns != NULL);
  431. }
  432. if (prefixOccupied) {
  433. /*
  434. * If the ns-prefix is occupied by an other ns-decl on the
  435. * result element, then this means:
  436. * 1) The desired prefix is shadowed
  437. * 2) There's no way around changing the prefix
  438. *
  439. * Try a desperate search for an in-scope ns-decl
  440. * with a matching ns-name before we use the last option,
  441. * which is to recreate the ns-decl with a modified prefix.
  442. */
  443. ns = xmlSearchNsByHref(target->doc, target, nsName);
  444. if (ns != NULL)
  445. return(ns);
  446. /*
  447. * Fallback to changing the prefix.
  448. */
  449. } else if ((target->parent != NULL) &&
  450. (target->parent->type == XML_ELEMENT_NODE))
  451. {
  452. /*
  453. * Try to find a matching ns-decl in the ancestor-axis.
  454. *
  455. * Check the common case: The parent element of the current
  456. * result element is in the same namespace (with an equal ns-prefix).
  457. */
  458. if ((target->parent->ns != NULL) &&
  459. ((target->parent->ns->prefix != NULL) == (nsPrefix != NULL)))
  460. {
  461. ns = target->parent->ns;
  462. if (nsPrefix == NULL) {
  463. if (xmlStrEqual(ns->href, nsName))
  464. return(ns);
  465. } else if (xmlStrEqual(ns->prefix, nsPrefix) &&
  466. xmlStrEqual(ns->href, nsName))
  467. {
  468. return(ns);
  469. }
  470. }
  471. /*
  472. * Lookup the remaining in-scope namespaces.
  473. */
  474. ns = xmlSearchNs(target->doc, target->parent, nsPrefix);
  475. if (ns != NULL) {
  476. if (xmlStrEqual(ns->href, nsName))
  477. return(ns);
  478. /*
  479. * Now check for a nasty case: We need to ensure that the new
  480. * ns-decl won't shadow a prefix in-use by an existing attribute.
  481. * <foo xmlns:a="urn:test:a">
  482. * <bar a:a="val-a">
  483. * <xsl:attribute xmlns:a="urn:test:b" name="a:b">
  484. * val-b</xsl:attribute>
  485. * </bar>
  486. * </foo>
  487. */
  488. if (target->properties) {
  489. xmlAttrPtr attr = target->properties;
  490. do {
  491. if ((attr->ns) &&
  492. xmlStrEqual(attr->ns->prefix, nsPrefix))
  493. {
  494. /*
  495. * Bad, this prefix is already in use.
  496. * Since we'll change the prefix anyway, try
  497. * a search for a matching ns-decl based on the
  498. * namespace name.
  499. */
  500. ns = xmlSearchNsByHref(target->doc, target, nsName);
  501. if (ns != NULL)
  502. return(ns);
  503. goto declare_new_prefix;
  504. }
  505. attr = attr->next;
  506. } while (attr != NULL);
  507. }
  508. } else {
  509. /*
  510. * Either no matching ns-prefix was found or the namespace is
  511. * shadowed.
  512. * Create a new ns-decl on the current result element.
  513. *
  514. * Hmm, we could also try to reuse an in-scope
  515. * namespace with a matching ns-name but a different
  516. * ns-prefix.
  517. * What has higher priority?
  518. * 1) If keeping the prefix: create a new ns-decl.
  519. * 2) If reusal: first lookup ns-names; then fallback
  520. * to creation of a new ns-decl.
  521. * REVISIT: this currently uses case 1) although
  522. * the old way was use xmlSearchNsByHref() and to let change
  523. * the prefix.
  524. */
  525. #if 0
  526. ns = xmlSearchNsByHref(target->doc, target, nsName);
  527. if (ns != NULL)
  528. return(ns);
  529. #endif
  530. }
  531. /*
  532. * Create the ns-decl on the current result element.
  533. */
  534. ns = xmlNewNs(target, nsName, nsPrefix);
  535. /* TODO: check errors */
  536. return(ns);
  537. } else {
  538. /*
  539. * This is either the root of the tree or something weird is going on.
  540. */
  541. ns = xmlNewNs(target, nsName, nsPrefix);
  542. /* TODO: Check result */
  543. return(ns);
  544. }
  545. declare_new_prefix:
  546. /*
  547. * Fallback: we need to generate a new prefix and declare the namespace
  548. * on the result element.
  549. */
  550. {
  551. xmlChar pref[30];
  552. int counter = 1;
  553. if (nsPrefix == NULL) {
  554. nsPrefix = BAD_CAST "ns";
  555. }
  556. do {
  557. snprintf((char *) pref, 30, "%s_%d", nsPrefix, counter++);
  558. ns = xmlSearchNs(target->doc, target, BAD_CAST pref);
  559. if (counter > 1000) {
  560. xsltTransformError(ctxt, NULL, invocNode,
  561. "Internal error in xsltAcquireResultInScopeNs(): "
  562. "Failed to compute a unique ns-prefix for the "
  563. "generated element");
  564. return(NULL);
  565. }
  566. } while (ns != NULL);
  567. ns = xmlNewNs(target, nsName, BAD_CAST pref);
  568. /* TODO: Check result */
  569. return(ns);
  570. }
  571. return(NULL);
  572. }
  573. /**
  574. * xsltGetNamespace:
  575. * @ctxt: a transformation context
  576. * @cur: the input node
  577. * @ns: the namespace
  578. * @out: the output node (or its parent)
  579. *
  580. * Find a matching (prefix and ns-name) ns-declaration
  581. * for the requested @ns->prefix and @ns->href in the result tree.
  582. * If none is found then a new ns-declaration will be
  583. * added to @resultElem. If, in this case, the given prefix is
  584. * already in use, then a ns-declaration with a modified ns-prefix
  585. * be we created.
  586. *
  587. * Called by:
  588. * - xsltCopyPropList() (*not* anymore)
  589. * - xsltShallowCopyElement()
  590. * - xsltCopyTreeInternal() (*not* anymore)
  591. * - xsltApplySequenceConstructor() (*not* in the refactored code),
  592. * - xsltElement() (*not* anymore)
  593. *
  594. * Returns a namespace declaration or NULL in case of
  595. * namespace fixup failures or API or internal errors.
  596. */
  597. xmlNsPtr
  598. xsltGetNamespace(xsltTransformContextPtr ctxt, xmlNodePtr cur, xmlNsPtr ns,
  599. xmlNodePtr out)
  600. {
  601. if (ns == NULL)
  602. return(NULL);
  603. #ifdef XSLT_REFACTORED
  604. /*
  605. * Namespace exclusion and ns-aliasing is performed at
  606. * compilation-time in the refactored code.
  607. * Additionally, aliasing is not intended for non Literal
  608. * Result Elements.
  609. */
  610. return(xsltGetSpecialNamespace(ctxt, cur, ns->href, ns->prefix, out));
  611. #else
  612. {
  613. xsltStylesheetPtr style;
  614. const xmlChar *URI = NULL; /* the replacement URI */
  615. if ((ctxt == NULL) || (cur == NULL) || (out == NULL))
  616. return(NULL);
  617. style = ctxt->style;
  618. while (style != NULL) {
  619. if (style->nsAliases != NULL)
  620. URI = (const xmlChar *)
  621. xmlHashLookup(style->nsAliases, ns->href);
  622. if (URI != NULL)
  623. break;
  624. style = xsltNextImport(style);
  625. }
  626. if (URI == UNDEFINED_DEFAULT_NS) {
  627. return(xsltGetSpecialNamespace(ctxt, cur, NULL, NULL, out));
  628. #if 0
  629. /*
  630. * TODO: Removed, since wrong. If there was no default
  631. * namespace in the stylesheet then this must resolve to
  632. * the NULL namespace.
  633. */
  634. xmlNsPtr dflt;
  635. dflt = xmlSearchNs(cur->doc, cur, NULL);
  636. if (dflt != NULL)
  637. URI = dflt->href;
  638. else
  639. return NULL;
  640. #endif
  641. } else if (URI == NULL)
  642. URI = ns->href;
  643. return(xsltGetSpecialNamespace(ctxt, cur, URI, ns->prefix, out));
  644. }
  645. #endif
  646. }
  647. /**
  648. * xsltGetPlainNamespace:
  649. * @ctxt: a transformation context
  650. * @cur: the input node
  651. * @ns: the namespace
  652. * @out: the result element
  653. *
  654. * Obsolete.
  655. * *Not* called by any Libxslt/Libexslt function.
  656. * Exaclty the same as xsltGetNamespace().
  657. *
  658. * Returns a namespace declaration or NULL in case of
  659. * namespace fixup failures or API or internal errors.
  660. */
  661. xmlNsPtr
  662. xsltGetPlainNamespace(xsltTransformContextPtr ctxt, xmlNodePtr cur,
  663. xmlNsPtr ns, xmlNodePtr out)
  664. {
  665. return(xsltGetNamespace(ctxt, cur, ns, out));
  666. }
  667. /**
  668. * xsltCopyNamespaceList:
  669. * @ctxt: a transformation context
  670. * @node: the target node
  671. * @cur: the first namespace
  672. *
  673. * Do a copy of an namespace list. If @node is non-NULL the
  674. * new namespaces are added automatically. This handles namespaces
  675. * aliases.
  676. * This function is intended only for *internal* use at
  677. * transformation-time for copying ns-declarations of Literal
  678. * Result Elements.
  679. *
  680. * Called by:
  681. * xsltCopyTreeInternal() (transform.c)
  682. * xsltShallowCopyElem() (transform.c)
  683. *
  684. * REVISIT: This function won't be used in the refactored code.
  685. *
  686. * Returns: a new xmlNsPtr, or NULL in case of error.
  687. */
  688. xmlNsPtr
  689. xsltCopyNamespaceList(xsltTransformContextPtr ctxt, xmlNodePtr node,
  690. xmlNsPtr cur) {
  691. xmlNsPtr ret = NULL, tmp;
  692. xmlNsPtr p = NULL,q;
  693. if (cur == NULL)
  694. return(NULL);
  695. if (cur->type != XML_NAMESPACE_DECL)
  696. return(NULL);
  697. /*
  698. * One can add namespaces only on element nodes
  699. */
  700. if ((node != NULL) && (node->type != XML_ELEMENT_NODE))
  701. node = NULL;
  702. while (cur != NULL) {
  703. if (cur->type != XML_NAMESPACE_DECL)
  704. break;
  705. /*
  706. * Avoid duplicating namespace declarations in the tree if
  707. * a matching declaration is in scope.
  708. */
  709. if (node != NULL) {
  710. if ((node->ns != NULL) &&
  711. (xmlStrEqual(node->ns->prefix, cur->prefix)) &&
  712. (xmlStrEqual(node->ns->href, cur->href))) {
  713. cur = cur->next;
  714. continue;
  715. }
  716. tmp = xmlSearchNs(node->doc, node, cur->prefix);
  717. if ((tmp != NULL) && (xmlStrEqual(tmp->href, cur->href))) {
  718. cur = cur->next;
  719. continue;
  720. }
  721. }
  722. #ifdef XSLT_REFACTORED
  723. /*
  724. * Namespace exclusion and ns-aliasing is performed at
  725. * compilation-time in the refactored code.
  726. */
  727. q = xmlNewNs(node, cur->href, cur->prefix);
  728. if (p == NULL) {
  729. ret = p = q;
  730. } else {
  731. p->next = q;
  732. p = q;
  733. }
  734. #else
  735. /*
  736. * TODO: Remove this if the refactored code gets enabled.
  737. */
  738. if (!xmlStrEqual(cur->href, XSLT_NAMESPACE)) {
  739. const xmlChar *URI;
  740. /* TODO apply cascading */
  741. URI = (const xmlChar *) xmlHashLookup(ctxt->style->nsAliases,
  742. cur->href);
  743. if (URI == UNDEFINED_DEFAULT_NS) {
  744. cur = cur->next;
  745. continue;
  746. }
  747. if (URI != NULL) {
  748. q = xmlNewNs(node, URI, cur->prefix);
  749. } else {
  750. q = xmlNewNs(node, cur->href, cur->prefix);
  751. }
  752. if (p == NULL) {
  753. ret = p = q;
  754. } else {
  755. p->next = q;
  756. p = q;
  757. }
  758. }
  759. #endif
  760. cur = cur->next;
  761. }
  762. return(ret);
  763. }
  764. /**
  765. * xsltCopyNamespace:
  766. * @ctxt: a transformation context
  767. * @elem: the target element node
  768. * @ns: the namespace node
  769. *
  770. * Copies a namespace node (declaration). If @elem is not NULL,
  771. * then the new namespace will be declared on @elem.
  772. *
  773. * Returns: a new xmlNsPtr, or NULL in case of an error.
  774. */
  775. xmlNsPtr
  776. xsltCopyNamespace(xsltTransformContextPtr ctxt ATTRIBUTE_UNUSED,
  777. xmlNodePtr elem, xmlNsPtr ns)
  778. {
  779. if ((ns == NULL) || (ns->type != XML_NAMESPACE_DECL))
  780. return(NULL);
  781. /*
  782. * One can add namespaces only on element nodes
  783. */
  784. if ((elem != NULL) && (elem->type != XML_ELEMENT_NODE))
  785. return(xmlNewNs(NULL, ns->href, ns->prefix));
  786. else
  787. return(xmlNewNs(elem, ns->href, ns->prefix));
  788. }
  789. /**
  790. * xsltFreeNamespaceAliasHashes:
  791. * @style: an XSLT stylesheet
  792. *
  793. * Free up the memory used by namespaces aliases
  794. */
  795. void
  796. xsltFreeNamespaceAliasHashes(xsltStylesheetPtr style) {
  797. if (style->nsAliases != NULL)
  798. xmlHashFree((xmlHashTablePtr) style->nsAliases, NULL);
  799. style->nsAliases = NULL;
  800. }