[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message] RE: escaping from CDATA
> Within XPath and XSLT, a node-set consisting of a single text > node can be used anywhere that a string is expected. But not > when you call JavaScript; your JavaScript function is > expecting a string, and it doesn't know what to do if you > give it a node-set instead. But that can easily be fixed in all cases with something like this. function Fun(ctx) { vat ipString = ""; if (typeof(ctx) == "object"){ if (ctx.length){ for (var i=0; i < 1; i++){ ctxN = ctx.item(i); if (ctxN.nodeType == 1) ipString += _wander(ctxN); if (ctxN.nodeType == 2) ipString += ctxN.nodeValue; } }else{ return ''; } }else{ ipString = ctx; } var xml = new ActiveXObject("msxml2.domdocument"); xml.loadXML(ipString); return xml.selectSingleNode("/"); } function _wander(ctx){ var retStr = ""; for (var i=0; i < ctx.childNodes.length; i++){ ctxN = ctx.childNodes[i]; switch(ctxN.nodeType){ case 1: retStr += _wander(ctxN); break; case 3: retStr += ctxN.nodeValue; break; default: break; } } return retStr; } Which says if the argument is type object then it is either an attribute node so use that value or an element/nodelist so wander the subtree getting the string value, else the argument is a string so use that. The _wander function probably isn't needed in this case and you could just use ipString += ctxN.nodeValue; but it allows situation like <parts><![CDATA[<element>]]> <part1> <![CDATA[<element attr="100"><a>100</a><b>200</b></element>]]> </part1> <part2> <![CDATA[<element attr="100"><a>100</a><b>200</b></element>]]> </part2> <![CDATA[</element>]]></parts> If anyone is perverse enough to want to do that. If you are limiting to one cdata then you could just do function Fun(ctx) { var ipString = ""; if (typeof(ctx) == "object"){ if (ctx.length){ ipString = ctxN.nodeValue; }else{ return ''; } }else{ ipString = ctx; } var xml = new ActiveXObject("msxml2.domdocument"); xml.loadXML(ipString); return xml.selectSingleNode("/"); } That way you can pass any type and not have to worry about converting it to a string first. Ciao Chris XML/XSL Portal http://www.bayes.co.uk/xml XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list
|
PURCHASE STYLUS STUDIO ONLINE TODAY!Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced! Download The World's Best XML IDE!Accelerate XML development with our award-winning XML IDE - Download a free trial today! Subscribe in XML format
|