Removing line breaks when using XSLT to convert XML to text
Here should go questions about transforming XML with XSLT and FOP.
-
- Posts: 80
- Joined: Wed Jan 14, 2009 12:50 pm
Removing line breaks when using XSLT to convert XML to text
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:
I want both this and the following (which is equivalent, without the linebreaks) to produce the same result from any transformation.
Can anyone help? I've tried the following stylesheet but it produces different results for the two inputs.
How can I control whether I get:
or
or
regardless of whether there are linebreaks or spaces in the input XML?
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>
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>
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>
Code: Select all
1a1b1c2a2b2c3a3b3c
Code: Select all
1a1b1c
2a2b2c
3a3b3c
Code: Select all
1a
1b
1c
2a
2b
2c
3a
3b
3c
-
- Site Admin
- Posts: 2095
- Joined: Thu Jan 09, 2003 2:58 pm
Re: Removing line breaks when using XSLT to convert XML to text
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
<xsl:template match="text()"/>
will give you always the 1a1b1c2a2b2c3a3b3c result.
Best Regards,
George
George Cristian Bina
-
- Posts: 80
- Joined: Wed Jan 14, 2009 12:50 pm
Re: Removing line breaks when using XSLT to convert XML to text
OK, where should I add it? And what about the other two results?
Thanks!
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
-
- Site Admin
- Posts: 2095
- Joined: Thu Jan 09, 2003 2:58 pm
Re: Removing line breaks when using XSLT to convert XML to text
You need to add that to your stylesheet:
In order to get
1a1b1c
2a2b2c
3a3b3c
you need to emit a new line at the end of processing l2 elements:
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
Best Regards,
George
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>
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> </xsl:text>
</xsl:template>
<xsl:template match="l3">
<xsl:value-of select="."/>
</xsl:template>
<xsl:template match="text()"/>
</xsl:stylesheet>
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> </xsl:text>
</xsl:template>
<xsl:template match="text()"/>
</xsl:stylesheet>
George
George Cristian Bina
Jump to
- Oxygen XML Editor/Author/Developer
- ↳ Feature Request
- ↳ Common Problems
- ↳ DITA (Editing and Publishing DITA Content)
- ↳ SDK-API, Frameworks - Document Types
- ↳ DocBook
- ↳ TEI
- ↳ XHTML
- ↳ Other Issues
- Oxygen XML Web Author
- ↳ Feature Request
- ↳ Common Problems
- Oxygen Content Fusion
- ↳ Feature Request
- ↳ Common Problems
- Oxygen JSON Editor
- ↳ Feature Request
- ↳ Common Problems
- Oxygen PDF Chemistry
- ↳ Feature Request
- ↳ Common Problems
- Oxygen Feedback
- ↳ Feature Request
- ↳ Common Problems
- Oxygen XML WebHelp
- ↳ Feature Request
- ↳ Common Problems
- XML
- ↳ General XML Questions
- ↳ XSLT and FOP
- ↳ XML Schemas
- ↳ XQuery
- NVDL
- ↳ General NVDL Issues
- ↳ oNVDL Related Issues
- XML Services Market
- ↳ Offer a Service