XSLT 缩进不适用于 MSXML [英] XSLT indent not working with MSXML

查看:27
本文介绍了XSLT 缩进不适用于 MSXML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试漂亮"一个 XML 文件.正如其他一些 SO 问题中所建议的,我正在使用以下样式表进行转换:

I am trying to "pretty" an XML file. As suggested in some other SO questions, I am using the following stylesheet to transform:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output indent="yes" method="xml" encoding="UTF-16" />
<xsl:strip-space elements="*"/>
<xsl:template match="node() | @*">
    <xsl:copy>
      <xsl:apply-templates select="node() | @*" />
    </xsl:copy>
  </xsl:template>
</xsl:stylesheet>

然而,这并没有产生预期的结果.对于输入文件:

However this is not producing the desired results. For an input file of:

 <A><B><C /></B></A>

生成的输出为:

<?xml version="1.0" encoding="UTF-16"?>
<A>
<B>
<C>
</C>
</B>
</A>

但我期望的输出是(标题行无关紧要):

But the output I am expecting is (header line doesn't matter):

<A>
    <B>
        <C />
    </B>
</A>

所以有两个问题:

  • 输出中没有缩进
  • <C/> 标签已被解包",这是我不想要的.
  • There is no indentation in the output
  • The <C /> tag has been "unpacked", which I don't want.

我已经尝试过 MSXSL.exe ,并通过使用(通过 C++)IXMLDOMDocument2::transformNode 输出到 BSTR,两种方法产生相同的输出.

I have tried with MSXSL.exe , and by using (via C++) IXMLDOMDocument2::transformNode outputting to a BSTR, both methods produce identical output.

这里出了什么问题?

推荐答案

以下 WSH(Windows Scripting Host)JScript 程序使用 MSXML 6.0(默认情况下可在所有受支持的 Microsoft 操作系统上使用,无需任何安装)输出

The following WSH (Windows Scripting Host) JScript program using MSXML 6.0 (which is available on all supported Microsoft OS by default, without any installation) outputs

<?xml version="1.0" encoding="UTF-16"?>
<A>
        <B>
                <C></C>
        </B>
</A>

程序是

var msxmlVersion = '6.0';
var xml = new ActiveXObject('Msxml2.DOMDocument.' + msxmlVersion);
xml.async = false;
xml.load('test2015012501.xml');

var xsl = new ActiveXObject('Msxml2.DOMDocument.' + msxmlVersion);
xsl.async = false;
xsl.load('test2015012501.xsl');

var resultDoc = new ActiveXObject('Msxml2.DOMDocument.' + msxmlVersion);

xml.transformNodeToObject(xsl, resultDoc);

WScript.Echo(resultDoc.xml);

输入和 XSLT 是您的示例.因此,使用 MSXML 6.0 和 transformNodeToObject 可以获得更好的缩进结果,尽管根据我的需要,缩进使用了太多的缩进字符.

the input and XSLT are your samples. So using MSXML 6.0 and transformNodeToObject you get better indentation results, although for my needs the indentation is using too many indent characters.

当然,您应该能够将 MSXML 6 与 C++ 结合使用,而不是使用 JScript,并获得相同的结果.

Of course instead of using JScript you should be able to use MSXML 6 with C++ and get the same results.

如果你想要一个文件而不是一个字符串,你当然可以使用 resultDoc.save('file.xml').

And if you want a file instead of a string you can of course use resultDoc.save('file.xml').

这篇关于XSLT 缩进不适用于 MSXML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆