Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Exlude the XSLT's (1.0) line returns and extra whitespace in Text output

I’m guilty of wanting my XSLT to look pretty and hence readable. So I indent and include line breaks as such. But the transform takes these literally (which I understand why), but I don’t know if there is an option to exclude these. I’ve searched extensively. I know a couple of years ago I found a solution, but I don’t recall what it was.

This transformation is used to generate a single line Email Subject and hence I don’t want the extra white space and line returns.

How can I have my readable XSLT with my desired, single line text output?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Example to play with: xsltransform.net

XML Input

<?xml version="1.0" encoding="UTF-8"?>
<response>
    <OrderHeaderId>123456</OrderHeaderId>
    <ProjectName>Project</ProjectName>
    <RequiresApproval>true</RequiresApproval>
</response>

XSLT

<?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" />
    <xsl:template match="/response">New Job: <xsl:value-of select="OrderHeaderId"/>: 
        <xsl:if test="RequiresApproval='true'">
            Requires Approval!              
        </xsl:if>
        <xsl:value-of select="ProjectName"/> - Acknowledgement from <xsl:value-of select="SellingCompanyName"/>
    </xsl:template>
</xsl:stylesheet>

Output

New Job: 123456: 
        
            Requires Approval!              
        Project - Acknowledgement from Acme

** Desired Output**

New Job: 123456: Requires Approval! Project - Acknowledgement from Acme

>Solution :

If you want your stylesheet indented, then use xsl:text instructions to output text explicitly:

XSLT 1.0

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" >
<xsl:output method="text" />

<xsl:template match="/response">
    <xsl:text>New Job: </xsl:text>
    <xsl:value-of select="OrderHeaderId"/>
    <xsl:text>: </xsl:text>
    <xsl:if test="RequiresApproval='true'">
        <xsl:text>Requires Approval! </xsl:text>
    </xsl:if>
    <xsl:value-of select="ProjectName"/>
    <xsl:text>- Acknowledgement from </xsl:text>
    <xsl:value-of select="SellingCompanyName"/>
</xsl:template>
    
</xsl:stylesheet>
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading