在XUL JavaScript中执行XSLT转换文件时出现问题

时间:2011-07-19 08:35:34

标签: javascript xml xslt xul

我正在研究XUL,我正在尝试使用JavaScript在XUL中执行XSLT处理器功能。

我有JavaScript功能,我可以将新记录更新到我的XML文件并保存XML文件。之后,我正在尝试执行XSLT处理器功能,但我无法加载我的XSL和XML文件。

我的工作环境是在使用Eclipse,XulBooster的Windows中。通常,要加载我使用的任何文件(“File:// C:/mercredi.xml”)或(“C:/mercredi.xml”);即使在我使用相同文件路径读取和保存XML文件的JavaScript函数中,两个文件路径也可以在其他函数中正常工作。

1.我已复制以下代码清单6:来自本网站:
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);

}
}

然后我检查了Mozilla网站并按照说明操作,但仍无法加载我的文件。 2.以下代码从本网站复制: https://developer.mozilla.org/en/Using_the_Mozilla_JavaScript_interface_to_XSL_Transformations

即使在这个函数中,我也无法加载我的XML文件。

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);
}
}

这是我执行XSLT处理器的process()函数的JavaScript代码。

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();

}

为什么我要在XUL中执行XSLT文件?是在XML文件中为我的客户生成唯一ID。

请帮帮我,我在这里做错了什么?!?非常感谢你。

这是我的XSLT文件:

<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>

1 个答案:

答案 0 :(得分:0)

出于某种原因:

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

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

它不起作用。

然后我意识到我在JavaScript中使用'readFile函数'来加载文件。我使用此函数加载XUL中的任何文件。

以下是代码:

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
}