2 * Cross browser XML Element API
4 * Overrides the Qooxdoo qx.xml.Element to handle the namespace prefixes
6 * http://msdn.microsoft.com/library/default.asp?url=/library/en-us/xmlsdk/html/81f3de54-3b79-46dc-8e01-73ca2d94cdb5.asp
7 * http://developer.mozilla.org/en/docs/Parsing_and_serializing_XML
9 qx
.Class
.define("org.argeo.ria.util.Element",
15 * Selects the first XmlNode that matches the XPath expression.
17 * @param element {Element | Document} root element for the search
18 * @param query {String} XPath query
19 * @param NSMap (Object) A map matching namespace prefixes to namespace URIS;
20 * @return {Element} first matching element
21 * @signature function(element, query, NSMap)
23 selectSingleNode
: qx
.core
.Variant
.select("qx.client",
25 "mshtml|opera": function(element
, query
, NSMap
) {
29 for(var prefix
in NSMap
){
30 namespaces
[i
] = 'xmlns:'+prefix
+'="'+NSMap
[prefix
]+'"';
33 var doc
= element
.ownerDocument
|| element
;
34 doc
.setProperty('SelectionNamespaces', namespaces
.join(" "));
37 return element
.selectSingleNode(query
);
41 "default": function(element
, query
, NSMap
)
44 this.__xpe
= new XPathEvaluator();
52 resolver = function(prefix
){
53 return NSMap
[prefix
] || null;
56 resolver
= xpe
.createNSResolver(element
);
58 //return xpe.evaluate(query, element, xpe.createNSResolver(element), XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
59 return xpe
.evaluate(query
, element
, resolver
, XPathResult
.FIRST_ORDERED_NODE_TYPE
, null).singleNodeValue
;
61 throw new Error("selectSingleNode: query: " + query
+ ", element: " + element
+ ", error: " + err
);
68 * Selects a list of nodes matching the XPath expression.
70 * @param element {Element | Document} root element for the search
71 * @param query {String} XPath query
72 * @param NSMap {Map} Mapping between namespaces prefixes and URI.
73 * @return {Element[]} List of matching elements
74 * @signature function(element, query, NSMap)
76 selectNodes
: qx
.core
.Variant
.select("qx.client",
78 "mshtml|opera": function(element
, query
, NSMap
) {
82 for(var prefix
in NSMap
){
83 namespaces
[i
] = 'xmlns:'+prefix
+'="'+NSMap
[prefix
]+'"';
86 var doc
= element
.ownerDocument
|| element
;
87 doc
.setProperty('SelectionNamespaces', namespaces
.join(" "));
89 return element
.selectNodes(query
);
92 "default": function(element
, query
, NSMap
)
97 this.__xpe
= xpe
= new XPathEvaluator();
103 resolver = function(prefix
){
104 return NSMap
[prefix
] || null;
107 resolver
= xpe
.createNSResolver(element
);
109 //var result = xpe.evaluate(query, element, xpe.createNSResolver(element), XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
110 var result
= xpe
.evaluate(query
, element
, resolver
, XPathResult
.ORDERED_NODE_SNAPSHOT_TYPE
, null);
112 throw new Error("selectNodes: query: " + query
+ ", element: " + element
+ ", error: " + err
);
116 for (var i
=0; i
<result
.snapshotLength
; i
++) {
117 nodes
[i
] = result
.snapshotItem(i
);
126 * Returns a list of elements with the given tag name belonging to the given namespace (http://developer.mozilla.org/en/docs/DOM:element.getElementsByTagNameNS).
128 * @param element {Element | Document} the element from where the search should start.
129 * Note that only the descendants of this element are included in the search, not the node itself.
130 * @param namespaceURI {var} is the namespace URI of elements to look for . For example, if you need to look
131 * for XHTML elements, use the XHTML namespace URI, <tt>http://www.w3.org/1999/xhtml</tt>.
132 * @param tagname {String} the tagname to look for
133 * @return {Element[]} a list of found elements in the order they appear in the tree.
134 * @signature function(element, namespaceURI, tagname)
136 getElementsByTagNameNS
: qx
.core
.Variant
.select("qx.client",
138 "mshtml": function(element
, namespaceURI
, tagname
)
140 var doc
= element
.ownerDocument
|| element
;
142 doc
.setProperty("SelectionLanguage", "XPath");
143 doc
.setProperty("SelectionNamespaces", "xmlns:ns='" + namespaceURI
+ "'");
145 return qx
.xml
.Element
.selectNodes(element
, 'descendant-or-self::ns:' + tagname
);
148 "default": function(element
, namespaceURI
, tagname
) {
149 return element
.getElementsByTagNameNS(namespaceURI
, tagname
);
155 * Selects the first XmlNode that matches the XPath expression and returns the text content of the element
157 * @param element {Element|Document} root element for the search
158 * @param query {String} XPath query
159 * @param NSMap {Object} Mapping between NS prefix / uri
160 * @return {String} the joined text content of the found element or null if not appropriate.
161 * @signature function(element, query)
163 getSingleNodeText : function(element
, query
, NSMap
)
165 var node
= this.selectSingleNode(element
, query
, NSMap
);
166 return qx
.dom
.Node
.getText(node
);