XSL 文件的 javascript XML 处理不起作用

标签 javascript xml xslt

在 javascript 中,尝试转换动态创建的 XML 数据岛,使用 XSL 文件对其进行排序,但结果是排序后的数据全部在一行上,没有 XML 格式或正确的缩进。看起来根本没有被使用。我需要在生成的 TransformNode() 中生成 XML 标签和缩进。

JavaScript 代码:

var sourceXML = document.getElementById(XMLViewID); //textArea containing XML
var xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
xmlDoc.async = false;
xmlDoc.loadXML(sourceXML.value);

var xslDoc = new ActiveXObject("Microsoft.XMLDOM");
xslDoc.async=false;
xslDoc.load("xsl.xsl");

// This should be the sorted, formatted XML data, in tree and indented format?
var sorted = xmlDoc.transformNode(xslDoc);

XML 数据:

    <table>
    <row>
        <A>j</A>
        <B>0</B>
    </row>
    <row>
        <A>c</A>
        <B>4</B>
    </row>
    <row>
        <A>f</A>
        <B>6</B>
    </row>
</table>

xsl.xsl:

<xsl:output method="xml" indent="yes" omit-xml-declaration="no"/>

<xsl:template match="/">
    <xsl:apply-templates select="table/row">
        <xsl:sort select="A" order="ascending"/>
    </xsl:apply-templates>
</xsl:template>

<xsl:template match="row">
    <xsl:value-of select="A"/>
    <xsl:value-of select="B"/>
</xsl:template>

我假设使用“indent=yes”和“omit-xml-declaration=no”,结果转换应该带有缩进和格式:

 <?xml version="1.0" encoding="UTF-16"?>
     <table>
       <row>
         <tr>
           <A>j</A>
           <B>0</B>
         </tr>
         <tr>
           <A>c</A>
           <B>4</B>
         </tr>
         <tr>
           <A>f</A>
           <B>6</B>
         </tr>
       </row>
     </table>

但它是:c4f6j0 在一行中,没有格式,没有 XML 标签...

最佳答案

到目前为止,您的 XSLT 只生成文本节点,如果您想要带有元素的 XML,那么您需要使用类似的代码来创建它们

<xsl:template match="table">
  <xsl:copy>
    <xsl:apply-templates select="row">
      <xsl:sort select="A"/>
    </xsl:apply-templates>
  </xsl:copy>
</xsl:template>

<xsl:template match="@* | node()">
  <xsl:copy>
    <xsl:apply-templates select="@* | node()"/>
  </xsl:copy>
</xsl:template>

关于XSL 文件的 javascript XML 处理不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18481657/

相关文章:

java - 向网站访问者显示一次性欢迎信息的最佳方式是什么?

javascript - knockout : Update item in an observableArray

xml - 解析器错误 : String not started expecting ' or " in php

iphone - gzipInflate/gzipDeflate 错误

c# - 如何在中继器控件内的 JavaScript 函数上传递隐藏字段值

javascript - 加载 facebook 评论插件异步

xml - 有没有办法根据文件名中的字符串使用 VBA 和 XML 将按钮添加到 Office 2010 功能区?

mysql - 如何使用 XSL 模板清理 Mysql 查询?

xml - 如何输出xls中的所有xml?

sorting - XSLT 数字节点不排序