简体   繁体   English

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

[英]Problem in executing XSLT transform file in XUL JavaScript

I'm working on XUL and i'm trying to execute XSLT processor function in XUL using JavaScript.我正在研究 XUL,我正在尝试使用 JavaScript 在 XUL 中执行 XSLT 处理器 function。

I have JavaScript function where I'm can update new record to my XML file and save the XML file.我有 JavaScript function 在那里我可以将新记录更新到我的 XML 文件并保存 Z3501BB093D39CFED38 文件After that I'm trying to execute the XSLT processor function but I couldn't load my XSL and XML file.之后,我尝试执行 XSLT 处理器 function 但我无法加载我的 XSL 和 XML 文件。

My working environment is in windows using Eclipse, XulBooster.我的工作环境是 windows,使用 Eclipse,XulBooster。 Generally, to load any file I use ("File://C:/mercredi.xml") or ("C:/mercredi.xml");通常,要加载我使用的任何文件 ("File://C:/mercredi.xml") 或 ("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.即使在 JavaScript function 中,这两个文件路径在其他功能中都可以正常工作,我使用相同的文件路径来读取和保存 XML 文件。

1.I have copied the following code Listing6: from this website: 1.我从这个网站复制了以下代码清单6:
http://www.ibm.com/developerworks/xml/library/x-ffox3/index.html 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.然后我检查了 Mozilla 网站并按照说明进行操作,但我仍然无法加载我的文件。 2.The following code is copied from this website: https://developer.mozilla.org/en/Using_the_Mozilla_JavaScript_interface_to_XSL_Transformations 2.以下代码复制自本网站: 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 中,我也无法加载我的 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);
}
}

This is my JavaScript code to execute the process() function of XSLT processor.这是我的 JavaScript 代码,用于执行 XSLT 处理器的进程() function。

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?为什么我要在我的 XUL 中执行 XSLT 文件? is to generate a unique ID for my customer in the XML file.是在 XML 文件中为我的客户生成一个唯一 ID。

Please help me What am I doing wrong here??.请帮帮我我在这里做错了什么???。 Thank you very much.非常感谢。

This is my XSLT file:这是我的 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>

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.然后我意识到我在 JavaScript 中使用“readFile 函数”来加载文件。 I used this function to load any file in XUL.我用这个 function 加载 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
} 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM