Removing line breaks when using XSLT to convert XML to text

Here should go questions about transforming XML with XSLT and FOP.
ra0543
Posts: 80
Joined: Wed Jan 14, 2009 12:50 pm

Removing line breaks when using XSLT to convert XML to text

Post by ra0543 »

I am using Oxygen Author and trying to establish how to control the presence or absence of line breaks where I want them in a text file generated by applying an XSLT transformation to an XML file.

The XML is as follows:

Code: Select all

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE l1 SYSTEM "Simple.dtd">
<?xml-stylesheet type="text/xsl" href="Simple2.xsl"?>
<l1><l2><l3>1a</l3><l3>1b</l3><l3>1c</l3>
</l2>
<l2>
<l3>2a</l3><l3>2b</l3><l3>2c</l3>
</l2>
<l2>
<l3>3a</l3>
<l3>3b</l3>
<l3>3c</l3>
</l2>
</l1>
I want both this and the following (which is equivalent, without the linebreaks) to produce the same result from any transformation.

Code: Select all

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE l1 SYSTEM "Simple.dtd">
<?xml-stylesheet type="text/xsl" href="Simple2.xsl"?>
<l1><l2><l3>1a</l3><l3>1b</l3><l3>1c</l3></l2><l2><l3>2a</l3><l3>2b</l3><l3>2c</l3></l2><l2><l3>3a</l3><l3>3b</l3><l3>3c</l3></l2></l1>
Can anyone help? I've tried the following stylesheet but it produces different results for the two inputs.

Code: Select all

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="text" encoding="utf-8" />
<xsl:template match="/"><xsl:apply-templates/></xsl:template><xsl:template match="l1"><xsl:apply-templates/></xsl:template><xsl:template match="l2"><xsl:apply-templates/></xsl:template><xsl:template match="l3"><xsl:value-of select="."/></xsl:template>
</xsl:stylesheet>
How can I control whether I get:

Code: Select all

1a1b1c2a2b2c3a3b3c
or

Code: Select all

1a1b1c
2a2b2c
3a3b3c
or

Code: Select all

1a
1b
1c
2a
2b
2c
3a
3b
3c
regardless of whether there are linebreaks or spaces in the input XML?
george
Site Admin
Posts: 2095
Joined: Thu Jan 09, 2003 2:58 pm

Re: Removing line breaks when using XSLT to convert XML to text

Post by george »

You get the text nodes copied to the output by the built in rules. Adding

<xsl:template match="text()"/>

will give you always the 1a1b1c2a2b2c3a3b3c result.

Best Regards,
George
George Cristian Bina
ra0543
Posts: 80
Joined: Wed Jan 14, 2009 12:50 pm

Re: Removing line breaks when using XSLT to convert XML to text

Post by ra0543 »

OK, where should I add it? And what about the other two results?

Thanks!
george wrote:You get the text nodes copied to the output by the built in rules. Adding

<xsl:template match="text()"/>

will give you always the 1a1b1c2a2b2c3a3b3c result.

Best Regards,
George
george
Site Admin
Posts: 2095
Joined: Thu Jan 09, 2003 2:58 pm

Re: Removing line breaks when using XSLT to convert XML to text

Post by george »

You need to add that to your stylesheet:

Code: Select all


<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="text" encoding="utf-8"/>
<xsl:template match="/">
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="l1">
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="l2">
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="l3">
<xsl:value-of select="."/>
</xsl:template>
<xsl:template match="text()"/>
</xsl:stylesheet>
In order to get
1a1b1c
2a2b2c
3a3b3c

you need to emit a new line at the end of processing l2 elements:

Code: Select all


<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="text" encoding="utf-8"/>
<xsl:template match="/">
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="l1">
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="l2">
<xsl:apply-templates/>
<xsl:text>&#10;</xsl:text>
</xsl:template>
<xsl:template match="l3">
<xsl:value-of select="."/>
</xsl:template>
<xsl:template match="text()"/>
</xsl:stylesheet>
and to get

1a
1b
1c
2a
2b
2c
3a
3b
3c

you need to output the new line after you process each l3 element

Code: Select all


<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="text" encoding="utf-8"/>
<xsl:template match="/">
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="l1">
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="l2">
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="l3">
<xsl:value-of select="."/>
<xsl:text>&#10;</xsl:text>
</xsl:template>
<xsl:template match="text()"/>
</xsl:stylesheet>
Best Regards,
George
George Cristian Bina
ra0543
Posts: 80
Joined: Wed Jan 14, 2009 12:50 pm

Re: Removing line breaks when using XSLT to convert XML to text

Post by ra0543 »

Thanks. It's amazing none of the online tutorials mentions this.
Post Reply