【问题标题】:Preserve whitespace between attributes in XSLT transformation在 XSLT 转换中保留属性之间的空格
【发布时间】:2012-02-09 20:15:43
【问题描述】:

我有以下 XML:

<?xml version="1.0"?>
<products>
    <product at1="a"
             at2="b"
             at3="c">
    </product>
</products>

以及以下 XSLT:

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

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

</xsl:stylesheet>

理论上,xslt 应该保持输入 xml 不变。但是,我处理后得到的输出是:

<?xml version="1.0"?>
<products>
    <product at1="a" at2="b" at3="c">
    </product>
</products>

有什么办法可以防止转换器重新格式化属性之间的间距。我知道输入和输出 xml 在功能上是等效的,但我想保留每行格式的属性以供人类阅读。如果重要的话,我正在使用 ubuntu 的 xsltproc 来做这个转换:

xsltproc -o test2.xml test.xslt test.xml

【问题讨论】:

标签: xml xslt


【解决方案1】:

不,不使用标准 XML/XSLT 工具。

该信息不是 XML 信息集的一部分,并且在 XML 解析器读取 XML 时会丢失。因此,无法在输出中保留。

您需要使用其他内容修改输出以应用这种格式。

【讨论】:

  • 在我开始编写自己的 xml 解析器之前,您能推荐任何可以在保留所有空白的同时读取 xml 的工具或库吗?
  • @AlexSpurling 我也有类似的问题。但是,为什么不只执行一个脚本(例如 python)在使用 XSLT 的第一次传递之后按照您想要的方式格式化缩进呢?它可能比重写整个 XML Parser 快得多:) 希望有所帮助!
猜你喜欢
  • 1970-01-01
  • 2011-08-16
  • 1970-01-01
  • 2014-05-11
  • 1970-01-01
  • 2012-07-09
  • 2014-08-16
  • 1970-01-01
  • 2014-09-11
相关资源
最近更新 更多