Search code examples
xsltline-breaks

Extra line break produced in converted file in XSLT


I used XSLT for converting document from XML to text. But extra line space is produced after every instance, please suggest how to solve this problem. Here I attached screen shot for your reference.

Example

Input:

<?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>

Solution

  • You can use <xsl:strip-space elements="*" /> to remove extra line breaks.

    <?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

    The <xsl:strip-space> element is used to define the elements for which white space should be removed. Preserving white space is the default setting, so using the element is only necessary if the <xsl:strip-space> element is used.