XML Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming Languages - MoreXML Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old August 12th, 2004, 07:41 AM
1ei1a 1ei1a is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2004
Posts: 32 1ei1a User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 28 m 13 sec
Reputation Power: 5
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!!

Reply With Quote
  #2  
Old August 12th, 2004, 08:37 AM
kid23 kid23 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2004
Posts: 62 kid23 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 5
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.

Reply With Quote
  #3  
Old August 12th, 2004, 12:34 PM
1ei1a 1ei1a is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2004
Posts: 32 1ei1a User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 28 m 13 sec
Reputation Power: 5
Quote:
Originally Posted by kid23
Hello Leila, here's my daily answer
I hope you'll find this example helpful.


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!

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming Languages - MoreXML Programming > CDATA in Javascript function


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

 Free IT White Papers!
 
How to Present Effectively Online
This white paper offers practical and actionable advice on the key steps that any presenter should consider as they plan and execute a Webinar or online meeting.

 
Open Source Security Myths
Open Source Software (OSS) is computer software whose source code is available to the general public with relaxed or non-existent intellectual property restrictions (or arrangement such as the public domain), and is usually developed with the input of many contributors.

 
Power and Cooling Capacity Management for Data Centers
This paper describes the principles for achieving power and cooling capacity management.

 
Scalable, Fault-Tolerant NAS for Oracle - The Next Generation
For several years NAS has been evolving as a storage alternative for Oracle databases, and for good reason: NAS is quite often the simplest, most cost-effective storage approach for Oracle. Learn about the benefits that HP's approach to scalable NAS brings to Oracle environments in this comprehensive white paper.

 
Understanding Web Application Security Challenges
This white paper discusses many common threats and preventive measures for Web application security, and explains what you can do to help protect your organization.

 

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway
Stay green...Green IT