owa*_*agh 2 xml xslt whitespace xalan
我正在尝试转换此XML: -
<list>
<unit>
<data1>a</data1>
<data2>b</data2>
<data3>c</data3>
</unit>
</list>
Run Code Online (Sandbox Code Playgroud)
对此: -
<list>
<unit>
<category1>
<data1>a</data1>
<data2>b</data2>
</category1>
<category2>
<data3>c</data3>
</category2>
</unit>
</list>
Run Code Online (Sandbox Code Playgroud)
使用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>
Run Code Online (Sandbox Code Playgroud)
现在,这保留了内部的缩进,但完全混淆了列表.这就是我得到的: -
<list>
<unit>
<category1>
<data1>a</data1>
<data2>b</data2>
</category1>
<category2>
<data3>c</data3>
</category2>
</unit>
</list>
Run Code Online (Sandbox Code Playgroud)
我在这里错过了什么?
我在这里错过了什么?
我认为不应该弄乱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>
Run Code Online (Sandbox Code Playgroud)
当应用于提供的XML文档时:
<list>
<unit>
<data1>a</data1>
<data2>b</data2>
<data3>c</data3>
</unit>
</list>
Run Code Online (Sandbox Code Playgroud)
产生想要的,缩进的结果:
<list>
<unit>
<category1>
<data1>a</data1>
<data2>b</data2>
</category1>
<category2>
<data3>c</data3>
</category2>
</unit>
</list>
Run Code Online (Sandbox Code Playgroud)
使用以下七个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处理器,我使用以下两次传递处理,第一次传递是上面的转换,第二次传递适用于第一次传递的结果,由Nikolai Grigoriev提出并在XSLT FAQ站点中提供的XML漂亮打印机之一作者: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="'
'"/>
<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>
Run Code Online (Sandbox Code Playgroud)
当对同一(提供的)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>
Run Code Online (Sandbox Code Playgroud)
这些都是我在计算机上的XSLT处理器.我建议尝试最后一次转换 - 它有可能用Xalan-C产生想要的结果.
请注意:
最后一个转换使用特定于MSXML的扩展函数xxx:node-set(),属于特定于MSXML的命名空间:
xmlns:ext="urn:schemas-microsoft-com:xslt"
Run Code Online (Sandbox Code Playgroud)
对于Xalan,需要替换为:
xmlns:ext="http://exslt.org/common"
Run Code Online (Sandbox Code Playgroud)
或者,如果不支持EXSLT,则使用本机Xalan命名空间:
xmlns:ext="http://xml.apache.org/xalan
Run Code Online (Sandbox Code Playgroud)
在最后一种情况下,ext:node-set()必须用调用替换对函数的调用ext:nodeset()(注意缺少的破折号):
<xsl:variable name="vPass1" select="ext:nodeset($vrtfPass1)"/>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
447 次 |
| 最近记录: |