Featured post

c# - Usage of Server Side Controls in MVC Frame work -

i using asp.net 4.0 , mvc 2.0 web application. project requiremrnt have use server side control in application not possibl in noraml case. ideally want use adrotator control , datalist control. i saw few samples , references in codepleax mvc controllib howwver found less useful. can tell how utilize theese controls in asp.net application along mvc. note: please provide functionalities related adrotator , datalist controls not equivalent functionalities thanks in advace. mvc pages not use normal .net solution makes use of normal .net components impossible. a normal .net page use event driven solution call different methods service side mvc use actions , view completly different way handle things. also, mvc not use viewstate normal .net controlls require. found article discussing mixing of normal .net , mvc.

javascript - How do I retrieve the value of a specific XML node by path? -


i looking javascript solution problem:

i have xml document loaded way of typical ajax method:

var xml = http.responsexml;

consider following example xml snippet:

<main>     <primary>          <enabled>true</enabled>      </primary>      <secondary>          <enabled>true</enabled>      </secondary>  </main> 

i value of node specifying path follows:

var second_enabled = getnodevaluebypath('main/secondary/enabled', xml); 

i cannot find concise way of doing this. seem forced iterate through node collections after using getelementsbytagname , like.

how construct method getnodevaluebypath or there construct in javascript allows this?

i not versed in javascript.

edit: here example shows attempt @ using xpath , how failing:

xml:

<?xml version="1.0" ?> <main xmlns="example.com">   <primary>     <enabled>true</enabled>   </primary>   <secondary>     <enabled>false</enabled>   </secondary> </main> 

javascript: (these relevant functions only)

function usehttpresponse() {     if (http.readystate == 4)     {         if(http.status == 200)         {             var xml = http.responsexml;             var evalue = getxmlvaluebypath('/main/secondary/enabled', xml);             alert(evalue);         }     } }  function getxmlvaluebypath(nodepath, xml) {     var result = xml.evaluate(nodepath, xml, null, xpathresult.string_type, null).stringvalue;     return result; } 

i using above javascript without additional libraries. using mozilla firefox 3.6.13 uses document.evaluate method select nodes (according this information w3schools). this application internal use , not have work on multiple browsers.

given these examples, alert dialog appear, contain no text. if remove string xmlns="example.com" xml document, alert dialog appears text "false" desired.

debugging using firebug shows resultnode empty string long namespace declared.

this faq: in xpath 1.0, qname test without prefix selects elements under null (or empty) namespace uri. need register prefix default namespace uri input source (example.com).

how do dom 3 xpath api?

from http://www.w3.org/tr/dom-level-3-xpath/xpath.html#xpathevaluator-evaluate

evaluate
evaluates xpath expression string , returns result of specified type if possible.
parameters
[...]
resolver of type xpathnsresolver
resolver permits translation of prefixes, including xml namespace prefix, within xpath expression appropriate namespace uris. if specified null, namespace prefix within expression result in domexception being thrown code namespace_err.

and https://developer.mozilla.org/en/dom/document.evaluate

var xpathresult = document.evaluate(  xpathexpression,   contextnode,   namespaceresolver,   resulttype,   result); 
  • namespaceresolver function passed namespace prefixes , should return string representing namespace uri associated prefix. used resolve prefixes within xpath itself, can matched document. null common html documents or when no namespace prefixes used.

following guide, can use document.creatensresolver or function like:

function nsresolver(prefix) {      return prefix == 'ex' ? 'example.com' : null;    }    function usehttpresponse() {   if (http.readystate == 4)   {      if (http.status == 200)      {         var xml = http.responsexml;         var evalue = xml.evaluate('string(/ex:main/ex:secondary/ex:enabled)',                                   xml,                                   nsresolver,                                   xpathresult.string_type,                                   null);         alert(evalue.stringvalue);      }   } }      

Comments

Popular posts from this blog

c# - Usage of Server Side Controls in MVC Frame work -

cocoa - Nesting arrays into NSDictionary object (Objective-C) -

ios - Very simple iPhone App crashes on UILabel settext -