xml - 保留 XSL 样式表中的空格

标签 xml xslt whitespace xalan

我正在尝试转换此 XML :-

<list>
  <unit>
    <data1>a</data1>
    <data2>b</data2>
    <data3>c</data3>
  </unit>
</list>

对此:-

<list>
  <unit>
    <category1>
      <data1>a</data1>
      <data2>b</data2>
    </category1>
    <category2>
      <data3>c</data3>
    </category2>
  </unit>
</list>

使用 XSL。我正在使用以下 XSL:-

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


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

<xsl:template match="//s:unit" xml:space="preserve">
  <xsl:copy>
  <category1>
    <xsl:apply-templates select="./s:data1"/>
    <xsl:apply-templates select="./s:data2"/>
  </category1>
  <category2>
    <xsl:apply-templates select="./s:data3"/>
  </category2>
  </xsl:copy>
</xsl:template>

</xsl:stylesheet>

现在,这保留了内部的缩进,但完全搞乱了它。列表。这就是我得到的:-

  <list>
<unit>
  <category1>
    <data1>a</data1>
    <data2>b</data2>
  </category1>
  <category2>
    <data3>c</data3>
  </category2>
</unit>
  </list>

我在这里缺少什么?

最佳答案

What am I missing here?

我认为人们不应该弄乱 XSLT 处理器的默认缩进。

最常见的是 <xsl:output indent="yes"/> 的组合和<xsl:strip-space elements="*"/>足以获得良好的缩进。

这种转变:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>

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

 <xsl:template match="unit">
  <unit>
      <category1>
       <xsl:apply-templates select="*[not(position() >2)]"/>
      </category1>
      <category2>
       <xsl:apply-templates select="*[position() >2]"/>
      </category2>
  </unit>
 </xsl:template>
</xsl:stylesheet>

应用于提供的 XML 文档时:

<list>
      <unit>
        <data1>a</data1>
        <data2>b</data2>
        <data3>c</data3>
      </unit>
</list>

产生想要的、良好缩进的结果:

<list>
  <unit>
    <category1>
      <data1>a</data1>
      <data2>b</data2>
    </category1>
    <category2>
      <data3>c</data3>
    </category2>
  </unit>
</list>

当使用以下七个 XSLT 处理器中的任何一个运行转换时,都会产生相同的结果:

  • AltovaXML (XML-SPY)。

  • .NET XslCompiledTransform。

  • .NET XslTransform。

  • 撒克逊 6.5.4。

  • Saxon 9.1.05(XSLT 2.0 处理器)。

  • XQSharp/XMLPrime(XSLT 2.0 处理器)。

  • AltovaXml(适用于 XSLT 2.0)。

MSXML3/4/6 的情况更为复杂——这些 XSLT 处理器的缩进仅包含换行符,因此每个元素都位于新行上,但出现在行的开头。

对于这些 XSLT 处理器,我使用以下两遍处理,第一遍是上述转换,第二遍适用于第一遍的结果 one of the XML pretty-printers 由 Nikolai Grigoriev 提出,可在 XSLT FAQ 中找到。 网站由 Dave Pawson 维护:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:ext="urn:schemas-microsoft-com:xslt"
 exclude-result-prefixes="ext">
 <xsl:output method="xml"/>
 <xsl:strip-space elements="*"/>

 <xsl:param name="indent-increment" select="'   '" />

 <xsl:variable name="vrtfPass1">
  <xsl:apply-templates select="/*"/>
 </xsl:variable>

 <xsl:variable name="vPass1" select="ext:node-set($vrtfPass1)"/>

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

 <xsl:template match="/">
  <xsl:apply-templates select="$vPass1/*" mode="pass2"/>
 </xsl:template>


 <xsl:template match="unit">
  <unit>
      <category1>
       <xsl:apply-templates select="*[not(position() >2)]"/>
      </category1>
      <category2>
       <xsl:apply-templates select="*[position() >2]"/>
      </category2>
  </unit>
 </xsl:template>

  <xsl:template match="*" mode="pass2">
     <xsl:param name="indent" select="'&#xA;'"/>

     <xsl:value-of select="$indent"/>
     <xsl:copy>
       <xsl:copy-of select="@*" />
       <xsl:apply-templates mode="pass2">
         <xsl:with-param name="indent"
              select="concat($indent, $indent-increment)"/>
       </xsl:apply-templates>
       <xsl:value-of select="$indent"/>
     </xsl:copy>
  </xsl:template>

  <xsl:template match="comment()|processing-instruction()" mode="pass2">
     <xsl:copy />
  </xsl:template>

  <!-- WARNING: this is dangerous. Handle with care -->
  <xsl:template match="text()[normalize-space(.)='']" mode="pass2"/>

</xsl:stylesheet>

当对同一个(提供的)XML 文档(上面)执行此转换时,生成的结果具有所需的缩进:

<?xml version="1.0" encoding="UTF-16"?>
<list>
   <unit>
      <category1>
         <data1>a
         </data1>
         <data2>b
         </data2>
      </category1>
      <category2>
         <data3>c
         </data3>
      </category2>
   </unit>
</list>

这些是我计算机上的所有 XSLT 处理器。我建议尝试最后一个转换 - 很可能它会使用 Xalan-C 产生想要的结果。

请注意:

最后一个转换使用 MSXML 特定的扩展函数 xxx:node-set(),属于 MSXML 特定的命名空间:

xmlns:ext="urn:schemas-microsoft-com:xslt"

对于 Xalan,需要替换为:

xmlns:ext="http://exslt.org/common"

或者,如果不支持 EXSLT,则使用 native Xalan 命名空间:

xmlns:ext="http://xml.apache.org/xalan

在最后一种情况下,调用 ext:node-set()函数必须替换为对 ext:nodeset() 的调用(注意缺少的破折号):

 <xsl:variable name="vPass1" select="ext:nodeset($vrtfPass1)"/>

关于xml - 保留 XSL 样式表中的空格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11350242/

相关文章:

xslt - XSLT 中的动态排序?

java - 默认的 TransformerFactory 是什么?

ruby - 我无法从 Nokogiri 解析的字符串中删除空格

html - XSLT - 将 <p> 添加到文本字符串中而不是\n

ruby-on-rails - Ruby on Rails DRY 从选择性输入表单中去除空格

python - 与 Python 中包含空格的术语一起使用的标记化?

xml - 如何使用简单的语法突出显示查看 XML 文件?

java - 在Android中设置按钮的宽度

c# - 使用 C# 获取 XML 文档的属性值

xml - 使用 xmlstarlet 连接多个 xml 元素