Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I'm working on XUL and i'm trying to execute XSLT processor function in XUL using JavaScript.

I have JavaScript function where I'm can update new record to my XML file and save the XML file. After that I'm trying to execute the XSLT processor function but I couldn't load my XSL and XML file.

My working environment is in windows using Eclipse, XulBooster. Generally, to load any file I use ("File://C:/mercredi.xml") or ("C:/mercredi.xml"); both file path works fine in other functions even in the JavaScript function which I used the same file path to read and save the XML file.

1.I have copied the following code Listing6: from this website:
http://www.ibm.com/developerworks/xml/library/x-ffox3/index.html

function process()
{
//Create an XSLT processor instance
var processor = new XSLTProcessor();
//Create an empty XML document for the XSLT transform
var transform = document.implementation.createDocument("", "", null);
//Load the XSLT
transform.onload = loadTransform;
transform.load("file://C:/idgenerator.xsl");

//Triggered once the XSLT document is loaded
function loadTransform(){
  //Attach the transform to the processor
  processor.importStylesheet(transform);
  source = document.implementation.createDocument("", "", null);
  source.load("file://C:/mercredi.xml");
  source.onload = runTransform;
}

//Triggered once the source document is loaded
function runTransform(){
  //Run the transform, creating a fragment output subtree that
  //can be inserted back into the main page document object (given
  //in the second argument)
  var frag = processor.transformToFragment(source, document);

}
}

Then I checked Mozilla website and followed the instructions, still I couldn't load my file. 2.The following code is copied from this website: https://developer.mozilla.org/en/Using_the_Mozilla_JavaScript_interface_to_XSL_Transformations

Even in this function I couldn't load my XML file.

function xslt()
{
var processor = new XSLTProcessor();

var testTransform = document.implementation.createDocument("", "test", null);
// just an example to get a transform into a script as a DOM
// XMLDocument.load is asynchronous, so all processing happens in the
// onload handler
testTransform.addEventListener("load", onload, false);
testTransform.load("file://C:/mercredi.xml");
function onload() {
  processor.importStylesheet(testTransform);
}
}

This is my JavaScript code to execute the process() function of XSLT processor.

function saveFile(output, savefile) {

    //function from http://puna.net.nz/archives/Code/Mozilla%20XUL%20LOG%20-%20read%20local%20files%20and%20write%20local%20files.htm
    //var savefile = "c:\\mozdata.txt";

    try {
        netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
    } catch (e) {
        alert("Permission to save file was denied.");
    }
    var file = Components.classes["@mozilla.org/file/local;1"]
        .createInstance(Components.interfaces.nsILocalFile);
    file.initWithPath( savefile );
    if ( file.exists() == false ) {
        alert( "File Updated Successfully ");
        file.create( Components.interfaces.nsIFile.NORMAL_FILE_TYPE, 420 );
    }
    var outputStream = Components.classes["@mozilla.org/network/file-output-stream;1"]
        .createInstance( Components.interfaces.nsIFileOutputStream );
    /* Open flags
    #define PR_RDONLY       0x01
    #define PR_WRONLY       0x02
    #define PR_RDWR         0x04
    #define PR_CREATE_FILE  0x08
    #define PR_APPEND      0x10
    #define PR_TRUNCATE     0x20
    #define PR_SYNC         0x40
    #define PR_EXCL         0x80
    */
    /*
    ** File modes ....
    **
    ** CAVEAT: 'mode' is currently only applicable on UNIX platforms.
    ** The 'mode' argument may be ignored by PR_Open on other platforms.
    **
    **   00400   Read by owner.
    **   00200   Write by owner.
    **   00100   Execute (search if a directory) by owner.
    **   00040   Read by group.
    **   00020   Write by group.
    **   00010   Execute by group.
    **   00004   Read by others.
    **   00002   Write by others
    **   00001   Execute by others.
    **
    */
    outputStream.init( file, 0x04 | 0x08 | 0x20, 420, 0 );
    var result = outputStream.write( output, output.length );
    outputStream.close();
alert( "File Updated Successfully ");
clear();
process();

}

Why I want to execute to XSLT file in my XUL? is to generate a unique ID for my customer in the XML file.

Please help me What am I doing wrong here?!? Thank you very much.

This is my XSLT file:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" indent="yes"/>
    <xsl:strip-space elements="*"/>

    <xsl:template match="node()|@*">
        <xsl:copy>
            <xsl:apply-templates select="node()|@*"/>
        </xsl:copy>
    </xsl:template>

    <xsl:template match="CONTACT">
        <xsl:copy>
               <Customer-Id>
               <xsl:value-of select="generate-id(.)"/> 
               </Customer-Id>
              <xsl:copy-of select="FirstName|LastName|gmail|yahoo| Hotmail |URL|Facebook-ID"/>
        </xsl:copy>
    </xsl:template>

</xsl:stylesheet>
share|improve this question
2  
You forgot to mention what error you get, exactly. (Hint: pasting the error messages verbatim will help others find this post later) –  Tomalak Jul 19 '11 at 8:42
    
Unfortunately, there is no error message in Error Console for this problem. –  linguini Jul 19 '11 at 8:47
    
@karthik: even though there is no error message, you can still tell us what exact symptom you are seeing. How do you know the code is not working? What was the expected behavior, and what was the actual behavior? –  LarsH Jul 19 '11 at 16:12
1  
@Larsh: I've verified with alert messages. alert('1'); transform.load("file://C:/idgenerator.xsl"); alert(2'); Alert 1 works but Alert 2 doesn't work. My code stops working at transform.load(); –  linguini Jul 19 '11 at 20:04
    
@Larsh: I have fixed the problem. Please tell me how can i post the solution? –  linguini Jul 19 '11 at 20:56

1 Answer 1

up vote 0 down vote accepted

For some reason this:

var testTransform = document.implementation.createDocument("", "test", null);

and

testTransform.addEventListener("load", onload, false);testTransform.load("file://C:/mercredi.xml");

it didn't work.

Then I realized that I'm using 'readFile function' in my JavaScript to load file. I used this function to load any file in XUL.

Here is the code:

function process()
{ 
    var src = readFile("c:\\idgenerator.xsl");//load my XSl file
    var parsed = (new DOMParser()).parseFromString(src, "text/xml");
    var stylesheet = parsed.documentElement;
    var processor = new XSLTProcessor();
    processor.importStylesheet(stylesheet );

    objXMLDoc = processor.transformToDocument(objXMLDoc);//load & transform my XML file

    var serializer = new XMLSerializer();
    var prettyString = serializer.serializeToString(objXMLDoc);
    saveFile(prettyString, "C:\\mercredi.xml");//Save the XML file
} 
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.