Click here to Skip to main content
6,595,854 members and growing! (16,906 online)
Email Password   helpLost your password?
Web Development » Client side scripting » General     Intermediate

Creating dynamic xml/xslt sub pages within existing page

By sashok(rus)

use ov Div and custom “smartDiv” JavaScript object to create shopping module.
Javascript, XML, CSS, HTML, Win Mobile, Win2K, WinXP, Visual Studio, Dev
Posted:14 Oct 2005
Views:27,707
Bookmarked:11 times
Unedited contribution
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
10 votes for this article.
Popularity: 3.00 Rating: 3.00 out of 5
3 votes, 30.0%
1
1 vote, 10.0%
2

3
2 votes, 20.0%
4
4 votes, 40.0%
5

Creating dynamic xml/xslt sub pages within existing page.

 

Theory:

Using <DIV> elements creates logical divisions for your Web page. With a little help from javascript Div can also be used in changing visual appearance of the pages. The big deal about DIV is the innerHTML property. By manipulating innerHTML, a page can be turned into a set of appearance, data and style independent sub pages.

 

Practical:

Sample screenshot 

Presented above is product ordering menu and basket. The basket uses my custom �smartDiv� JavaScript object. Use of  this object helps to create persistent, save state shopping module within the page. �smartDiv� is an object located in external js file. It has 2 parameters:

  1. xml root name;
  2. url of Transformer(xslt).

SmartDiv has �SET� properties describing DIV�s visual appearance.

  1. SetClassName;
  2. SetPosition;
  3. SetSize;
  4. SetBorder;

 

Also it has an important property xml_dom(will be explained in details later).  The only method smartDiv has - is Transform XML method. This function retrieves XSLT by using XMLHTTP and parameter URL. Next step it transforms xml_dom into html by using XSLT and writes result into DIV�s innerHTML.

 

function smartDiv(rootname,URL) {

  this.Display = document.createElement('div'); // create

  this.Display.id = 'bask';

  document.body.appendChild(this.Display);

  this.Display.style.position = 'absolute';

  this.Display.innerHTML ='';

 

  this.SetClassName = function(cname) {

   --------------------

  }

  this.SetSize = function(w,h) {

   --------------------

  }

  this.SetPosition = function(l,t) {

   --------------------

  }

  this.SetBorder = function(bs,bc,bw) {

   --------------------

  }

  this.xmlHTTP = null;

  this.xml_dom = new ActiveXObject("MSXML2.DOMDocument");

  this.xml_dom.loadXML('<?xml version="1.0" ?>');

  this.root = this.xml_dom.createElement(rootname);

  this.xml_dom.appendChild(this.root);

  this.xslresponse =null;

 

  this.TransformXML = function() {

    try {

      this.xmlHTTP = new ActiveXObject("Microsoft.XMLHTTP");

      this.xmlHTTP.open("GET",URL,false);

      this.xmlHTTP.send();

      this.xslresponse = this.xmlHTTP.responseText;

     

      this.xml = new ActiveXObject("Msxml2.DOMDocument.4.0");

      this.xsl = new ActiveXObject("Msxml2.DOMDocument.4.0");

      this.xml.async = false;

      this.xsl.async = false;

      this.xml.validateOnParse = false;

      this.xml.loadXML(this.xml_dom.xml);

      this.xsl.loadXML(this.xslresponse);

      this.Display.innerHTML = this.xml.transformNode(this.xsl);

    } catch (e){

      alert(e.description);

  }}}

 

How to make �smartDiv� work:

 

1. Ordering page creates an instance of smartDiv <body onload="initDiv();">

If you noticed, I call this instance � Basket. Size, position and style are being set next. Also name of the root tag and xslt�s url are assigned.

 

function initDiv() {

 Basket = new smartDiv('Basket','basket.xslt');

 Basket.SetClassName('Border');

 Basket.SetSize(180,280);

 Basket.SetPosition(200,20);

 Basket.SetBorder('dashed','#999999',1);

 }

 

2. Each time quantity of ordered goods changes, XML adjusted accordingly.

This is where smartDiv�s xml_dom property is being used.

Going trough list of �TEXT� elements we collect them into xml_dom.

It looks like that:

<Basket>

  <Product qty=�1� name=�fish� price=�1.99�/>

</Basket>

Now the only thing left for us to do is to call smartDiv�s Transform method and it does its job for us. Results are being displayed within DIV element.

 

 smartDiv.prototype.BuildXML = function() {

 var elm = document.forms[0].elements;

 oSelection = this.xml_dom.selectNodes("//Product");

 oSelection.removeAll();

 for (var i=0; i < elm.length ;++i){

    if (elm[i].type == 'text') {

    var Product = this.xml_dom.createElement("Product");

    var attr = this.xml_dom.createAttribute("qty");

    attr.value = elm[i].value;

    Product.setAttributeNode(attr);

    attr = null;

  

    ---------------------------

 

    this.root.appendChild(Product);

 }}

 this.TransformXML();

}

var Basket;

 

Other Functionality:

Function Clear speaks for itself. It cleans all �TEXT� elements and xml_dom as well.

 

function Clear() {

var elm = document.forms[0].elements;

for (var i=0; i < elm.length ;++i){

  if (elm[i].type == 'text') {

       elm[i].value = '';

  }}

Basket.BuildXML();

}

 

 

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

sashok(rus)


Member

Location: United States United States

Other popular Client side scripting articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 12 of 12 (Total in Forum: 12) (Refresh)FirstPrevNext
General10x PinmemberWashu Seino4:55 25 Oct '05  
GeneralRe: 10x PinmemberAlex.Bykovsky8:48 25 Oct '05  
GeneralNot very usable in real world PinmemberJohn Hann12:33 14 Oct '05  
GeneralRe: Not very usable in real world PinmemberAlex.Bykovsky12:38 14 Oct '05  
GeneralRe: Not very usable in real world PinmemberTornn12:59 15 Oct '05  
GeneralRe: Not very usable in real world PinmemberAlex.Bykovsky13:58 15 Oct '05  
GeneralRe: Not very usable in real world PinmemberAlex.Bykovsky12:45 14 Oct '05  
GeneralContenders to the throne Pinmemberfwsouthern15:51 14 Oct '05  
GeneralRe: Not very usable in real world Pinmembertripwirezone14:13 19 Oct '05  
GeneralRe: Not very usable in real world PinmemberAlex.Bykovsky6:37 20 Oct '05  
GeneralSource vs Demo ??? Pinmemberfwsouthern9:56 14 Oct '05  
GeneralRe: Source vs Demo ??? PinmemberAlex.Bykovsky11:23 14 Oct '05  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 14 Oct 2005
Editor:
Copyright 2005 by sashok(rus)
Everything else Copyright © CodeProject, 1999-2009
Web22 | Advertise on the Code Project