2011-07-19 19 views
1

私はXULで作業していますが、JavaScriptを使用してXULでXSLTプロセッサ関数を実行しようとしています。XUL JavaScriptでXSLT変換ファイルを実行する際の問題

私は自分のXMLファイルに新しいレコードを更新してXMLファイルを保存できるJavaScript関数を持っています。その後、XSLTプロセッサの機能を実行しようとしていますが、XSLとXMLファイルをロードできませんでした。

私の仕事環境は、Eclipse、XulBoosterを使用しているウィンドウです。一般に、私が使用するファイル( "File:// C:/mercredi.xml")または( "C:/mercredi.xml")をロードするには、どちらのファイルパスも、同じファイルパスを使用してXMLファイルを読み込んで保存するJavaScript関数であっても、他の関数でもうまく機能します。

1.Iは、次のコードListing6をコピーした:このウェブサイトから: 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のWebサイトをチェックし、指示に従いました。 2.次のコードは、このWebサイトからコピーされています。 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); 
} 
} 

これは私のJavaScriptコードであり、XSLTプロセッサのprocess()関数を実行します。

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ファイルに実行したいのはなぜですか?私の顧客の一意のIDをXMLファイルに生成することです。

私を助けてください私はここで間違って何ですか?!?どうもありがとうございました。

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

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

それは動作しませんでした。この何らかの理由

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

あなたは正確に、あなたは何を得るのエラー言及するのを忘れてしまった:ここ

はコードです。 (ヒント:エラーメッセージをそのまま貼り付けると後でこの記事を見つけるのに役立ちます) – Tomalak

+0

残念ながら、この問題はエラーコンソールには表示されません。 – linguini

+0

@karthik:エラーメッセージは表示されませんが、表示されている正確な症状を教えてください。コードが動作していないことをどのように知っていますか?期待された行動は何で、実際の行動は何でしたか? – LarsH

答えて

0

これは私のXSLTファイルです。

次に、JavaScriptを読み込むためにJavaScriptで 'readFile function'を使用していることに気付きました。この関数を使用して、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 
} 
関連する問題