XSLT中转换后的文件中产生了额外的换行符

时间:2019-02-22 02:52:15

标签: xslt line-breaks

我使用XSLT将文档从XML转换为文本。但是在每次执行后都会产生额外的行空间,请提出如何解决此问题的建议。在这里,我附上了屏幕截图供您参考。

示例

输入:

<?xml version="1.0" encoding="UTF-8"?>
<book-part book-part-type="chapter" id="chapter1">
<book-part-meta>
<title-group>
<label>1</label>
<title>The Developmental Origins of Health and Disease&#x2014;Where Did It All Begin&#x003F;</title>
</title-group>
<contrib-group>
<contrib contrib-type="author"><name><surname>Nicholas</surname><given-names>L. M.</given-names></name></contrib>
<contrib contrib-type="author"><name><surname>Ozanne</surname><given-names>S. E.</given-names></name></contrib>
</contrib-group>
</book-part-meta>
<body>
<sec id="sec1_1">
<label>1.1</label>
<title>THE DEVELOPMENTAL ORIGINS OF ADULT DISEASE&#x2014;ORIGINS OF THE HYPOTHESIS</title>
<p>One of the earliest proposals establishing the association between early life events and the risk for disease in adult life was more than 80 years ago by Kermack and colleagues. 
<fig id="fig1_1">
<label>FIGURE 1.1</label>
<caption><p>Exposure to suboptimal nutrition during fetal development results in an adaptive response to optimize the growth of key body organs to the detriment of others. </p></caption>
<graphic href="001x001"/>
</fig>
</p>
</sec>
</body>
</book-part>

XSL:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format" xmlns:xlink="http://www.w3.org/1999/xlink">
<xsl:output method="text" omit-xml-declaration="yes" standalone="yes" indent="no"/>
[enter image description here][1]
<xsl:template match="fig/label"/>

<xsl:template match="fig/caption">
<xsl:text disable-output-escaping="yes">\caption{</xsl:text><xsl:apply-templates/><xsl:text disable-output-escaping="yes">}</xsl:text>
</xsl:template>

<xsl:template match="fig/graphic">
<xsl:text disable-output-escaping="yes">\includegraphics{</xsl:text><xsl:apply-templates select="@href"/><xsl:text disable-output-escaping="yes">.pdf}</xsl:text>
</xsl:template>



</xsl:stylesheet>

1 个答案:

答案 0 :(得分:0)

您可以使用<xsl:strip-space elements="*" />删除多余的换行符。

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format"
xmlns:xlink="http://www.w3.org/1999/xlink">

<xsl:output method="text" omit-xml-declaration="yes" standalone="yes" indent="no" />
<xsl:strip-space elements="*" />

<xsl:template match="fig/label" />

<xsl:template match="fig/caption">
    <xsl:text disable-output-escaping="yes">\caption{</xsl:text>
    <xsl:apply-templates />
    <xsl:text disable-output-escaping="yes">}</xsl:text>
</xsl:template>

<xsl:template match="fig/graphic">
    <xsl:text disable-output-escaping="yes">\includegraphics{</xsl:text>
    <xsl:apply-templates select="@href" />
    <xsl:text disable-output-escaping="yes">.pdf}</xsl:text>
</xsl:template>
</xsl:stylesheet> 

http://xsltransform.net/gVAjbT2

<xsl:strip-space>元素用于定义应删除空白的元素。 保留空白是默认设置,因此只有在使用<xsl:strip-space>元素的情况下才需要使用该元素。