/*
 | Title: Tópico XML scripts
 | Author: Raymond Bissonnette
 | Revision: 2008-07-09
 */

////////////////////////////////////////////////////////////////////////////////////////////
function LoadXmlFileInElementId(sXmlFileName, sXPath, sId) {
	
	//http://dev.abiss.gr/sarissa/
	//http://www.xml.com/pub/a/2005/02/23/sarissa.html
	//http://www.gemusicboosters.org/technical/transformation_configuration_setup.html
	
	//alert(sXmlFileName);
	
	var output = document.getElementById(sId);
	
	if (output != null) {
		var xmlDoc = Sarissa.getDomDocument();  
		xmlDoc.async = false;
		xmlDoc.load(sXmlFileName);
		
		if(xmlDoc.parseError.errorCode != 0){
			output.innerHTML = "The XML file is not well formed or there was an other error!";   
            } 

		try {
			xmlDoc.setProperty("SelectionLanguage", "XPath");   
			xmlDoc.setProperty("SelectionNamespaces", "xmlns:xhtml='http://www.w3.org/1999/xhtml' xmlns:xsl='http://www.w3.org/1999/XSL/Transform'");
			 
			var targets = xmlDoc.selectNodes(sXPath);
			//write the target element to the output element
			output.innerHTML = new XMLSerializer().serializeToString(targets[0])
		    }
		catch(e) {output.innerHTML = e.message;}
	    }
	else {alert("Cannot find element with the following attribute: id = " + sId );}
    }

////////////////////////////////////////////////////////////////////////////////////////////
function TransformXmlFileInElementId(sXmlFileName, sXPath, sId, sXslFileName) {
	
    //http://dev.abiss.gr/sarissa/
    //http://www.xml.com/pub/a/2005/02/23/sarissa.html
    //http://www.gemusicboosters.org/technical/transformation_configuration_setup.html

    //alert(sXmlFileName);

    var output = document.getElementById(sId);

    if (output != null) {
    		
	    //load xml document
	    var xmlDoc = Sarissa.getDomDocument();  
	    xmlDoc.async = false;
	    xmlDoc.load(sXmlFileName);
	    if(xmlDoc.parseError.errorCode != 0) {
		    output.innerHTML = "The XML file is not well formed or there was an other error!";   
            }
    	
	    //load xsl document
	    var xslDoc = Sarissa.getDomDocument();   
	    xslDoc.async = false;
	    xslDoc.load(sXslFileName);
	    if(xslDoc.parseError.errorCode != 0) {  
		    output.innerHTML = "The XSL file is not well formed or there was an other error!";  		
            }

        xmlDoc.setProperty("SelectionLanguage", "XPath");   
        xmlDoc.setProperty("SelectionNamespaces", "xmlns:xhtml='http://www.w3.org/1999/xhtml' xmlns:xsl='http://www.w3.org/1999/XSL/Transform'");
        
        xslDoc.setProperty("SelectionLanguage", "XPath");   
        xslDoc.setProperty("SelectionNamespaces", "xmlns:xhtml='http://www.w3.org/1999/xhtml' xmlns:xsl='http://www.w3.org/1999/XSL/Transform'");
    
        try {
            // create an instance of XSLTProcessor   
            var processor = new XSLTProcessor(); 
            processor.importStylesheet(xslDoc);  
            //processor.setParameter(null, "title", "1");
            //alert("importing stylesheet");
            }
        catch(e) {
            //alert("error importing stylesheet : " & e.message);
            output.innerHTML = e.message;
            }
      
        try {
            // transform the document  
            var newDoc = processor.transformToDocument(xmlDoc);
            //alert("transforming document");
          
            newDoc.setProperty("SelectionLanguage", "XPath");   
            newDoc.setProperty("SelectionNamespaces", "xmlns:xhtml='http://www.w3.org/1999/xhtml' xmlns:xsl='http://www.w3.org/1999/XSL/Transform'");  
          
            //var targets = xmlDoc.selectNodes('//xhtml:body');
            //var targets = xslDoc.selectNodes('//xsl:stylesheet');
            var targets = newDoc.selectNodes(sXPath);
            //alert(x.length);
          
            // show transformation results   
            //alert(new XMLSerializer().serializeToString(newDoc));	
            //output.innerHTML = new XMLSerializer().serializeToString(newDoc);
            //alert(new XMLSerializer().serializeToString(div[0]));
            output.innerHTML = new XMLSerializer().serializeToString(targets[0]);
            }
        catch (e) {
            //alert("error transforming document : " & e.message);
            output.innerHTML = e.message;
            }
        }
	else {alert("Cannot find element with the following attribute: id = " + sId );}
    }
