This is the mail archive of the
xsl-list@mulberrytech.com
mailing list .
RE: escaping from CDATA
- To: <xsl-list at lists dot mulberrytech dot com>
- Subject: RE: [xsl] escaping from CDATA
- From: "Chris Bayes" <chris at bayes dot co dot uk>
- Date: Fri, 26 Oct 2001 16:46:39 +0100
- Reply-To: xsl-list at lists dot mulberrytech dot com
Or even just do something that would work
function Fun(ctx) {
var ipString = "";
if (typeof(ctx) == "object"){
if (ctx.length){
ipString = ctx.nextNode.text;
}else{
return '';
}
}else{
ipString = ctx;
}
var xml = new ActiveXObject("msxml2.domdocument");
xml.loadXML(ipString);
return xml.selectSingleNode("/");
}
Ciao Chris
XML/XSL Portal
http://www.bayes.co.uk/xml
> -----Original Message-----
> From: owner-xsl-list@lists.mulberrytech.com
> [mailto:owner-xsl-list@lists.mulberrytech.com] On Behalf Of
> Chris Bayes
> Sent: 26 October 2001 15:38
> To: xsl-list@lists.mulberrytech.com
> Subject: RE: [xsl] 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
>
>
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list