【发布时间】:2010-01-11 13:44:00
【问题描述】:
我有以下应用 XSLT 样式的代码
Test.Xml.xslTransform = function(xml, xsl) {
try {
// code for IE
if (window.ActiveXObject) {
ex = xml.transformNode(xsl);
return ex;
}
// code for Mozilla, Firefox, Opera, etc.
else if (document.implementation && document.implementation.createDocument) {
xsltProcessor = new XSLTProcessor();
xsltProcessor.importStylesheet(xsl);
resultDocument = xsltProcessor.transformToFragment(xml, document);
return resultDocument;
}
} catch (exception) {
if (typeof (exception) == "object") {
if (exception.message) {
alert(exception.message);
}
} else {
alert(exception);
}
}
该代码在 IE 和 firefox 中有效,但在 Chrome 和 Safari 中无效。任何想法为什么?
更新
ResultDocument = xsltProcessor.transformToFragment(xml, document);
上面的行返回 null。没有抛出任何错误。
更新
代码不工作,因为 xslt 文件包含 xsl:include。需要找到一种方法让包含工作我将在此处粘贴进度
更新
建议我使用http://plugins.jquery.com/project/Transform/ 插件。我正在尝试使用客户端库作为包含作品的示例(http://daersystems.com/jquery/transform/)。
代码在 IE 中可以运行,但在 chrome 中仍然不行。
Test.Xml.xslTransform = function(xml, xsl) {
try {
$("body").append("<div id='test' style='display:none;'></div>");
var a = $("#test").transform({ xmlobj: xml, xslobj: xsl });
return a.html();
}
catch (exception) {
if (typeof (exception) == "object") {
if (exception.message) {
alert(exception.message);
}
} else {
alert(exception);
}
}
}
xml和xsl都是传入的对象。
更新
我尝试将 XSL 文件更改为没有包含的非常简单的文件,Chrome 仍然没有应用样式表,而 IE 是。作为对象引入的 XSL 是:
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:rs="urn:schemas-microsoft-com:rowset"
xmlns:z="#RowsetSchema"
xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:spsoap="http://schemas.microsoft.com/sharepoint/soap/"
>
<xsl:output method="html"/>
<xsl:template match="/">
<h1>test</h1>
</xsl:template>
</xsl:stylesheet>
更新
我想要的最终结果是将 xsl 应用于 xml 文件。 xsl 文件包含在其中。理想情况下,我希望在客户端上进行传输。
更新 Rupert 能否用 xml 更新问题以及如何调用 Test.Xml.xslTransform ?
我使用 ie8 得到了 xml
<?xml version="1.0"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><soap:Body><SearchListItemsResponse xmlns="http://schemas.microsoft.com/sharepoint/soap/"><SearchListItemsResult><listitems xmlns:s="uuid:BDC6E3F0-6DA3-11d1-A2A3-00AA00C14882" xmlns:dt="uuid:C2F41010-65B3-11d1-A29F-00AA00C14882" xmlns:rs="urn:schemas-microsoft-com:rowset" xmlns:z="#RowsetSchema">
<rs:data ItemCount="1">
<z:row ows_Title="Test" ows_FirstName="Test 4" ows_UniqueId="74;#{1A16CF3E-524D-4DEF-BE36-68A964CC24DF}" ows_FSObjType="74;#0" ows_MetaInfo="74;#" ows_ID="74" ows_owshiddenversion="10" ows_Created="2009-12-29 12:21:01" ows_FileRef="74;#Lists/My List Name/74_.000" ReadOnly="False" VerificationRequired="0"/>
</rs:data>
</listitems></SearchListItemsResult></SearchListItemsResponse></soap:Body></soap:Envelope>
代码调用如下:
xsl = Test.Xml.loadXMLDoc("/_layouts/xsl/xsl.xslt");
var doc = Test.Xml.xslTransform(xData.responseXML, xsl);
xData 是 Web 服务返回的 xml。
【问题讨论】:
-
请提供更多信息,例如异常消息。我推荐使用 Sarissa(用于跨浏览器 XML/XSLT 的开源 JavaScript)。
-
也许 XML 上的 XSLT 没有返回任何结果,因为 XSLT 中的模板都不匹配 XML 文档。请参阅我的博客“避免常见的 XSLT 错误”(dev.ektron.com/blogs.aspx?id=22956),尤其是链接“XSLT:命名空间”(dev.ektron.com/kb_article.aspx?id=481)
-
看起来在 xslt 中使用 xsl:include 有问题。目前正在寻找解决方法。
-
Rupert 能否用 xml 更新问题以及如何调用
Test.Xml.xslTransform? -
Rupert 似乎在使用对象时存在问题。你能确认我发布的修复对你有用吗?
标签: javascript jquery xslt safari google-chrome