[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

RE: escaping from CDATA

Subject: RE: escaping from CDATA
From: "Chris Bayes" <chris@xxxxxxxxxxx>
Date: Fri, 26 Oct 2001 15:37:54 +0100
cdata nodevalue
> 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


Current Thread

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.