在 XSLT 的结果文档中保留源文档的缩进
Keeping the indentation of the source document in the result document in XSLT
我一直在使用 Oxygen XML 编辑器进行我的 XSL 转换(XSL v.2 样式表上的 Saxon-HE 9.8.0.8)并且源文档和结果文档的缩进是相同的:我需要的。
当我从命令行 运行 XSLT(Saxon-HE 9.8.011J,Java 版本 1.8.0_161)时,我没有得到相同的结果(结果文档将完全没有缩进)。有什么我可以做的来改变这个吗?
(<xsl:output indent="yes"/>
也会缩进我的内联元素,这不是我需要的,<xsl:strip-space elements/>
会删除一些内联元素之间必要的空格。
来源:
<chapter id="ch3">
<title>Sed quam, quaes apiducius nit peror asperch icatiat</title>
<section id="s1">
<title>Et faccae sitiaessum res re dolorer errovitam,</title>
<paragraph id="p6">
<text>lorerit ab is arum dolore quaepudit exped magnate mpelestinus volupta</text>
</paragraph>
<paragraph id="p7">
<text>lorerit ab is arum <span class="s1">dolore</span> <span class="s2">quaepudit</span> exped magnate mpelestinus volupta</text>
</paragraph>
<paragraph id="p12">
<text>lorerit ab is arum dolore quaepudit exped magnate mpelestinus volupta</text>
</paragraph>
</section>
</chapter>
XSLT
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" exclude-result-prefixes="xs" version="2.0" xmlns:xlink="http://www.w3.org/1999/xlink">
<xsl:template match="*">
<xsl:element name="{local-name()}" >
<xsl:for-each select="@*" >
<xsl:attribute name="{local-name()}">
<xsl:value-of select="."/>
</xsl:attribute>
</xsl:for-each>
<xsl:apply-templates/>
</xsl:element>
</xsl:template>
<xsl:template match="paragraph">
<xsl:element name="paragraph">
<xsl:attribute name="id">p<xsl:number count="paragraph" from="chapter" level="any"></xsl:number>
</xsl:attribute>
<xsl:apply-templates/>
</xsl:element>
</xsl:template>
</xsl:stylesheet>
命令行代码
java -cp path\saxon9he.jar net.sf.saxon.Transform -t -s:path\source.xml -xsl:path\transformation.xsl -o:path\result.xml
除了源代码在特定的 DTD 中。当我删除源代码开头提到的 DTD 时,缩进保持不变,否则没有缩进。这是什么原因,我该如何更改?
谢谢!
玛丽亚
(我是初学者)
如果:
(a) 您没有从输入文档中删除 space
(b) 您的样式表将包括白色space 文本节点在内的所有文本节点复制到结果树
(c) 你不缩进序列化
那么结果的缩进应该和源一样。
如果这没有发生,那么我们需要查看您正在做的事情的详细信息。白space错的方法太多了,不看细节是不可能知道错在哪里的。
(顺便说一句,您可以在 xsl:output
上使用类似 suppress-indentation="p"
的东西来缩进段落级别以上但不在段落内。)
我一直在使用 Oxygen XML 编辑器进行我的 XSL 转换(XSL v.2 样式表上的 Saxon-HE 9.8.0.8)并且源文档和结果文档的缩进是相同的:我需要的。
当我从命令行 运行 XSLT(Saxon-HE 9.8.011J,Java 版本 1.8.0_161)时,我没有得到相同的结果(结果文档将完全没有缩进)。有什么我可以做的来改变这个吗?
(<xsl:output indent="yes"/>
也会缩进我的内联元素,这不是我需要的,<xsl:strip-space elements/>
会删除一些内联元素之间必要的空格。
来源:
<chapter id="ch3">
<title>Sed quam, quaes apiducius nit peror asperch icatiat</title>
<section id="s1">
<title>Et faccae sitiaessum res re dolorer errovitam,</title>
<paragraph id="p6">
<text>lorerit ab is arum dolore quaepudit exped magnate mpelestinus volupta</text>
</paragraph>
<paragraph id="p7">
<text>lorerit ab is arum <span class="s1">dolore</span> <span class="s2">quaepudit</span> exped magnate mpelestinus volupta</text>
</paragraph>
<paragraph id="p12">
<text>lorerit ab is arum dolore quaepudit exped magnate mpelestinus volupta</text>
</paragraph>
</section>
</chapter>
XSLT
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" exclude-result-prefixes="xs" version="2.0" xmlns:xlink="http://www.w3.org/1999/xlink">
<xsl:template match="*">
<xsl:element name="{local-name()}" >
<xsl:for-each select="@*" >
<xsl:attribute name="{local-name()}">
<xsl:value-of select="."/>
</xsl:attribute>
</xsl:for-each>
<xsl:apply-templates/>
</xsl:element>
</xsl:template>
<xsl:template match="paragraph">
<xsl:element name="paragraph">
<xsl:attribute name="id">p<xsl:number count="paragraph" from="chapter" level="any"></xsl:number>
</xsl:attribute>
<xsl:apply-templates/>
</xsl:element>
</xsl:template>
</xsl:stylesheet>
命令行代码
java -cp path\saxon9he.jar net.sf.saxon.Transform -t -s:path\source.xml -xsl:path\transformation.xsl -o:path\result.xml
除了源代码在特定的 DTD 中。当我删除源代码开头提到的 DTD 时,缩进保持不变,否则没有缩进。这是什么原因,我该如何更改?
谢谢! 玛丽亚 (我是初学者)
如果:
(a) 您没有从输入文档中删除 space
(b) 您的样式表将包括白色space 文本节点在内的所有文本节点复制到结果树
(c) 你不缩进序列化
那么结果的缩进应该和源一样。
如果这没有发生,那么我们需要查看您正在做的事情的详细信息。白space错的方法太多了,不看细节是不可能知道错在哪里的。
(顺便说一句,您可以在 xsl:output
上使用类似 suppress-indentation="p"
的东西来缩进段落级别以上但不在段落内。)