0 Replies Latest reply on Jun 9, 2007 12:46 AM by holla2040

    xpath, node absolute path and Remote Events for XML (REX)

    holla2040
      Folks,
      No question here, just a solution to share.

      I want to implement Remote Events for XML (REX) in Actionscript 3. Outgoing rex events require that I find an absolute xpath expression for my XML DOM nodes that have changes. Incoming rex events require that I can find the appropriate XML DOM node using xpath.

      nodeAbsolutePath returns a node's absolute xpath expression. (see below)
      xpath-as3 provides xpath.

      References
      http://www.w3.org/TR/rex/
      http://code.google.com/p/xpath-as3/wiki/GettingStarted
      http://www.xml.com/pub/a/2004/11/24/py-xml.html
      http://forums.oracle.com/forums/thread.jspa?messageID=1439497&#1439497

      public function nodeAbsolutePath(node:XML):String {
      var name:String = node.name();
      var parent:XML = node.parent();
      var index:int = 1;
      var pos:String;

      if (parent) {
      for each (var child:XML in parent.children()) {
      if (child == node) {
      break;
      }
      if (child.name() == name) {
      index += 1;
      }
      }
      pos = "["+String(index)+"]"; /* [1], [2], ... */
      switch (node.nodeKind()) {
      case 'text':
      return nodeAbsolutePath(parent)+"/text()"+pos;
      break;
      case 'comment':
      return nodeAbsolutePath(parent)+"/comment()"+pos;
      break;
      case 'processing-instruction':
      return nodeAbsolutePath(parent)+"/processing-instruction()"+pos;
      break;
      case 'attribute':
      return nodeAbsolutePath(parent)+"/@"+name;
      break;
      case 'element':
      return nodeAbsolutePath(parent)+"/"+name+pos;
      break;
      }
      }
      return "/"+name+"[1]"; /* we're at the top baby*/
      }

      Here are some links.
      http://hollabaugh.com/flex/NodeAbsolutePath.mxml.txt
      http://hollabaugh.com/flex/NodeAbsolutePath.swf

      Have fun!
      Craig