← Back to team overview

zorba-coders team mailing list archive

[Merge] lp:~fcavalieri/zorba/identifiers into lp:zorba

 

Federico Cavalieri has proposed merging lp:~fcavalieri/zorba/identifiers into lp:zorba.

Requested reviews:
  Markos Zaharioudakis (markos-za)

For more details, see:
https://code.launchpad.net/~fcavalieri/zorba/identifiers/+merge/77693

Changed the implementation of the reference module.
A map nodes->reference and a map reference->nodes is now in the store.
References work after updates to trees.
References can be obtained any node not only nodes in a collection.
References are now base on uuids.

Since last proposal
Added missing ref1 test expected result.
-- 
https://code.launchpad.net/~fcavalieri/zorba/identifiers/+merge/77693
Your team Zorba Coders is subscribed to branch lp:zorba.
=== modified file 'include/zorba/pregenerated/diagnostic_list.h'
--- include/zorba/pregenerated/diagnostic_list.h	2011-08-17 23:04:48 +0000
+++ include/zorba/pregenerated/diagnostic_list.h	2011-09-30 12:22:26 +0000
@@ -488,6 +488,10 @@
 
 extern ZORBA_DLL_PUBLIC ZorbaErrorCode ZAPI0028_INVALID_NODE_URI;
 
+extern ZORBA_DLL_PUBLIC ZorbaErrorCode ZAPI0029_REFERENCE_ALREADY_PRESENT;
+
+extern ZORBA_DLL_PUBLIC ZorbaErrorCode ZAPI0030_NO_CURRENT_REFERENCE;
+
 extern ZORBA_DLL_PUBLIC ZorbaErrorCode ZAPI0039_XQUERY_HAS_ITERATOR_ALREADY;
 
 extern ZORBA_DLL_PUBLIC ZorbaErrorCode ZAPI0040_ITERATOR_NOT_OPEN;

=== modified file 'modules/com/zorba-xquery/www/modules/node-reference.xq'
--- modules/com/zorba-xquery/www/modules/node-reference.xq	2011-08-26 23:36:24 +0000
+++ modules/com/zorba-xquery/www/modules/node-reference.xq	2011-09-30 12:22:26 +0000
@@ -18,14 +18,19 @@
 
 (:~
  : The module provides functions to compute a stable reference (URI) for
- : a node that is stored in a collection and vice versa.
+ : any node, either temporary or stored in a collection and for retrieving
+ : nodes given their identifier. The identifiers are immutable, i.e. a node 
+ : identifier do not change during the node lifetime. Identifiers are also 
+ : unique in that an already generated identifier cannot be generated again.
+ : Morever a node, at any time during its lifetime, can be retrieved by its 
+ : identifier.
  :
  : <p>Please see the <a href="../../html/data_lifecycle.html">data lifecycle
  : documentation</a> about details on management and manipulation of collections.</p>
  : 
  : @see <a href="../../html/data_lifecycle.html">Data Lifecycle</a>
  :
- : @author Matthias Brantner
+ : @author Federico Cavalieri
  :
  : @project XDM/node
  :
@@ -41,9 +46,6 @@
  : Compute a stable and opaque node reference (with type xs:anyURI) for
  : a given node.
  :
- : <p>The function can only compute reference for nodes that a stored
- : in a collection [zerr:ZAPI0080].</p>
- :
  : <p>The returned URI is stable, i.e. it still can be dereferenced if
  : the node or the containing collection is modified.</p>
  :
@@ -58,7 +60,7 @@
 (:~
  : Returns the node identified by the given node reference.
  :
- : <p>The function may return the empty sequence if the node
+ : <p>The function returns the empty sequence if the node
  : that is referenced was deleted.</p>
  :
  : @param $arg the URI of the node to retrieve.
@@ -72,3 +74,15 @@
 declare function ref:node-by-reference(
   $arg as xs:anyURI
 ) as node()? external;
+
+
+(:~
+ : Returns whether a reference has already been generated for the given node. 
+ :  
+ : @param $node a node 
+ :
+ : @return whether an reference has already been generated the given node.
+ :)
+declare function ref:has-reference(
+  $node as node()  
+) as xs:boolean external;
\ No newline at end of file

=== modified file 'modules/com/zorba-xquery/www/modules/pregenerated/errors.xq'
--- modules/com/zorba-xquery/www/modules/pregenerated/errors.xq	2011-08-17 23:04:48 +0000
+++ modules/com/zorba-xquery/www/modules/pregenerated/errors.xq	2011-09-30 12:22:26 +0000
@@ -277,6 +277,14 @@
 
 (:~
 :)
+declare variable $zerr:ZAPI0029 as xs:QName := fn:QName($zerr:NS, "zerr:ZAPI0029");
+
+(:~
+:)
+declare variable $zerr:ZAPI0030 as xs:QName := fn:QName($zerr:NS, "zerr:ZAPI0030");
+
+(:~
+:)
 declare variable $zerr:ZAPI0039 as xs:QName := fn:QName($zerr:NS, "zerr:ZAPI0039");
 
 (:~

=== modified file 'src/compiler/rewriter/tools/expr_tools.cpp'
--- src/compiler/rewriter/tools/expr_tools.cpp	2011-08-12 10:21:10 +0000
+++ src/compiler/rewriter/tools/expr_tools.cpp	2011-09-30 12:22:26 +0000
@@ -57,6 +57,31 @@
     return false;
   }
 
+  /*
+   * This is an hack for avoiding variable substitution when a variable is used
+   * as argument in a node:reference() function. This avoids that an expression
+   * like let $x:=<x/> return ref:node-by-reference(ref:node-reference($x)) get
+   * rewritten into ref:node-by-reference(ref:node-reference(</x>)). In the
+   * latter case </x> is  cannot be retrieved. The variable used inside the 
+   * node-reference function are counted twice, thus EliminateUnusedLetVars 
+   * do not substitute them.
+   */
+  if (e->get_expr_kind() == fo_expr_kind)
+  {
+    const fo_expr* fo = static_cast<const fo_expr *>(&*e);
+    const function* fn = fo->get_func();
+    if (fn->getKind() == FunctionConsts::FN_ZORBA_REF_NODE_REFERENCE_1)
+    {
+      ExprConstIterator iter(e);
+      while (!iter.done())
+      {
+        if (!count_variable_uses_rec(iter.get_expr(), var, rCtx, limit, count))
+          break;
+        iter.next();
+      }
+    }
+  }
+
   if (e == var)
   {
     ++count;

=== modified file 'src/diagnostics/diagnostic_en.xml'
--- src/diagnostics/diagnostic_en.xml	2011-09-16 21:58:20 +0000
+++ src/diagnostics/diagnostic_en.xml	2011-09-30 12:22:26 +0000
@@ -1767,6 +1767,14 @@
     <diagnostic code="ZAPI0028" name="INVALID_NODE_URI">
       <value>"$1": invalid node URI</value>
     </diagnostic>
+    
+    <diagnostic code="ZAPI0029" name="REFERENCE_ALREADY_PRESENT">
+      <value>"$1": reference already present in the store</value>
+    </diagnostic>
+    
+    <diagnostic code="ZAPI0030" name="NO_CURRENT_REFERENCE">
+      <value>node has no current reference</value>
+    </diagnostic>
 
     <diagnostic code="ZAPI0039" name="XQUERY_HAS_ITERATOR_ALREADY">
       <value>XQuery has iterator already</value>

=== modified file 'src/diagnostics/pregenerated/diagnostic_list.cpp'
--- src/diagnostics/pregenerated/diagnostic_list.cpp	2011-08-17 23:04:48 +0000
+++ src/diagnostics/pregenerated/diagnostic_list.cpp	2011-09-30 12:22:26 +0000
@@ -711,6 +711,12 @@
 ZorbaErrorCode ZAPI0028_INVALID_NODE_URI( "ZAPI0028" );
 
 
+ZorbaErrorCode ZAPI0029_REFERENCE_ALREADY_PRESENT( "ZAPI0029" );
+
+
+ZorbaErrorCode ZAPI0030_NO_CURRENT_REFERENCE( "ZAPI0030" );
+
+
 ZorbaErrorCode ZAPI0039_XQUERY_HAS_ITERATOR_ALREADY( "ZAPI0039" );
 
 

=== modified file 'src/diagnostics/pregenerated/dict_en.cpp'
--- src/diagnostics/pregenerated/dict_en.cpp	2011-09-16 21:58:20 +0000
+++ src/diagnostics/pregenerated/dict_en.cpp	2011-09-30 12:22:26 +0000
@@ -238,6 +238,8 @@
   { "ZAPI0021", "\"$1\": item to load is not a node" },
   { "ZAPI0027", "cannot update dynamic context with iterators" },
   { "ZAPI0028", "\"$1\": invalid node URI" },
+  { "ZAPI0029", "\"$1\": reference already present in the store" },
+  { "ZAPI0030", "node has no current reference" },
   { "ZAPI0039", "XQuery has iterator already" },
   { "ZAPI0040", "iterator is not open" },
   { "ZAPI0041", "iterator is already open" },

=== modified file 'src/functions/pregenerated/func_nodes.cpp'
--- src/functions/pregenerated/func_nodes.cpp	2011-08-04 02:39:56 +0000
+++ src/functions/pregenerated/func_nodes.cpp	2011-09-30 12:22:26 +0000
@@ -41,6 +41,16 @@
   return new NodeReferenceIterator(sctx, loc, argv);
 }
 
+PlanIter_t fn_zorba_ref_has_reference::codegen(
+  CompilerCB*,
+  static_context* sctx,
+  const QueryLoc& loc,
+  std::vector<PlanIter_t>& argv,
+  AnnotationHolder& ann) const
+{
+  return new HasReferenceIterator(sctx, loc, argv);
+}
+
 PlanIter_t fn_zorba_ref_node_by_reference::codegen(
   CompilerCB*,
   static_context* sctx,
@@ -248,6 +258,18 @@
   {
     
 
+    DECL_WITH_KIND(sctx, fn_zorba_ref_has_reference,
+        (createQName("http://www.zorba-xquery.com/modules/node-reference","","has-reference";), 
+        GENV_TYPESYSTEM.ANY_NODE_TYPE_ONE, 
+        GENV_TYPESYSTEM.BOOLEAN_TYPE_ONE),
+        FunctionConsts::FN_ZORBA_REF_HAS_REFERENCE_1);
+
+  }
+
+
+  {
+    
+
     DECL_WITH_KIND(sctx, fn_zorba_ref_node_by_reference,
         (createQName("http://www.zorba-xquery.com/modules/node-reference","","node-by-reference";), 
         GENV_TYPESYSTEM.ANY_URI_TYPE_ONE, 

=== modified file 'src/functions/pregenerated/func_nodes.h'
--- src/functions/pregenerated/func_nodes.h	2011-06-16 19:47:58 +0000
+++ src/functions/pregenerated/func_nodes.h	2011-09-30 12:22:26 +0000
@@ -51,6 +51,19 @@
 };
 
 
+//fn-zorba-ref:has-reference
+class fn_zorba_ref_has_reference : public function
+{
+public:
+  fn_zorba_ref_has_reference(const signature& sig, FunctionConsts::FunctionKind kind)
+    : function(sig, kind) {
+
+}
+
+  CODEGEN_DECL();
+};
+
+
 //fn-zorba-ref:node-by-reference
 class fn_zorba_ref_node_by_reference : public function
 {

=== modified file 'src/functions/pregenerated/function_enum.h'
--- src/functions/pregenerated/function_enum.h	2011-08-17 20:22:07 +0000
+++ src/functions/pregenerated/function_enum.h	2011-09-30 12:22:26 +0000
@@ -194,6 +194,7 @@
   FN_ZORBA_MATH_MODF_1,
   FN_ZORBA_MATH_FREXP_1,
   FN_ZORBA_REF_NODE_REFERENCE_1,
+  FN_ZORBA_REF_HAS_REFERENCE_1,
   FN_ZORBA_REF_NODE_BY_REFERENCE_1,
   FN_LOCAL_NAME_0,
   FN_LOCAL_NAME_1,

=== modified file 'src/runtime/nodes/nodes_impl.cpp'
--- src/runtime/nodes/nodes_impl.cpp	2011-08-04 02:39:56 +0000
+++ src/runtime/nodes/nodes_impl.cpp	2011-09-30 12:22:26 +0000
@@ -33,43 +33,67 @@
 
 namespace zorba {
 
-bool NodeReferenceIterator::nextImpl(store::Item_t& result, PlanState& planState) const
-{
-  PlanIteratorState *state;
-  bool valid;
-  store::Item_t inNode;
-  DEFAULT_STACK_INIT(PlanIteratorState, state, planState);
-
-  valid = consumeNext(inNode, theChildren[0], planState);
-  assert(valid);
-
-  if (! inNode->getCollection() )
-  {
-    throw XQUERY_EXCEPTION(
-      zerr::ZAPI0080_CANNOT_RETRIEVE_NODE_REFERENCE,
-      ERROR_LOC( loc )
-    );
-  }
-
-  STACK_PUSH(GENV_STORE.getReference(result, inNode), state);
-  STACK_END(state);
-}
-
-bool NodeByReferenceIterator::nextImpl(store::Item_t& result, PlanState& planState) const
-{
-  store::Item_t inUri;
-  bool valid;
-
-  PlanIteratorState *state;
-  DEFAULT_STACK_INIT(PlanIteratorState, state, planState);
-
-  valid = consumeNext(inUri, theChildren[0], planState);
-  assert(valid);
-
-  STACK_PUSH(GENV_STORE.getNodeByReference(result, inUri), state);
-  STACK_END (state);
-}
-
+/*******************************************************************************
+********************************************************************************/
+bool
+NodeReferenceIterator::nextImpl(store::Item_t& aResult, PlanState& aPlanState) const
+{
+  store::Item_t lNode;
+  store::Item_t lGenerateIdentifier;
+  zstring lNodeId;
+
+  PlanIteratorState *state;
+  DEFAULT_STACK_INIT(PlanIteratorState, state, aPlanState);
+
+  consumeNext(lNode, theChildren[0].getp(), aPlanState);
+
+  STACK_PUSH(GENV_STORE.getReference(aResult, lNode), state);
+
+  STACK_END (state);
+}
+
+/*******************************************************************************
+********************************************************************************/
+bool
+HasReferenceIterator::nextImpl(store::Item_t& aResult, PlanState& aPlanState) const
+{
+  store::Item_t lNode;
+
+  PlanIteratorState *state;
+  DEFAULT_STACK_INIT(PlanIteratorState, state, aPlanState);
+
+  consumeNext(lNode, theChildren[0].getp(), aPlanState);
+
+  STACK_PUSH(GENV_ITEMFACTORY->createBoolean(aResult, GENV_STORE.hasReference(lNode)), state);
+
+  STACK_END (state);
+}
+
+/*******************************************************************************
+********************************************************************************/
+bool
+NodeByReferenceIterator::nextImpl(store::Item_t& result, PlanState& planState) const
+{
+    store::Item_t lUUID;
+    bool haveResult;
+
+    PlanIteratorState *state;
+    DEFAULT_STACK_INIT(PlanIteratorState, state, planState);
+
+    consumeNext(lUUID, theChildren[0].getp(), planState);
+    try
+    {
+      haveResult=GENV_STORE.getNodeByReference(result, lUUID->getStringValue());
+    }
+    catch (ZorbaException& e)
+    {
+      set_source( e, loc );
+      throw;
+    }
+    STACK_PUSH(haveResult, state);
+
+    STACK_END (state);
+}
 
 // 14.2 fn:local-name
 //---------------------
@@ -354,9 +378,9 @@
           // need to convert the opaque uri into a valid ncname
           // e.g. z0.1.1.c.50
 #ifndef NDEBUG
-          ZORBA_ASSERT( uri_string.find_first_of("zorba:") == 0 );
+          ZORBA_ASSERT( uri_string.find_first_of("uuid:") == 0 );
 #endif
-          lRes = "z" + uri_string.substr(uri_string.find_first_of(":") + 1);
+          lRes = "u" + uri_string.substr(uri_string.find_first_of(":") + 1);
         }
       }
       retval = GENV_ITEMFACTORY->createString(result, lRes);

=== modified file 'src/runtime/nodes/pregenerated/nodes.cpp'
--- src/runtime/nodes/pregenerated/nodes.cpp	2011-06-16 19:47:58 +0000
+++ src/runtime/nodes/pregenerated/nodes.cpp	2011-09-30 12:22:26 +0000
@@ -60,6 +60,34 @@
 // </NodeReferenceIterator>
 
 
+// <HasReferenceIterator>
+const char* HasReferenceIterator::class_name_str = "HasReferenceIterator";
+HasReferenceIterator::class_factory<HasReferenceIterator>
+HasReferenceIterator::g_class_factory;
+
+const serialization::ClassVersion 
+HasReferenceIterator::class_versions[] ={{ 1, 0x000905, false}};
+
+const int HasReferenceIterator::class_versions_count =
+sizeof(HasReferenceIterator::class_versions)/sizeof(struct serialization::ClassVersion);
+
+void HasReferenceIterator::accept(PlanIterVisitor& v) const {
+  v.beginVisit(*this);
+
+  std::vector<PlanIter_t>::const_iterator lIter = theChildren.begin();
+  std::vector<PlanIter_t>::const_iterator lEnd = theChildren.end();
+  for ( ; lIter != lEnd; ++lIter ){
+    (*lIter)->accept(v);
+  }
+
+  v.endVisit(*this);
+}
+
+HasReferenceIterator::~HasReferenceIterator() {}
+
+// </HasReferenceIterator>
+
+
 // <NodeByReferenceIterator>
 const char* NodeByReferenceIterator::class_name_str = "NodeByReferenceIterator";
 NodeByReferenceIterator::class_factory<NodeByReferenceIterator>

=== modified file 'src/runtime/nodes/pregenerated/nodes.h'
--- src/runtime/nodes/pregenerated/nodes.h	2011-06-16 19:47:58 +0000
+++ src/runtime/nodes/pregenerated/nodes.h	2011-09-30 12:22:26 +0000
@@ -34,10 +34,10 @@
 namespace zorba {
 
 /**
- * 
- *  Iterator to implement the zorba:node-reference function.
- *  
- * Author: Zorba Team
+ *        
+ *      declare function ref:node-reference($node as node) as xs:anyURI
+ *    
+ * Author: Federico Cavalieri
  */
 class NodeReferenceIterator : public NaryBaseIterator<NodeReferenceIterator, PlanIteratorState>
 { 
@@ -71,9 +71,45 @@
 
 /**
  * 
- *      Iterator to implement the zorba:node-by-reference function.
- *    
- * Author: Zorba Team
+ *      declare function id:has-reference($node as node) as xs:boolean
+ *    
+ * Author: Federico Cavalieri
+ */
+class HasReferenceIterator : public NaryBaseIterator<HasReferenceIterator, PlanIteratorState>
+{ 
+public:
+  SERIALIZABLE_CLASS(HasReferenceIterator);
+
+  SERIALIZABLE_CLASS_CONSTRUCTOR2T(HasReferenceIterator,
+    NaryBaseIterator<HasReferenceIterator, PlanIteratorState>);
+
+  void serialize( ::zorba::serialization::Archiver& ar)
+  {
+    serialize_baseclass(ar,
+    (NaryBaseIterator<HasReferenceIterator, PlanIteratorState>*)this);
+  }
+
+  HasReferenceIterator(
+    static_context* sctx,
+    const QueryLoc& loc,
+    std::vector<PlanIter_t>& children)
+    : 
+    NaryBaseIterator<HasReferenceIterator, PlanIteratorState>(sctx, loc, children)
+  {}
+
+  virtual ~HasReferenceIterator();
+
+  void accept(PlanIterVisitor& v) const;
+
+  bool nextImpl(store::Item_t& result, PlanState& aPlanState) const;
+};
+
+
+/**
+ * 
+ *      declare function id:node-by-identifier($identifier as xs:string) as node()?
+ *    
+ * Author: Federico Cavalieri
  */
 class NodeByReferenceIterator : public NaryBaseIterator<NodeByReferenceIterator, PlanIteratorState>
 { 

=== modified file 'src/runtime/spec/nodes/nodes.xml'
--- src/runtime/spec/nodes/nodes.xml	2011-08-04 02:39:56 +0000
+++ src/runtime/spec/nodes/nodes.xml	2011-09-30 12:22:26 +0000
@@ -11,45 +11,70 @@
   xmlns:zorba="http://www.zorba-xquery.com";
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance";
   xsi:schemaLocation="http://www.zorba-xquery.com ../runtime.xsd">
-    
-<!--
-/*******************************************************************************
-********************************************************************************/
--->
-<zorba:iterator name="NodeReferenceIterator">
-
-  <zorba:description author="Zorba Team">
-  Iterator to implement the zorba:node-reference function.
-  </zorba:description>
-
-  <zorba:function>
-    <zorba:signature localname="node-reference" prefix="fn-zorba-ref">
-      <zorba:param>node()</zorba:param>
-      <zorba:output>xs:anyURI</zorba:output>
-    </zorba:signature>
-  </zorba:function>
-
-</zorba:iterator>
-
-<!--
-/*******************************************************************************
-********************************************************************************/
--->
-<zorba:iterator name="NodeByReferenceIterator">
-
-    <zorba:description author="Zorba Team">
-      Iterator to implement the zorba:node-by-reference function.
-    </zorba:description>
-
-    <zorba:function>
-      <zorba:signature localname="node-by-reference" prefix="fn-zorba-ref">
-        <zorba:param>xs:anyURI</zorba:param>
+
+<!--
+/*******************************************************************************
+********************************************************************************/
+-->
+  <zorba:iterator name="NodeReferenceIterator">
+
+    <zorba:description author="Federico Cavalieri">       
+      declare function ref:node-reference($node as node) as xs:anyURI
+    </zorba:description>
+    
+    <zorba:function>
+    
+      <zorba:signature localname="node-reference" prefix="fn-zorba-ref">
+        <zorba:param>node()</zorba:param>   
+        <zorba:output>xs:anyURI</zorba:output>
+      </zorba:signature>
+      
+    </zorba:function>
+
+  </zorba:iterator>
+
+<!--
+/*******************************************************************************
+********************************************************************************/
+-->
+  <zorba:iterator name="HasReferenceIterator">
+
+    <zorba:description author="Federico Cavalieri">
+      declare function id:has-reference($node as node) as xs:boolean
+    </zorba:description>
+    
+    <zorba:function>
+    
+      <zorba:signature localname="has-reference" prefix="fn-zorba-ref">
+        <zorba:param>node()</zorba:param>                
+        <zorba:output>xs:boolean</zorba:output>
+      </zorba:signature>
+      
+    </zorba:function>
+
+  </zorba:iterator>
+
+<!--
+/*******************************************************************************
+********************************************************************************/
+-->
+  <zorba:iterator name="NodeByReferenceIterator">
+
+    <zorba:description author="Federico Cavalieri">
+      declare function id:node-by-identifier($identifier as xs:string) as node()?
+    </zorba:description>
+
+    <zorba:function>
+    
+      <zorba:signature localname="node-by-reference" prefix="fn-zorba-ref">        
+        <zorba:param>xs:anyURI</zorba:param>        
         <zorba:output>node()?</zorba:output>
       </zorba:signature>
+      
     </zorba:function>
-
-</zorba:iterator>
-
+        
+  </zorba:iterator>
+    
 <!--
 /*******************************************************************************
 ********************************************************************************/

=== modified file 'src/runtime/visitors/pregenerated/planiter_visitor.h'
--- src/runtime/visitors/pregenerated/planiter_visitor.h	2011-08-17 20:22:07 +0000
+++ src/runtime/visitors/pregenerated/planiter_visitor.h	2011-09-30 12:22:26 +0000
@@ -305,6 +305,8 @@
 
     class NodeReferenceIterator;
 
+    class HasReferenceIterator;
+
     class NodeByReferenceIterator;
 
     class FnLocalNameIterator;
@@ -958,6 +960,9 @@
     virtual void beginVisit ( const NodeReferenceIterator& ) = 0;
     virtual void endVisit   ( const NodeReferenceIterator& ) = 0;
 
+    virtual void beginVisit ( const HasReferenceIterator& ) = 0;
+    virtual void endVisit   ( const HasReferenceIterator& ) = 0;
+
     virtual void beginVisit ( const NodeByReferenceIterator& ) = 0;
     virtual void endVisit   ( const NodeByReferenceIterator& ) = 0;
 

=== modified file 'src/runtime/visitors/pregenerated/printer_visitor.cpp'
--- src/runtime/visitors/pregenerated/printer_visitor.cpp	2011-08-17 20:22:07 +0000
+++ src/runtime/visitors/pregenerated/printer_visitor.cpp	2011-09-30 12:22:26 +0000
@@ -2027,6 +2027,20 @@
 // </NodeReferenceIterator>
 
 
+// <HasReferenceIterator>
+void PrinterVisitor::beginVisit ( const HasReferenceIterator& a) {
+  thePrinter.startBeginVisit("HasReferenceIterator", ++theId);
+  printCommons( &a, theId );
+  thePrinter.endBeginVisit( theId );
+}
+
+void PrinterVisitor::endVisit ( const HasReferenceIterator& ) {
+  thePrinter.startEndVisit();
+  thePrinter.endEndVisit();
+}
+// </HasReferenceIterator>
+
+
 // <NodeByReferenceIterator>
 void PrinterVisitor::beginVisit ( const NodeByReferenceIterator& a) {
   thePrinter.startBeginVisit("NodeByReferenceIterator", ++theId);

=== modified file 'src/runtime/visitors/pregenerated/printer_visitor.h'
--- src/runtime/visitors/pregenerated/printer_visitor.h	2011-08-17 20:22:07 +0000
+++ src/runtime/visitors/pregenerated/printer_visitor.h	2011-09-30 12:22:26 +0000
@@ -460,6 +460,9 @@
     void beginVisit( const NodeReferenceIterator& );
     void endVisit  ( const NodeReferenceIterator& );
 
+    void beginVisit( const HasReferenceIterator& );
+    void endVisit  ( const HasReferenceIterator& );
+
     void beginVisit( const NodeByReferenceIterator& );
     void endVisit  ( const NodeByReferenceIterator& );
 

=== modified file 'src/store/api/copymode.h'
--- src/store/api/copymode.h	2011-06-14 17:26:33 +0000
+++ src/store/api/copymode.h	2011-09-30 12:22:26 +0000
@@ -34,13 +34,15 @@
   bool  theTypePreserve;
   bool  theNsPreserve;
   bool  theNsInherit;
+  bool	theFreezeReference;
 
   CopyMode()
     :
     theDoCopy(true),
     theTypePreserve(true),
     theNsPreserve(true),
-    theNsInherit(true)
+    theNsInherit(true),
+    theFreezeReference(false)
   {
   }
 
@@ -49,7 +51,8 @@
     theDoCopy(aCopyMode.theDoCopy),
     theTypePreserve(aCopyMode.theTypePreserve),
     theNsPreserve(aCopyMode.theNsPreserve),
-    theNsInherit(aCopyMode.theNsInherit)
+    theNsInherit(aCopyMode.theNsInherit),
+    theFreezeReference(aCopyMode.theFreezeReference)
   {
   }
 
@@ -57,12 +60,14 @@
         bool copy,
         bool typePreserve,
         bool nsPreserve,
-        bool nsInherit)
+        bool nsInherit,
+        bool freezeReference=false)
   {
     theDoCopy = copy;
     theTypePreserve = typePreserve;
     theNsPreserve = nsPreserve;
     theNsInherit = nsInherit;
+    theFreezeReference=freezeReference;
   }
 
   std::string toString() const
@@ -76,6 +81,8 @@
     s += (theNsPreserve ? "T" : "F");
     s += " ";
     s += (theNsInherit ? "T" : "F");
+    s += " ";
+    s += (theFreezeReference ? "T" : "F");
     s += "]";
     return s;
   }

=== modified file 'src/store/api/store.h'
--- src/store/api/store.h	2011-09-27 10:43:46 +0000
+++ src/store/api/store.h	2011-09-30 12:22:26 +0000
@@ -87,26 +87,35 @@
    */
   virtual TempSeq_t createTempSeq(bool lazy) = 0;
 
-
-  /* -------------------------- Reference Management --------------------------*/
-		
-  /** 
-   * Computes the URI of the passed item.
-   *
-   * @param item XDM item
-   * @return Returns an item of type xs:uri 
-   */
-  virtual bool getReference(Item_t& result, const Item* node) = 0;
-		
-  /**
-   * Returns Item which is identified by a reference
-   *
-   * @param uri Has to be an xs:URI item
-   * @returns referenced item if it exists, otherwise NULL
-   */
-  virtual bool getNodeByReference(Item_t& result, const Item* uri) = 0;
-		
-		
+  /* ----------------------- Node Reference Management -------------------------*/
+
+  /**
+   * Computes the reference of the given node.
+   *
+   * @param node XDM node
+   * @return the identifier as an item of type xs:anyURI
+   */
+  virtual bool getReference(Item_t& result, Item* node) = 0;
+
+  /**
+   * Returns whether a reference has already been generated for the given node.
+   *
+   * @param item XDM node
+   * @return whether a reference has already been generated for the given node.
+   */
+  virtual bool hasReference(const Item* node) = 0;
+
+  /**
+   * Returns the node which is identified by the given reference.
+   * If the given reference is not a valid reference, as generated by
+   * getReference, error ZAPI0028 is raised.
+   *
+   * @param reference an xs:anyURI item
+   * @returns the node identified by the reference, NULL otherwise
+   */
+  virtual bool getNodeByReference(store::Item_t& result, const zstring& reference) = 0;
+
+
   /* --------------------------- Node Id Management ---------------------------*/
 
   /**

=== modified file 'src/store/naive/node_items.cpp'
--- src/store/naive/node_items.cpp	2011-09-23 14:26:00 +0000
+++ src/store/naive/node_items.cpp	2011-09-30 12:22:26 +0000
@@ -693,6 +693,7 @@
   }
 #endif
 
+  GET_STORE().unregisterNode(this);
   delete this;
 }
 
@@ -1387,6 +1388,10 @@
     throw;
   }
 
+  if (haveFrozenReference() || copymode.theFreezeReference)
+    GET_STORE().copyReference(this,copyNode);
+
+
   NODE_TRACE1("}");
   NODE_TRACE1("Copied doc node " << this << " to node " << copyNode);
 
@@ -2011,6 +2016,9 @@
     throw;
   }
 
+  if (haveFrozenReference() || copymode.theFreezeReference)
+    GET_STORE().copyReference(this,copyNode);
+
   NODE_TRACE1("Copied elem node " << this << " to node " << copyNode
               << " name = " << theName->getStringValue() << " parent = "
               << (parent ? parent : 0x0)
@@ -3125,6 +3133,9 @@
     throw;
   }
 
+  if (haveFrozenReference() || copymode.theFreezeReference)
+    GET_STORE().copyReference(this,copyNode);
+
   NODE_TRACE1("Copied attribute node " << this << " to node " << copyNode
               << " name = " << theName->show() << " parent = "
               << std::hex << (parent ? (ulong)parent : 0) << " pos = " << pos
@@ -3593,6 +3604,9 @@
     throw;
   }
 
+  if (haveFrozenReference() || copymode.theFreezeReference)
+    GET_STORE().copyReference(this,copyNode);
+
   NODE_TRACE1("Copied text node " << this << " to node " << copyNode
               << " parent = " << std::hex << (parent ? (ulong)parent : 0)
               << " pos = " << pos);
@@ -4079,6 +4093,9 @@
     throw;
   }
 
+  if (haveFrozenReference() || copymode.theFreezeReference)
+    GET_STORE().copyReference(this,copyNode);
+
   NODE_TRACE1("Copied pi node " << this << " to node " << copyNode
               << " parent = " << std::hex << (parent ? (ulong)parent : 0)
               << " pos = " << pos);
@@ -4202,6 +4219,9 @@
     throw;
   }
 
+  if (haveFrozenReference() || copymode.theFreezeReference)
+    GET_STORE().copyReference(this,copyNode);
+
   NODE_TRACE1("Copied coment node " << this << " to node " << copyNode
               << " parent = " << std::hex << (parent ? (ulong)parent : 0)
               << " pos = " << pos);

=== modified file 'src/store/naive/node_items.h'
--- src/store/naive/node_items.h	2011-09-23 14:26:00 +0000
+++ src/store/naive/node_items.h	2011-09-30 12:22:26 +0000
@@ -325,14 +325,16 @@
 
     // For element nodes only. The flag is set for a node N if there is another
     // node M in its subtree such that N and M have the same name.
-    IsRecursive       =   0x4000
+    IsRecursive       =   0x4000,
 
 #ifndef EMBEDED_TYPE
-    ,
     // For element and attribute nodes only. The flag is set if the node has
     // a type other than untyped (for elements) or untypedAtomic (for attributes)
-    HaveType  =   0x8000
+    HaveType  =   0x8000,
 #endif
+
+    HaveReference    = 0x10000,
+    HaveFrozenReference = 0x20000
   };
 
 protected:
@@ -495,6 +497,15 @@
 
   void setFlags(uint32_t flags) { theFlags = flags; }
 
+  bool haveReference() const         { return (theFlags & HaveReference) != 0; }
+  void setHaveReference()            { theFlags |= HaveReference; }
+  void resetHaveReference()          { theFlags &= ~HaveReference; }
+
+  bool haveFrozenReference() const   { return (theFlags & HaveFrozenReference) != 0; }
+  void setHaveFrozenReference()      { theFlags |= HaveFrozenReference; }
+  void resetHaveFrozenReference()    { theFlags &= ~HaveFrozenReference; }
+
+
 #ifndef ZORBA_NO_FULL_TEXT
   FTTokenIterator_t getTokens( TokenizerProvider const&, Tokenizer::Numbers&,
                                locale::iso639_1::type, bool = false ) const;

=== modified file 'src/store/naive/simple_store.cpp'
--- src/store/naive/simple_store.cpp	2011-09-26 22:49:54 +0000
+++ src/store/naive/simple_store.cpp	2011-09-30 12:22:26 +0000
@@ -57,6 +57,8 @@
 #include "store/naive/pul_primitive_factory.h"
 
 #include "util/cxx_util.h"
+#include "util/uuid/uuid.h"
+#include "zorbautils/string_util.h"
 
 #ifndef ZORBA_NO_FULL_TEXT
 #include "runtime/full_text/default_tokenizer.h"
@@ -334,6 +336,24 @@
       theNodeFactory = NULL;
     }
 
+    if(theNodeToReferencesMap.size()>0)
+    {
+      std::map<const store::Item *,zstring>::iterator iter= theNodeToReferencesMap.begin();
+      std::map<const store::Item *,zstring>::iterator end= theNodeToReferencesMap.end();
+      for (; iter != end; ++iter)
+        std::cerr << "Reference: " << iter->second << "is still in the nodes to references map" << std::endl;
+      ZORBA_FATAL(0, theNodeToReferencesMap.size()+" node references still in the nodes to references map");
+    }
+
+    if(theReferencesToNodeMap.size()>0)
+    {
+      std::map<const zstring,const store::Item *>::iterator iter= theReferencesToNodeMap.begin();
+      std::map<const zstring,const store::Item *>::iterator end= theReferencesToNodeMap.end();
+      for (; iter != end; ++iter)
+        std::cerr << "Reference: " << iter->first << "is still in the references to nodes map" << std::endl;
+      ZORBA_FATAL(0, theReferencesToNodeMap.size()+" node references still in the references to nodes map");
+    }
+
     // do cleanup of the libxml2 library
     // however, after that, a user will have to call
     // LIBXML_TEST_VERSION if he wants to use libxml2
@@ -1307,362 +1327,187 @@
 }
 
 
-/*******************************************************************************
-  Computes the URI for the given node.
-********************************************************************************/
-bool SimpleStore::getReference(store::Item_t& result, const store::Item* node)
-{
-  std::ostringstream stream;
-
-  const OrdPathNode* n = static_cast<const OrdPathNode*>(node);
-  stream << "zorba:"
-         << n->getCollectionId() << "."
-         << n->getTreeId() << "."
-         << n->getTree()->getPosition() + 1;
-
-#ifdef TEXT_ORDPATH
-
-  if (n->getNodeKind() == store::StoreConsts::attributeNode)
-  {
-    stream <<  ".a."
-  }
-  else
-  {
-    stream <<  ".c."
-  }
-  stream << n->getOrdPath().serialize();
-
-#else
-
-  if (n->getNodeKind() == store::StoreConsts::attributeNode)
-  {
-    stream << ".a."
-           << static_cast<const OrdPathNode*>(n)->getOrdPath().serialize();
-  }
-  else if (n->getNodeKind() == store::StoreConsts::textNode)
-  {
-    stream << ".a."
-           << static_cast<const OrdPathNode*>(n->getParent())->getOrdPath().serialize();
-  }
-  else
-  {
-    stream << ".c."
-           << static_cast<const OrdPathNode*>(n)->getOrdPath().serialize();
-  }
-#endif
-
-  zstring str(stream.str());
-
-  return theItemFactory->createAnyURI(result, str);
-}
-
-
-/*******************************************************************************
-  Returns Item which is identified by a reference
-
-  @param uri Has to be an xs:URI item
-  @returns referenced item if it exists, otherwise NULL
-********************************************************************************/
-bool SimpleStore::getNodeByReference(store::Item_t& result, const store::Item* uri)
-{
-  const zstring& str = uri->getString();
-
-  ulong prefixlen = (ulong)strlen("zorba:");
-
-  if (strncmp(str.c_str(), "zorba:", prefixlen))
-    throw ZORBA_EXCEPTION(
-      zerr::ZAPI0028_INVALID_NODE_URI, ERROR_PARAMS( str )
-    );
-
-  const char* start;
-  long tmp = 0;
-
-  //
-  // Decode collection id
-  //
-  start = str.c_str() + prefixlen;
-  char* next = const_cast<char*>(start);
-
-  tmp = strtol(start, &next, 10);
-
-  if (tmp < 0 || tmp == LONG_MAX)
-    throw ZORBA_EXCEPTION(
-      zerr::ZAPI0028_INVALID_NODE_URI, ERROR_PARAMS( str )
-    );
-
-  start = next;
-
-  if (*start != '.')
-    throw ZORBA_EXCEPTION(
-      zerr::ZAPI0028_INVALID_NODE_URI, ERROR_PARAMS( str )
-    );
-
-  ++start;
-
-  ulong collectionId = (ulong)tmp;
-
-  //
-  // Decode tree id
-  //
-  tmp = strtol(start, &next, 10);
-
-  if (tmp <= 0 || tmp == LONG_MAX)
-    throw ZORBA_EXCEPTION(
-      zerr::ZAPI0028_INVALID_NODE_URI, ERROR_PARAMS( str )
-    );
-
-  start = next;
-
-  if (*start != '.')
-    throw ZORBA_EXCEPTION(
-      zerr::ZAPI0028_INVALID_NODE_URI, ERROR_PARAMS( str )
-    );
-
-  ++start;
-
-  ulong treeId = (ulong)tmp;
-
-  //
-  // Decode tree position within collection
-  //
-  tmp = strtol(start, &next, 10);
-
-  if (tmp <= 0 || tmp == LONG_MAX)
-    throw ZORBA_EXCEPTION(
-      zerr::ZAPI0028_INVALID_NODE_URI, ERROR_PARAMS( str )
-    );
-
-  start = next;
-
-  if (*start != '.')
-    throw ZORBA_EXCEPTION(
-      zerr::ZAPI0028_INVALID_NODE_URI, ERROR_PARAMS( str )
-    );
-
-  ++start;
-
-  ulong treePos = (ulong)tmp;
-
-  //
-  // Check if the uri specifies attribute node or not
-  //
-  bool attributeNode;
-
-  if (*start == 'a')
-    attributeNode = true;
-  else if (*start == 'c')
-    attributeNode = false;
-  else
-    throw ZORBA_EXCEPTION(
-      zerr::ZAPI0028_INVALID_NODE_URI, ERROR_PARAMS( str )
-    );
-
-  ++start;
-  if (*start != '.')
-    throw ZORBA_EXCEPTION(
-      zerr::ZAPI0028_INVALID_NODE_URI, ERROR_PARAMS( str )
-    );
-
-  ++start;
-
-  //
-  // Search for the tree
-  //
-  XmlNode* rootNode = NULL;
-
-  if (collectionId == 0)
-  {
-    DocumentSet::iterator it = theDocuments.begin();
-    DocumentSet::iterator end = theDocuments.end();
-
-    for (; it != end; ++it)
-    {
-      rootNode = (*it).second.getp();
-
-      if (rootNode->getTreeId() == treeId)
-        break;
-    }
-
-    if (it == end)
-      rootNode = NULL;
-  }
-  else
-  {
-    // Look for the collection
-    SimpleCollection* collection;
-
-    CollectionIterator_t lIter = theCollections->collections();
-    lIter->open();
-
-    store::Collection_t lCollection;
-    bool lFound = false;
-
-    // search the collection
-    while (lIter->next(lCollection))
-    {
-      collection = static_cast<SimpleCollection*>(lCollection.getp());
-
-      if (collection->getId() == collectionId) {
-        lFound = true;
-        break;
-      }
-    }
-
-    if (!lFound)
-    {
-      lIter = theCollections->collections(true);
-      lIter->open();
-
-      // search the collection
-      while (lIter->next(lCollection))
-      {
-        collection = static_cast<SimpleCollection*>(lCollection.getp());
-
-        if (collection->getId() == collectionId) {
-          lFound = true;
-          break;
-        }
-      }
-    }
-
-    // If collection found, look for the tree
-    if (lFound)
-    {
-      store::Item_t rootItem = collection->nodeAt(treePos);
-
-      rootNode = BASE_NODE(rootItem);
-
-      if (rootNode == NULL || rootNode->getTreeId() != treeId)
-      {
-        store::Iterator_t treeIter = collection->getIterator();
-
-        treeIter->open();
-
-        while (treeIter->next(rootItem))
-        {
-          rootNode = BASE_NODE(rootItem);
-          if (rootNode->getTreeId() == treeId)
-            break;
-        }
-
-        treeIter->close();
-
-        rootNode = BASE_NODE(rootItem);
-      }
-    }
-  }
-
-  if (rootNode == NULL)
-  {
-    result = NULL;
-    return false;
-  }
-
-  //
-  // Search for node in the tree
-  //
-
-  OrdPath op((unsigned char*)start, (ulong)strlen(start));
-
-#ifdef TEXT_ORDPATH
-  if (static_cast<OrdPathNode*>(rootNode)->getOrdPath() == op)
-  {
-    result = rootNode;
-    return true;
-  }
-#else
-  if (rootNode->getNodeKind() == store::StoreConsts::textNode)
-  {
-    result = NULL;
-    return false;
-  }
-  else if (static_cast<OrdPathNode*>(rootNode)->getOrdPath() == op)
-  {
-    result = rootNode;
-    return true;
-  }
-#endif
-
-  XmlNode* parent = rootNode;
-
-  while (1)
-  {
-    ulong i;
-
-    if (parent->getNodeKind() != store::StoreConsts::documentNode &&
-        parent->getNodeKind() != store::StoreConsts::elementNode)
-    {
-      result = NULL;
-      return false;
-    }
-
-    if (attributeNode && parent->getNodeKind() == store::StoreConsts::elementNode)
-    {
-      ElementNode* elemParent = reinterpret_cast<ElementNode*>(parent);
-
-      ulong numAttrs = elemParent->numAttrs();
-      for (i = 0; i < numAttrs; i++)
-      {
-        AttributeNode* child = elemParent->getAttr(i);
-
-        OrdPath::RelativePosition pos =  child->getOrdPath().getRelativePosition(op);
-
-        if (pos == OrdPath::SELF)
-        {
-          result = child;
-          return true;
-        }
-        else if (pos != OrdPath::FOLLOWING)
-        {
-          result = NULL;
-          return false;
-        }
-      }
-    }
-
-    InternalNode* parent2 = reinterpret_cast<InternalNode*>(parent);
-
-    ulong numChildren = parent2->numChildren();
-    for (i = 0; i < numChildren; i++)
-    {
-#ifdef TEXT_ORDPATH
-      OrdPathNode* child = static_cast<OrdPathNode*>(parent2->getChild(i));
-#else
-      XmlNode* c = parent2->getChild(i);
-
-      if (c->getNodeKind() == store::StoreConsts::textNode)
-        continue;
-
-      OrdPathNode* child = static_cast<OrdPathNode*>(c);
-#endif
-
-      OrdPath::RelativePosition pos =  child->getOrdPath().getRelativePosition(op);
-
-      if (pos == OrdPath::SELF)
-      {
-        result = child;
-        return true;
-      }
-      else if (pos == OrdPath::DESCENDANT)
-      {
-        parent = child;
-        break;
-      }
-      else if (pos != OrdPath::FOLLOWING)
-      {
-        result = NULL;
-        return false;
-      }
-    }
-
-    if (i == numChildren)
-    {
-      result = NULL;
-      return false;
-    }
-  }
-}
-
+/* ------------------------ Node References Management ---------------------------*/
+
+/**
+ * Computes the reference of the given node.
+ *
+ * @param node XDM node
+ * @return the identifier as an item of type xs:anyURI
+ */
+bool SimpleStore::getReference(store::Item_t& result, store::Item* node)
+{
+  XmlNode* xmlNode=static_cast<XmlNode*>(node);
+  if (xmlNode->haveReference())
+  {
+    std::map<const store::Item *,zstring>::iterator resIt=theNodeToReferencesMap.find(node);
+    ZORBA_FATAL(resIt!=theNodeToReferencesMap.end(),"Node reference cannot be found");
+    zstring id=resIt->second;
+    return theItemFactory->createString(result, id);
+  }
+
+  uuid_t uuid;
+  uuid_create(&uuid);
+  zstring uuidStr=uuidToURI(uuid);
+  xmlNode->setHaveReference();
+  theNodeToReferencesMap[node]=uuidStr;
+  theReferencesToNodeMap[uuidStr]=node;
+  return theItemFactory->createAnyURI(result, uuidStr);
+}
+
+/**
+ * Returns the already computed reference of the given node.
+ * If no reference has already been computed for the given node
+ * error ZAPI0030 is raised.
+ *
+ * @param result reference as an item of type xs:string
+ * @param node XDM node
+ * @return whether the reference has been created successfully
+ */
+bool SimpleStore::getCurrentReference(store::Item_t& result, const store::Item* node)
+{
+  const XmlNode* xmlNode=static_cast<const XmlNode*>(node);
+  if (!xmlNode->haveReference())
+    throw ZORBA_EXCEPTION(zerr::ZAPI0030_NO_CURRENT_REFERENCE);
+
+  std::map<const store::Item *,zstring>::iterator resIt=theNodeToReferencesMap.find(node);
+  ZORBA_FATAL(resIt!=theNodeToReferencesMap.end(),"Node reference cannot be found");
+  zstring id=resIt->second;
+  return theItemFactory->createString(result, id);
+}
+
+/**
+ * Returns the node which is identified by the given reference.
+ *
+ * @param reference an xs:anyURI item
+ * @returns the node identified by the reference, NULL otherwise
+ */
+bool SimpleStore::getNodeByReference(store::Item_t& result, const zstring& reference)
+{
+  if (
+          reference.length()!=41
+          ||
+          !utf8::match_whole(reference, "uuid:[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}")
+         )
+      {
+        throw ZORBA_EXCEPTION(
+          zerr::ZAPI0028_INVALID_NODE_URI,
+          ERROR_PARAMS(reference)
+        );
+      }
+
+  std::map<const zstring, const store::Item *>::iterator resIt;
+  if ((resIt=theReferencesToNodeMap.find(reference))!=theReferencesToNodeMap.end())
+  {
+    result=resIt->second;
+    return true;
+  }
+  result=NULL;
+  return false;
+}
+
+/**
+ * Returns whether a reference has already been generated for the given node.
+ *
+ * @param item XDM node
+ * @return whether a reference has already been generated for the given node.
+ */
+bool SimpleStore::hasReference(const store::Item* node)
+{
+  return static_cast<const XmlNode*>(node)->haveReference();
+}
+
+/**
+ * Copies the reference of a source node to a target node. The source
+ * node must already have a reference. The target nodes acquires the
+ * source reference but it is not registered in the reference-to-node
+ * map. The target node also get its "haveFrozenReference" flag set.
+ * Used in PUL manipulation.
+ *
+ * @param source source XDM node
+ * @param target target XDM node
+ */
+void SimpleStore::copyReference(const XmlNode* source, XmlNode* target)
+{
+  store::Item_t reference;
+  getCurrentReference(reference,source);
+  unregisterNode(target);
+  target->setHaveReference();
+  target->setHaveFrozenReference();
+  theNodeToReferencesMap[target]=reference->getStringValue();
+}
+
+/**
+ * Sets the reference of a node to a given value. The "haveFrozenIdentifer"
+ * flag is also set.
+ * Used in PUL manipulation.
+ *
+ * @param node  XDM node
+ * @param reference the reference to set
+ */
+void SimpleStore::restoreReference(XmlNode* node, const zstring& reference)
+{
+  unregisterNode(node);
+  theNodeToReferencesMap[node]=reference;
+  node->setHaveReference();
+  node->setHaveFrozenReference();
+}
+
+/**
+ * Unfreezes the reference of a given node:
+ * - Registers the node in the references to node map
+ * - Resets the node "haveFrozenReference" flag
+ * The node must already have an reference, otherwise
+ * error ZAPI0030 is raised.
+ * The node reference must not be used for any other
+ * node in the reference-to-node map, otherwise
+ * error ZAPI0029 is raised.
+ * Used in PUL manipulation.
+ *
+ * @param node XDM node
+ */
+void SimpleStore::unfreezeReference(XmlNode* node)
+{
+  store::Item_t reference;
+  getCurrentReference(reference,node);
+  store::Item_t result;
+  if (getNodeByReference(result, reference->getStringValue()))
+  {
+    throw ZORBA_EXCEPTION(
+            zerr::ZAPI0029_REFERENCE_ALREADY_PRESENT,
+            ERROR_PARAMS(reference->getStringValue())
+          );
+  }
+  theReferencesToNodeMap[reference->getStringValue()]=node;
+  node->resetHaveFrozenReference();
+}
+
+/**
+ * Removes a node from the reference-to-nodes and nodes-to-references maps.
+ *
+ * @param node XDM node
+ * @return whether the node was registered or not.
+ */
+bool SimpleStore::unregisterNode(XmlNode* node)
+{
+  if (!node->haveReference())
+    return false;
+  std::map<const store::Item *,zstring>::iterator resIt;
+  if ((resIt=theNodeToReferencesMap.find(node))!=theNodeToReferencesMap.end())
+  {
+    zstring value=resIt->second;
+    theNodeToReferencesMap.erase(resIt);
+    node->resetHaveReference();
+    if (!node->haveFrozenReference())
+      theReferencesToNodeMap.erase(value);
+    else
+      node->resetHaveFrozenReference();
+    //if a node has a frozen reference it is not registered in the references to node map
+    return true;
+  }
+  else
+    return false;
+}
 
 /*******************************************************************************
 

=== modified file 'src/store/naive/simple_store.h'
--- src/store/naive/simple_store.h	2011-09-26 22:49:54 +0000
+++ src/store/naive/simple_store.h	2011-09-30 12:22:26 +0000
@@ -66,30 +66,34 @@
 
 
 /*******************************************************************************
-  theSchemaTypeNames   : Maps each enum value from SchemaTypeNames (see
-                         store_defs.h) to its associated QName item.
-
-  theCollectionCounter : Incremented every time a new collection is created. The
-                         current value of the counter is then assigned as the
-                         id of the new collection.
-
-  theNamespacePool     :
-  theQNamePool         :
-
-  theItemFactory       : Factory to create items.
-  theIteratorFactory   : Factory to create iterators.
-  theNodeFactory       : Factory to create node items.
-
-  theDocuments         : A hashmap that for each xml tree that does not belong
-                         to any collection, maps the URI of the tree to the root
-                         node of the tree.
-  theCollections       : Container which contains the collections of the store.
-                         It includes a map that maps the qname of each collection
-                         to the collection's container object.
-  theIndices           : A hashmap that for each index, maps the qname of the
-                         index to the index container object.
-  theICs               : A hashmap the for each integrity constraint, maps the
-                         qname of the ic to the ic's container object.
+  theSchemaTypeNames     : Maps each enum value from SchemaTypeNames (see
+                           store_defs.h) to its associated QName item.
+
+  theCollectionCounter   : Incremented every time a new collection is created. The
+                           current value of the counter is then assigned as the
+                           id of the new collection.
+
+  theNamespacePool       :
+  theQNamePool           :
+
+  theItemFactory         : Factory to create items.
+  theIteratorFactory     : Factory to create iterators.
+  theNodeFactory         : Factory to create node items.
+
+  theDocuments           : A hashmap that for each xml tree that does not belong
+                           to any collection, maps the URI of the tree to the root
+                           node of the tree.
+  theCollections         : Container which contains the collections of the store.
+                           It includes a map that maps the qname of each collection
+                           to the collection's container object.
+  theIndices             : A hashmap that for each index, maps the qname of the
+                           index to the index container object.
+  theICs                 : A hashmap the for each integrity constraint, maps the
+                           qname of the ic to the ic's container object.
+  theReferencesToNodeMap : A hashmap that maps node references to the referenced
+                           nodes
+  theNodeToReferencesMap : A hashmap that maps nodes into their references
+
 ********************************************************************************/
 class SimpleStore : public store::Store
 {
@@ -142,6 +146,13 @@
 
   long                          theTraceLevel;
 
+  /* ------------------------ Node Reference Management ---------------------------*/
+
+  std::map<const zstring, const store::Item *> theReferencesToNodeMap;
+  std::map<const store::Item *, zstring> theNodeToReferencesMap;
+
+  /* ------------------------ Node Reference Management ---------------------------*/
+
 #ifndef ZORBA_NO_FULL_TEXT
   internal::StemmerProvider const * theStemmerProvider;
   TokenizerProvider const     * theTokenizerProvider;
@@ -328,9 +339,25 @@
         bool& found,
         bool& unique);
 
-  bool getReference(store::Item_t& result, const store::Item* node);
-
-  bool getNodeByReference(store::Item_t& result, const store::Item* uri);
+  /* ------------------------ Node Reference Management ---------------------------*/
+
+  bool getReference(store::Item_t& result, store::Item* node);
+
+  bool getCurrentReference(store::Item_t& result, const store::Item* node);
+
+  bool hasReference(const store::Item* node);
+
+  bool getNodeByReference(store::Item_t& result, const zstring& reference);
+
+  bool unregisterNode(XmlNode* node);
+
+  void copyReference(const XmlNode* source, XmlNode* target);
+
+  void restoreReference(XmlNode* node, const zstring& reference);
+
+  void unfreezeReference(XmlNode* node);
+
+  /* ------------------------ Node Reference Management ---------------------------*/
 
   store::TempSeq_t createTempSeq(bool lazy);
 

=== modified file 'src/util/uuid/uuid.cpp'
--- src/util/uuid/uuid.cpp	2011-09-02 21:41:55 +0000
+++ src/util/uuid/uuid.cpp	2011-09-30 12:22:26 +0000
@@ -235,6 +235,18 @@
   return lResult;
 }
 
+/* uuidToString -- transform a UUID to a uri*/
+zstring uuidToURI(const uuid_t& u)
+{
+  char lBuffer[200];
+  sprintf(lBuffer, "uuid:%8.8x-%4.4x-%4.4x-%2.2x%2.2x-9300a64ac3cd", u.time_low, u.time_mid,
+          u.time_hi_and_version, u.clock_seq_hi_and_reserved,
+          u.clock_seq_low);
+
+  zstring lResult = lBuffer;
+  return lResult;
+}
+
 /*
 //This appendix lists the name space IDs for some potentially
 //interesting namespaces, as initialized C structures and in the

=== modified file 'src/util/uuid/uuid.h'
--- src/util/uuid/uuid.h	2011-09-02 21:41:55 +0000
+++ src/util/uuid/uuid.h	2011-09-30 12:22:26 +0000
@@ -36,6 +36,9 @@
 /* uuidToString -- transform a UUID to a string */
 zstring uuidToString(const uuid_t& uuid);
 
+/* uuidToURI -- transform a UUID to a URI-formatted string */
+zstring uuidToURI(const uuid_t& uuid);
+
 } // namespace zorba
 #endif /* ZORBA_UTIL_UUID_H */
 /* vim:set et sw=2 ts=2: */

=== added file 'state'
Binary files state	1970-01-01 00:00:00 +0000 and state	2011-09-30 12:22:26 +0000 differ
=== added file 'test/rbkt/ExpQueryResults/zorba/paths/ref1.xml.res'
--- test/rbkt/ExpQueryResults/zorba/paths/ref1.xml.res	1970-01-01 00:00:00 +0000
+++ test/rbkt/ExpQueryResults/zorba/paths/ref1.xml.res	2011-09-30 12:22:26 +0000
@@ -0,0 +1,387 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<people>
+<person id="person0">
+<name>Huei Demke</name>
+<emailaddress>mailto:Demke@xxxxx</emailaddress>
+<address>
+<street>95 Grinter St</street>
+<city>Macon</city>
+<country>United States</country>
+<zipcode>32</zipcode>
+</address>
+<creditcard>8182 1228 4493 3873</creditcard>
+<profile income="55386.86">
+<education>High School</education>
+<gender>male</gender>
+<business>No</business>
+<age>37</age>
+</profile>
+</person>
+<person id="person1">
+<name>Daishiro Juric</name>
+<emailaddress>mailto:Juric@xxxxxxxxxxxxxx</emailaddress>
+<phone>+0 (692) 53964736</phone>
+<address>
+<street>5 Pinet St</street>
+<city>Athens</city>
+<country>United States</country>
+<province>Vermont</province>
+<zipcode>13</zipcode>
+</address>
+<homepage>http://www.rwth-aachen.de/~Juric</homepage>
+<creditcard>7833 6950 5399 8913</creditcard>
+<watches>
+<watch open_auction="open_auction2"/>
+<watch open_auction="open_auction4"/>
+<watch open_auction="open_auction5"/>
+<watch open_auction="open_auction6"/>
+<watch open_auction="open_auction9"/>
+<watch open_auction="open_auction0"/>
+</watches>
+</person>
+<person id="person2">
+<name>Kawon Unni</name>
+<emailaddress>mailto:Unni@xxxxxxxx</emailaddress>
+<phone>+0 (866) 58076283</phone>
+<watches>
+<watch open_auction="open_auction0"/>
+<watch open_auction="open_auction7"/>
+<watch open_auction="open_auction2"/>
+<watch open_auction="open_auction4"/>
+</watches>
+</person>
+<person id="person3">
+<name>Ewing Andrade</name>
+<emailaddress>mailto:Andrade@xxxxxxx</emailaddress>
+<profile income="41889.41">
+<interest category="category0"/>
+<interest category="category0"/>
+<education>Graduate School</education>
+<gender>female</gender>
+<business>Yes</business>
+<age>18</age>
+</profile>
+<watches>
+<watch open_auction="open_auction6"/>
+<watch open_auction="open_auction10"/>
+<watch open_auction="open_auction2"/>
+<watch open_auction="open_auction6"/>
+<watch open_auction="open_auction5"/>
+</watches>
+</person>
+<person id="person4">
+<name>Jamaludin Kleiser</name>
+<emailaddress>mailto:Kleiser@xxxxxx</emailaddress>
+<phone>+0 (400) 25499686</phone>
+<homepage>http://www.edu.hk/~Kleiser</homepage>
+<profile income="9876.00">
+<interest category="category0"/>
+<interest category="category0"/>
+<interest category="category0"/>
+<interest category="category0"/>
+<interest category="category0"/>
+<interest category="category0"/>
+<education>High School</education>
+<business>No</business>
+<age>34</age>
+</profile>
+</person>
+<person id="person5">
+<name>Eliana Ruemmler</name>
+<emailaddress>mailto:Ruemmler@xxxxxxxx</emailaddress>
+<phone>+0 (361) 10427629</phone>
+<homepage>http://www.forth.gr/~Ruemmler</homepage>
+<watches>
+</watches>
+</person>
+<person id="person6">
+<name>Bassem Nagasaki</name>
+<emailaddress>mailto:Nagasaki@xxxxxxxxxxxxx</emailaddress>
+<phone>+0 (182) 62129440</phone>
+<homepage>http://www.panasonic.com/~Nagasaki</homepage>
+<creditcard>6012 3708 3300 9328</creditcard>
+</person>
+<person id="person7">
+<name>Mehrdad McCay</name>
+<emailaddress>mailto:McCay@xxxxxxx</emailaddress>
+<address>
+<street>40 Kipper St</street>
+<city>Lyon</city>
+<country>United States</country>
+<zipcode>33</zipcode>
+</address>
+<homepage>http://www.acm.org/~McCay</homepage>
+<watches>
+<watch open_auction="open_auction10"/>
+<watch open_auction="open_auction6"/>
+<watch open_auction="open_auction7"/>
+<watch open_auction="open_auction6"/>
+<watch open_auction="open_auction10"/>
+</watches>
+</person>
+<person id="person8">
+<name>Bassem Manderick</name>
+<emailaddress>mailto:Manderick@xxxxxxxx</emailaddress>
+<phone>+0 (18) 14026838</phone>
+<address>
+<street>45 Ratnaker St</street>
+<city>Zihuatenejo</city>
+<country>United States</country>
+<province>Illinois</province>
+<zipcode>4</zipcode>
+</address>
+<creditcard>8940 7694 6616 2527</creditcard>
+<watches>
+<watch open_auction="open_auction2"/>
+<watch open_auction="open_auction7"/>
+<watch open_auction="open_auction8"/>
+<watch open_auction="open_auction8"/>
+<watch open_auction="open_auction2"/>
+</watches>
+</person>
+<person id="person9">
+<name>Jarkko Nozawa</name>
+<emailaddress>mailto:Nozawa@xxxxxxxxxxxx</emailaddress>
+<phone>+0 (835) 5433372</phone>
+<homepage>http://www.concordia.ca/~Nozawa</homepage>
+<creditcard>2447 4672 2532 3021</creditcard>
+<profile income="57878.50">
+<interest category="category0"/>
+<interest category="category0"/>
+<interest category="category0"/>
+<interest category="category0"/>
+<interest category="category0"/>
+<interest category="category0"/>
+<interest category="category0"/>
+<interest category="category0"/>
+<interest category="category0"/>
+<interest category="category0"/>
+<interest category="category0"/>
+<interest category="category0"/>
+<business>No</business>
+<age>33</age>
+</profile>
+</person>
+<person id="person10">
+<name>Masanao Marsiglia</name>
+<emailaddress>mailto:Marsiglia@xxxxxx</emailaddress>
+<address>
+<street>22 Caticha St</street>
+<city>Phoenix</city>
+<country>United States</country>
+<province>Alaska</province>
+<zipcode>31</zipcode>
+</address>
+<creditcard>8543 4364 4394 3142</creditcard>
+<profile income="44300.88">
+<interest category="category0"/>
+<interest category="category0"/>
+<education>Other</education>
+<gender>female</gender>
+<business>Yes</business>
+<age>48</age>
+</profile>
+<watches>
+<watch open_auction="open_auction0"/>
+<watch open_auction="open_auction9"/>
+</watches>
+</person>
+<person id="person11">
+<name>Saul Schaap</name>
+<emailaddress>mailto:Schaap@xxxxxxxx</emailaddress>
+<phone>+0 (255) 85051213</phone>
+<address>
+<street>11 Gumm St</street>
+<city>Cody</city>
+<country>United States</country>
+<province>Montana</province>
+<zipcode>28</zipcode>
+</address>
+<profile income="33731.14">
+<interest category="category0"/>
+<education>College</education>
+<business>Yes</business>
+<age>23</age>
+</profile>
+<watches>
+<watch open_auction="open_auction3"/>
+<watch open_auction="open_auction7"/>
+<watch open_auction="open_auction0"/>
+<watch open_auction="open_auction1"/>
+<watch open_auction="open_auction8"/>
+<watch open_auction="open_auction0"/>
+<watch open_auction="open_auction9"/>
+</watches>
+</person>
+<person id="person12">
+<name>Kishor Monkewich</name>
+<emailaddress>mailto:Monkewich@xxxxxxxx</emailaddress>
+<phone>+0 (254) 26597439</phone>
+<address>
+<street>6 Thiria St</street>
+<city>Denver</city>
+<country>United States</country>
+<province>Mississipi</province>
+<zipcode>20</zipcode>
+</address>
+<homepage>http://www.llnl.gov/~Monkewich</homepage>
+<creditcard>7036 1307 7693 1072</creditcard>
+<profile income="43552.04">
+<interest category="category0"/>
+<gender>female</gender>
+<business>Yes</business>
+</profile>
+</person>
+<person id="person13">
+<name>Martti Halgason</name>
+<emailaddress>mailto:Halgason@xxxxxxxxxxxxx</emailaddress>
+<phone>+0 (778) 41220156</phone>
+<profile income="41407.27">
+<interest category="category0"/>
+<interest category="category0"/>
+<interest category="category0"/>
+<interest category="category0"/>
+<gender>female</gender>
+<business>Yes</business>
+<age>20</age>
+</profile>
+<watches>
+<watch open_auction="open_auction7"/>
+</watches>
+</person>
+<person id="person14">
+<name>Laurian Grass</name>
+<emailaddress>mailto:Grass@xxxxxxxx</emailaddress>
+<phone>+0 (486) 33977729</phone>
+<address>
+<street>44 Wergeland St</street>
+<city>Mobile</city>
+<country>United States</country>
+<zipcode>29</zipcode>
+</address>
+<creditcard>1021 8216 1485 5908</creditcard>
+<profile income="53231.89">
+<interest category="category0"/>
+<gender>male</gender>
+<business>Yes</business>
+<age>28</age>
+</profile>
+</person>
+<person id="person15">
+<name>Shooichi Oerlemans</name>
+<emailaddress>mailto:Oerlemans@xxxxx</emailaddress>
+<address>
+<street>7 Rein St</street>
+<city>Alexandria</city>
+<country>Cook Islands</country>
+<zipcode>13</zipcode>
+</address>
+<creditcard>1796 7353 7134 5098</creditcard>
+<watches>
+<watch open_auction="open_auction9"/>
+<watch open_auction="open_auction1"/>
+<watch open_auction="open_auction2"/>
+<watch open_auction="open_auction6"/>
+<watch open_auction="open_auction8"/>
+<watch open_auction="open_auction8"/>
+</watches>
+</person>
+<person id="person16">
+<name>Uzi Atrawala</name>
+<emailaddress>mailto:Atrawala@xxxxxxxx</emailaddress>
+<phone>+49 (161) 95788409</phone>
+<address>
+<street>77 Coyle St</street>
+<city>Great</city>
+<country>United States</country>
+<province>Oklahoma</province>
+<zipcode>14</zipcode>
+</address>
+<profile income="26302.86">
+<interest category="category0"/>
+<interest category="category0"/>
+<interest category="category0"/>
+<gender>male</gender>
+<business>Yes</business>
+</profile>
+</person>
+<person id="person17">
+<name>Aloys Singleton</name>
+<emailaddress>mailto:Singleton@xxxxxxxxxxxx</emailaddress>
+<phone>+0 (735) 97531072</phone>
+</person>
+<person id="person18">
+<name>Maha DuBourdieux</name>
+<emailaddress>mailto:DuBourdieux@xxxxxxxxxxx</emailaddress>
+<phone>+0 (43) 57060411</phone>
+<homepage>http://www.clustra.com/~DuBourdieux</homepage>
+<profile income="32624.17">
+<interest category="category0"/>
+<business>Yes</business>
+</profile>
+<watches>
+</watches>
+</person>
+<person id="person19">
+<name>Masaski Carrere</name>
+<emailaddress>mailto:Carrere@xxxxxx</emailaddress>
+<phone>+0 (155) 68703448</phone>
+<address>
+<street>51 Bedard St</street>
+<city>Copenhagen</city>
+<country>United States</country>
+<zipcode>29</zipcode>
+</address>
+<homepage>http://www.lri.fr/~Carrere</homepage>
+<watches>
+<watch open_auction="open_auction8"/>
+</watches>
+</person>
+<person id="person20">
+<name>Nestoras Gausemeier</name>
+<emailaddress>mailto:Gausemeier@xxxxxxxxxxx</emailaddress>
+<phone>+0 (864) 72833728</phone>
+<address>
+<street>80 Melter St</street>
+<city>Ciudad</city>
+<country>Greenland</country>
+<zipcode>23</zipcode>
+</address>
+<creditcard>4586 2519 6227 6395</creditcard>
+</person>
+<person id="person21">
+<name>Yechezkel Calmet</name>
+<emailaddress>mailto:Calmet@xxxxxxxx</emailaddress>
+<watches>
+<watch open_auction="open_auction3"/>
+<watch open_auction="open_auction9"/>
+<watch open_auction="open_auction0"/>
+<watch open_auction="open_auction6"/>
+<watch open_auction="open_auction6"/>
+<watch open_auction="open_auction2"/>
+</watches>
+</person>
+<person id="person22">
+<name>Slavian Usery</name>
+<emailaddress>mailto:Usery@xxxxxxxxxxx</emailaddress>
+<creditcard>7501 3122 6462 7866</creditcard>
+<watches>
+</watches>
+</person>
+<person id="person23">
+<name>Piere Schiex</name>
+<emailaddress>mailto:Schiex@xxxxxx</emailaddress>
+<homepage>http://www.sfu.ca/~Schiex</homepage>
+<creditcard>1536 3643 2092 5012</creditcard>
+<watches>
+<watch open_auction="open_auction6"/>
+<watch open_auction="open_auction4"/>
+</watches>
+</person>
+<person id="person24">
+<name>Shaoyun Morreau</name>
+<emailaddress>mailto:Morreau@xxxxxxxx</emailaddress>
+<phone>+83 (890) 82752371</phone>
+<creditcard>6926 3703 6920 9765</creditcard>
+</person>
+</people>
\ No newline at end of file

=== added file 'test/rbkt/ExpQueryResults/zorba/reference/reference_1.xml.res'
--- test/rbkt/ExpQueryResults/zorba/reference/reference_1.xml.res	1970-01-01 00:00:00 +0000
+++ test/rbkt/ExpQueryResults/zorba/reference/reference_1.xml.res	2011-09-30 12:22:26 +0000
@@ -0,0 +1,2 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<result><before hasid="false">41</before><after hasid="true">41</after><stable>true</stable></result>
\ No newline at end of file

=== added file 'test/rbkt/ExpQueryResults/zorba/reference/reference_2.xml.res'
--- test/rbkt/ExpQueryResults/zorba/reference/reference_2.xml.res	1970-01-01 00:00:00 +0000
+++ test/rbkt/ExpQueryResults/zorba/reference/reference_2.xml.res	2011-09-30 12:22:26 +0000
@@ -0,0 +1,2 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<elem>elem</elem><attr attr="attr"/><?pi pi?><!--comment-->document
\ No newline at end of file

=== added file 'test/rbkt/ExpQueryResults/zorba/reference/reference_3.xml.res'
--- test/rbkt/ExpQueryResults/zorba/reference/reference_3.xml.res	1970-01-01 00:00:00 +0000
+++ test/rbkt/ExpQueryResults/zorba/reference/reference_3.xml.res	2011-09-30 12:22:26 +0000
@@ -0,0 +1,2 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<result><out-of-module-global><root>reference.xqlib</root></out-of-module-global><put-doc><root>doc</root></put-doc><in-scope><root>global</root></in-scope><temporary-variable-out-of-scope/><temporary-variable-in-scope><root>temp</root></temporary-variable-in-scope><temporary-variable-in-scope><root>temp</root></temporary-variable-in-scope><temporary-variable-in-scope><root>temp</root><root>temp</root><root>temp</root></temporary-variable-in-scope><temporary-variable-in-scope><root>1</root><root>2</root><root>3</root></temporary-variable-in-scope><temporary-variable-in-scope><root>temp</root></temporary-variable-in-scope><temporary-variable-in-scope><root>temp</root></temporary-variable-in-scope></result>
\ No newline at end of file

=== added file 'test/rbkt/ExpQueryResults/zorba/reference/reference_4.xml.res'
--- test/rbkt/ExpQueryResults/zorba/reference/reference_4.xml.res	1970-01-01 00:00:00 +0000
+++ test/rbkt/ExpQueryResults/zorba/reference/reference_4.xml.res	2011-09-30 12:22:26 +0000
@@ -0,0 +1,1 @@
+<?xml version="1.0" encoding="UTF-8"?>

=== modified file 'test/rbkt/ExpQueryResults/zorba/reference/reference_coll_1.xml.res'
--- test/rbkt/ExpQueryResults/zorba/reference/reference_coll_1.xml.res	2011-06-24 19:58:33 +0000
+++ test/rbkt/ExpQueryResults/zorba/reference/reference_coll_1.xml.res	2011-09-30 12:22:26 +0000
@@ -1,1 +1,2 @@
-<b><z/></b>
+<?xml version="1.0" encoding="UTF-8"?>
+<b><z/></b>
\ No newline at end of file

=== modified file 'test/rbkt/ExpQueryResults/zorba/reference/reference_coll_2.xml.res'
--- test/rbkt/ExpQueryResults/zorba/reference/reference_coll_2.xml.res	2011-06-24 19:58:33 +0000
+++ test/rbkt/ExpQueryResults/zorba/reference/reference_coll_2.xml.res	2011-09-30 12:22:26 +0000
@@ -1,1 +1,2 @@
-<z/>
+<?xml version="1.0" encoding="UTF-8"?>
+<z/>
\ No newline at end of file

=== modified file 'test/rbkt/ExpQueryResults/zorba/reference/reference_coll_3.xml.res'
--- test/rbkt/ExpQueryResults/zorba/reference/reference_coll_3.xml.res	2011-06-24 19:58:33 +0000
+++ test/rbkt/ExpQueryResults/zorba/reference/reference_coll_3.xml.res	2011-09-30 12:22:26 +0000
@@ -1,1 +1,2 @@
-e
+<?xml version="1.0" encoding="UTF-8"?>
+e
\ No newline at end of file

=== modified file 'test/rbkt/Queries/zorba/paths/ref1.spec'
--- test/rbkt/Queries/zorba/paths/ref1.spec	2011-08-04 02:39:56 +0000
+++ test/rbkt/Queries/zorba/paths/ref1.spec	2011-09-30 12:22:26 +0000
@@ -1,4 +1,3 @@
 Args: 
 -x 
 input-context:=xs:string($RBKT_SRC_DIR/Queries/zorba/xmark/auction.xml)
-Error: http://www.zorba-xquery.com/errors:ZAPI0080

=== removed file 'test/rbkt/Queries/zorba/reference/error.spec'
--- test/rbkt/Queries/zorba/reference/error.spec	2011-06-09 12:58:16 +0000
+++ test/rbkt/Queries/zorba/reference/error.spec	1970-01-01 00:00:00 +0000
@@ -1,4 +0,0 @@
-Args: 
--x 
-input-context:=xs:string($RBKT_SRC_DIR/Queries/zorba/reference/books.xml)
-Error: http://www.w3.org/2005/xqt-errors:XPST0017

=== removed file 'test/rbkt/Queries/zorba/reference/error.xq'
--- test/rbkt/Queries/zorba/reference/error.xq	2010-11-09 22:35:09 +0000
+++ test/rbkt/Queries/zorba/reference/error.xq	1970-01-01 00:00:00 +0000
@@ -1,6 +0,0 @@
-declare namespace ref = "http://www.zorba-xquery.com/modules/node-reference";;
-
-declare variable $input-context external;
-
-let $book := doc($input-context)/bib/book[1]
-return ref:node-reference($book)

=== added file 'test/rbkt/Queries/zorba/reference/reference.xqlib'
--- test/rbkt/Queries/zorba/reference/reference.xqlib	1970-01-01 00:00:00 +0000
+++ test/rbkt/Queries/zorba/reference/reference.xqlib	2011-09-30 12:22:26 +0000
@@ -0,0 +1,3 @@
+module namespace idd= "http://www.zorba-xquery.com/reference";;
+
+declare variable $idd:node:=<root>reference.xqlib</root>;

=== added file 'test/rbkt/Queries/zorba/reference/reference_1.xq'
--- test/rbkt/Queries/zorba/reference/reference_1.xq	1970-01-01 00:00:00 +0000
+++ test/rbkt/Queries/zorba/reference/reference_1.xq	2011-09-30 12:22:26 +0000
@@ -0,0 +1,15 @@
+import module namespace id = "http://www.zorba-xquery.com/modules/node-reference";;
+
+variable $node:=<root><child>text</child></root>;
+
+variable $before-hasid:=id:has-reference($node);
+variable $before-currentid:=id:node-reference($node);
+variable $got:=id:node-reference($node);
+variable $after-hasid:=id:has-reference($node);
+variable $after:=id:node-reference($node);
+
+<result>
+<before hasid="{$before-hasid}">{fn:string-length($before-currentid)}</before>
+<after hasid="{$after-hasid}">{fn:string-length($got)}</after>
+<stable>{$got eq $after}</stable>
+</result>
\ No newline at end of file

=== added file 'test/rbkt/Queries/zorba/reference/reference_2.xq'
--- test/rbkt/Queries/zorba/reference/reference_2.xq	1970-01-01 00:00:00 +0000
+++ test/rbkt/Queries/zorba/reference/reference_2.xq	2011-09-30 12:22:26 +0000
@@ -0,0 +1,14 @@
+import module namespace id = "http://www.zorba-xquery.com/modules/node-reference";;
+
+variable $el:=element {"elem"}{"elem"};
+variable $attr:=attribute{"attr"}{"attr"};
+variable $pi:=processing-instruction {"pi"}{"pi"};
+variable $comm:=comment {"comment"};
+variable $doc:=document {"document"};
+
+
+id:node-by-reference(id:node-reference($el)),
+element {"attr"}{id:node-by-reference(id:node-reference($attr))},
+id:node-by-reference(id:node-reference($pi)),
+id:node-by-reference(id:node-reference($comm)),
+id:node-by-reference(id:node-reference($doc))
\ No newline at end of file

=== added file 'test/rbkt/Queries/zorba/reference/reference_3.xq'
--- test/rbkt/Queries/zorba/reference/reference_3.xq	1970-01-01 00:00:00 +0000
+++ test/rbkt/Queries/zorba/reference/reference_3.xq	2011-09-30 12:22:26 +0000
@@ -0,0 +1,45 @@
+import module namespace idd = "http://www.zorba-xquery.com/reference"; at "reference.xqlib";
+import module namespace id = "http://www.zorba-xquery.com/modules/node-reference";;
+import module namespace doc = "http://www.zorba-xquery.com/modules/store/dynamic/documents";;
+
+variable $node:=<root>global</root>;
+
+fn:put(<root>doc</root>,"doc");
+
+
+<result>
+<out-of-module-global>{id:node-by-reference(id:node-reference($idd:node))}</out-of-module-global>
+<put-doc>{id:node-by-reference(id:node-reference(doc:document("doc")))}</put-doc>
+<in-scope>{id:node-by-reference(id:node-reference($node))}</in-scope>
+
+<temporary-variable-out-of-scope>{id:node-by-reference(id:node-reference(<root>temp</root>))}</temporary-variable-out-of-scope>
+<temporary-variable-in-scope>{let $temp:=<root>temp</root> return id:node-by-reference(id:node-reference($temp))}</temporary-variable-in-scope>
+<temporary-variable-in-scope>{
+let $temp:=<root>temp</root> 
+return
+let $ref:=id:node-reference($temp)
+return
+id:node-by-reference($ref)
+}</temporary-variable-in-scope>
+<temporary-variable-in-scope>{
+for $i in (1 to 3)
+let $temp:=<root>temp</root> 
+let $ref:=id:node-reference($temp)
+return
+id:node-by-reference($ref)
+}</temporary-variable-in-scope>
+
+<temporary-variable-in-scope>{
+for $i in (1 to 3)
+let $temp:=<root>{$i}</root>
+where string($temp) = string($i)   
+let $ref:=id:node-reference($temp)
+return
+id:node-by-reference($ref)
+}</temporary-variable-in-scope>
+
+<temporary-variable-in-scope>{let $temp:=<root>temp</root> return id:node-by-reference(id:node-reference($temp))}</temporary-variable-in-scope>
+
+<temporary-variable-in-scope>{variable $temp:=<root>temp</root>; id:node-by-reference(id:node-reference($temp))}</temporary-variable-in-scope>
+
+</result>
\ No newline at end of file

=== added file 'test/rbkt/Queries/zorba/reference/reference_4.spec'
--- test/rbkt/Queries/zorba/reference/reference_4.spec	1970-01-01 00:00:00 +0000
+++ test/rbkt/Queries/zorba/reference/reference_4.spec	2011-09-30 12:22:26 +0000
@@ -0,0 +1,1 @@
+Error: http://www.zorba-xquery.com/errors:ZAPI0028

=== added file 'test/rbkt/Queries/zorba/reference/reference_4.xq'
--- test/rbkt/Queries/zorba/reference/reference_4.xq	1970-01-01 00:00:00 +0000
+++ test/rbkt/Queries/zorba/reference/reference_4.xq	2011-09-30 12:22:26 +0000
@@ -0,0 +1,3 @@
+import module namespace id = "http://www.zorba-xquery.com/modules/node-reference";;
+
+id:node-by-reference(xs:anyURI("this:is-not-a-reference"))
\ No newline at end of file

=== removed file 'test/rbkt/Queries/zorba/reference/reference_doc_example_01.spec'
--- test/rbkt/Queries/zorba/reference/reference_doc_example_01.spec	2011-08-04 02:39:56 +0000
+++ test/rbkt/Queries/zorba/reference/reference_doc_example_01.spec	1970-01-01 00:00:00 +0000
@@ -1,4 +0,0 @@
-Args: 
--x 
-input-context:=xs:string($RBKT_SRC_DIR/Queries/zorba/reference/books.xml)
-Error: http://www.zorba-xquery.com/errors:ZAPI0080

=== removed file 'test/rbkt/Queries/zorba/reference/reference_doc_example_01.xq'
--- test/rbkt/Queries/zorba/reference/reference_doc_example_01.xq	2011-06-24 19:58:33 +0000
+++ test/rbkt/Queries/zorba/reference/reference_doc_example_01.xq	1970-01-01 00:00:00 +0000
@@ -1,7 +0,0 @@
-import module namespace ref =
-  "http://www.zorba-xquery.com/modules/node-reference";;
-
-declare variable $input-context external;
-
-let $book := doc($input-context)/bib/book[1]
-return ref:node-reference($book)

=== removed file 'test/rbkt/Queries/zorba/reference/reference_doc_example_02.spec'
--- test/rbkt/Queries/zorba/reference/reference_doc_example_02.spec	2011-08-04 02:39:56 +0000
+++ test/rbkt/Queries/zorba/reference/reference_doc_example_02.spec	1970-01-01 00:00:00 +0000
@@ -1,4 +0,0 @@
-Args: 
--x 
-input-context:=xs:string($RBKT_SRC_DIR/Queries/zorba/reference/books.xml)
-Error: http://www.zorba-xquery.com/errors:ZAPI0080

=== removed file 'test/rbkt/Queries/zorba/reference/reference_doc_example_02.xq'
--- test/rbkt/Queries/zorba/reference/reference_doc_example_02.xq	2011-06-24 19:58:33 +0000
+++ test/rbkt/Queries/zorba/reference/reference_doc_example_02.xq	1970-01-01 00:00:00 +0000
@@ -1,8 +0,0 @@
-import module namespace ref =
-  "http://www.zorba-xquery.com/modules/node-reference";;
-
-declare variable $input-context external;
-
-let $book := doc($input-context)/bib/book[1]
-let $bookref := ref:node-reference($book)
-return ref:node-by-reference($bookref)


Follow ups