2

我目前正在尝试将多个 xml 文件放入我的 xsl 文件中,然后将它们处理成有用的 html。这适用于非 webkit 浏览器,但不适用于 webkit 浏览器。

我正在使用 JavaScript 执行转换,除了我想要多个 xml 文件之外,它在所有浏览器中都可以正常工作。

我尝试了各种方法来尝试将额外的 xml 文件放入 xsl,这些文件都在同一台服务器上,但在不同的文件夹中。

1)我尝试将 xml 文件直接添加为变量。例如:

<xsl:variable name="structure" select="document('../structure.xml')"/> 

从那以后,我发现 webkit 浏览器不允许这样做,因为它被认为是安全风险。但是,如果有人知道任何解决方法或不同的方法来实现相同的结果,那就太好了

2) 我尝试将 xml 文件作为参数传递给处理器。例如:

xml=loadXMLDoc("structure.xml);
xsl=loadXMLDoc("xsl/head.xsl");
structurexml =loadXMLDoc("/extranet/structure.xml");
xsltProcessor =new XSLTProcessor();
xsltProcessor.importStylesheet(xsl);
xsltProcessor.setParameter(null, "structure", structurexml);
process.transform();

loadXMLDoc 是一段简单的代码,用于加载 xml 和 xsl 文件

function loadXMLDoc(fname) {
var isIE = (navigator.appName=="Microsoft Internet Explorer");
if (window.XMLHttpRequest) {
    xhttp = new XMLHttpRequest();
}
else {
  xhttp = new ActiveXObject('Microsoft.XMLHTTP');
}
xhttp.open('GET', fname, false);
if (xhttp.overrideMimeType){
  xhttp.overrideMimeType('text/xml');   
}
xhttp.send(null);
if (isIE) {
  xmlDoc = new ActiveXObject('Microsoft.XMLDOM');
  xmlDoc.async = false;
  xmlDoc.load(fname);
}
else {
  xmlDoc = xhttp.responseXML;
  if (!xmlDoc) {
    xmlDoc = (new DOMParser()).parseFromString(xhttp.responseText, 'text/xml');
  }
}
return xmlDoc;
}

这不起作用,因为 webkit 不允许节点集作为参数。再次,任何类型的工作都会很棒

3)我已经尝试了上述方法,但在添加为参数之前将 xml 转换为字符串。这允许我获取要传递的参数,但我无法将字符串转换回 xml 以进行处理。

如果有人对此有任何想法,那也很棒。

我想到的另一种选择是使用 JavaScript 将 xml 文件合并为一个巨大的文件并进行处理,但我不知道该怎么做。

此外,我无法进行服务器端处理,因为我无法控制服务器,所以我只剩下客户端。

正如我之前所说,我可以解决非 webkit 浏览器的问题。我一直在尝试解决这个问题一段时间,但找不到任何解决方法。我看不出我是唯一一个遇到这个问题的人,我真的希望有人遇到过类似的问题并找到解决方法或解决这个问题。

提前致谢!

马特


更新:

我还考虑过在 JavaScript 中将 xml 文件一起添加。

xml=loadXMLDoc("structure.xml);
xsl=loadXMLDoc("xsl/head.xsl");
structurexml =loadXMLDoc("/extranet/structure.xml");
newxml = xml;
newxml.appendChild(sessionxml);

但是收到 Uncaught Error: HIERARCHY_REQUEST_ERR: DOM Exception 3 的错误,我认为这是因为新 xml 中没有根节点?有谁知道我如何能够成功添加它们?这两个文件是

<?xml version="1.0" encoding="utf-8"?>
<structure>
  <product>
    <id>1</id>
    <unit>
        <id>1</id>
        <name>Blah</name>
        <code>Blah</code>
    </unit>
  </product>
</structure>

<?xml version="1.0" encoding="utf-8"?>
<user>
  <emp>true</emp>
  <first>Test</first>
  <last>User</last>
</user>

理想情况下,我希望文件格式为

<?xml version="1.0" encoding="utf-8"?>
<root>
  <structure>
    <product>
      <id>1</id>
      <unit>
          <id>1</id>
          <name>Blah</name>
          <code>Blah</code>
      </unit>
    </product>
  </structure>
  <user>
    <emp>true</emp>
    <first>Test</first>
    <last>User</last>
  </user>
</root>

有没有一种简单的方法可以在 JavaScript 或 JQuery 中实现这一点?

4

1 回答 1

0

Saxon-CE是一个 XSLT 2.0 处理器(编译为 JavaScript),将在 webkit 和所有主要浏览器中运行,并且(自然)支持 document() 函数。除了原子值,它还支持将文档、节点、数组甚至 JavaScript 对象作为参数传递。

您可以使用与您自己的代码非常相似的代码运行转换,但使用:

 xsltProcessor = Saxon.createXSLT20Processor();

但是也可以使用以下 JavaScript

Saxon.run({
   stylesheet: "xsl/head.xsl",
   source:     "structure.xml",
   parameters: {
                    structure: Saxon.requestXML("/extranet/structure.xml");
               }

});

'structure' 参数仅用于说明目的,在您的情况下,您只需要在 XSLT 中正常使用 document() 函数。

我还应该提到,使用 Saxon-CE 可以使用标准 XSLT 2.0 xsl:result-document 指令对 HTML 文档进行更新。因此,如果您想通过添加一个段落来更新 id 为“head”的 DIV 元素,您可以使用:

<xsl:result-document href="#head" method="append-content">
   <p>Book Title: <xsl:value-of select="$title"/></p>
</xsl:result-document>
于 2012-06-21T07:26:22.087 回答