【发布时间】:2011-07-19 08:35:34
【问题描述】:
我正在研究 XUL,我正在尝试使用 JavaScript 在 XUL 中执行 XSLT 处理器功能。
我有 JavaScript 函数,我可以将新记录更新到我的 XML 文件并保存 XML 文件。之后,我尝试执行 XSLT 处理器功能,但无法加载 XSL 和 XML 文件。
我的工作环境是在 windows 中使用 Eclipse,XulBooster。通常,要加载我使用的任何文件 ("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>
【问题讨论】:
-
你忘了提到你得到了什么错误,确切地说。 (提示:逐字粘贴错误消息将有助于其他人稍后找到此帖子)
-
很遗憾,错误控制台中没有针对此问题的错误消息。
-
@karthik:即使没有错误消息,您仍然可以告诉我们您看到的确切症状。你怎么知道代码不起作用?预期行为是什么,实际行为是什么?
-
@Larsh:我已经通过警报消息进行了验证。警报('1'); transform.load("file://C:/idgenerator.xsl");警报(2');警报 1 有效,但警报 2 无效。我的代码在 transform.load(); 处停止工作
-
@Larsh:我已经解决了这个问题。请告诉我如何发布解决方案?
标签: javascript xml xslt xul