|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
CDATA in Javascript function
Hi,
I am having a problem retrieving the html tags from my XML document when it's being loaded into a DOM object. For example, my xml contains the following: <my:InsideView> . . . <my:Body> <b>Guess what happened today?</b> <p>This is example text</p> <p>I want to select all content within the my:body tag but the html tags are also getting interpreted as xml tags!</p> </my:Body> . . . </my:InsideView> My javascript function is as follows . . . strTemp = ""; xmlObj = (xmlDocs.selectNodes("//my:InsideView//my:Body")); strTemp = xmlObj.item(0).text; if (strTemp.length > 0) { document.all.mainText.innerHTML = strTemp; } else { document.all.mainText.innerHTML = ""; } . . . The problem though, is that when the content is displayed in the <div> tag, all the HTML <b> and <p> tags within <my:Body> have been interpreted as xml and not reflected in my <div> Is there a way to select the <my:Body> node in Javascript and tell it to ignore any other tags within <my:Body> - in affect using a CDATA type function??? Any suggestions would be most appreciated!! |
|
#2
|
|||
|
|||
|
Hello Leila, here's my daily answer
![]() The first thing I'd say is that you can always write small xsl:templates to have the HTML tags processed. I know the goal is not to recreate a full list of templates for all HTML tags, but if it's for one or two, it can be an acceptable solution. For the rest, I think that the following piece of code, although it's unrelated, will help you.. remember this XMLpointer thing I had proposed into this post (solution 2.. improved) http://forums.devshed.com/t168864/s.html Well, I managed to simulate xpointers by using some JS. With this, I can now point into any xml file which is logically seen as a DB, and transform the result of my selection. Here's the explanation, a bit long, but definitely worth the read: XML: Code:
<XMLPointer URL="mydb.xml" XSL="anyfile.xsl" Layout="anylaout.xml" Select="//User[Name=\'UserName\']"> AnyText </XMLPointer> XSL: Code:
<xsl:template match="XMLPointer">
<a target="_self" href="javascript:ProcessXMLPointer('{@URL}','{@Select}','{@XSL}','{@Layout}');">
<xsl:value-of select="text()" />
</a>
</xsl:template>
With this, I create a link to my JS function, and pass it my XML attributes as parameters. Their meaning is: - URL = the xml file containing the data to be extracted could look like this: <Users> <User> <Name>..</Name> </User> ... more users </Users> - XSL = the xsl file used for the transformation - Layout = a layout file.. the reason of its existence is rather simple.. when you make an xpath selection into a DB, the result is a part of the source tree.. in this case, the result would be <User>...</User>, but you might be looking forward to integrate this hierarchy into an xml (not xsl!!!) template written specifically for displaying user information. Here's how I do it: Code:
<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet href="anyfile.xsl" type="text/xsl"?>
<Root>
<UserInfo>
<ReplaceMe/>
.. any structure in here
</UserInfo>
</Root>
By doing this, I create the general structure of the page, and use the tag <ReplaceMe/> as a bookmark to know where I'll insert my selection from JS (see further). - Select = my selection, in xpath format.. note that I have to use \' instead of ' to be sure that strings are passed correctly to JS. In pseudo code, here's what my JS function does (full code follows): - load my xml DB - retrieve the selection using the xpath value - load the layout file into a string (let's call it strLayout) - replace all occurences of <ReplaceMe/> with the DB selection - load the stylesheet - transform and display the results now the JS code: Code:
/*
This function simulates the behaviour of an xpointer, and makes sure that a selection into an
XML file get transformed by an XSL file, using a given layout
*/
function ProcessXMLPointer(XMLSource, XMLSelection, XSLFile, XMLLayout)
{
// Load XML database
var xmldb = Sarissa.getDomDocument();
xmldb.async = false;
xmldb.load(XMLSource);
// the following two lines are needed for IE
xmldb.setProperty("SelectionNamespaces", "xmlns:xsl='http://www.w3.org/1999/XSL/Transform'");
xmldb.setProperty("SelectionLanguage", "XPath");
// Make the XPath Selection on the DB
var objNodeList = xmldb.selectNodes(XMLSelection);
var xmlReplaceString = "";
for(i=0;i<objNodeList.length;i++)
xmlReplaceString += objNodeList[i].xml;
// Load the Layout file
var xml_layout = Sarissa.getDomDocument();
xml_layout.async = false;
xml_layout.load(XMLLayout);
var xmlString = "";
var objReplaceNodes = xml_layout.selectNodes("/");
for(i=0;i<objReplaceNodes.length;i++)
xmlString += objReplaceNodes[i].xml;
// While <ReplaceMe/> is found, loop
for (;xmlString.indexOf('<ReplaceMe/>') != -1;)
{
xmlString = xmlString.replace('<ReplaceMe/>',xmlReplaceString);
}
var xmlPage = Sarissa.getDomDocument();
xmlPage.loadXML(xmlString);
// get the stylesheet document
var xsl = Sarissa.getDomDocument();
xsl.async = false;
xsl.load(XSLFile);
var sResult = xmlPage.transformNode(xsl);
document.write(sResult);
}
Although it may sound tricky, it's actually very logical, and very practical. I hope you'll find this example helpful. ![]() |
|
#3
|
|||
|
|||
|
Quote:
Hi there! Once again, many thanks for a very comprehansive answer. Unfortunately though, I wasn't able to use this idea as I cannot use an XSLT in this instance. I am parsing my XML doc into Javascript and using the various methods and objects available to me (DOM) to display the content within the XML nodes on my html page. The code that solved my problem was that instead of: strTemp = xmlDocs.selectNodes("//my:InsideView//my:Body")[0].text; I use: strTemp = xmlDocs.selectNodes("//my:InsideView//my:Body")[0].xml; This means that strTemp contains a string of everything in the object <my:Body> including the HTML tags. These are not interpreted as XML nodes (as before) so when I insert the strTemp into my <div> tag, the correct formatting in displayed! It's amazing how one line of code can cause such frustration! Many thanks for taking the time to answer! |
![]() |
| Viewing: Dev Shed Forums > Programming Languages - More > XML Programming > CDATA in Javascript function |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|
|