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

问题描述 投票:0回答:1

我使用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>
xslt line-breaks
1个回答
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>元素时才需要使用该元素。

© www.soinside.com 2019 - 2024. All rights reserved.