Home > database >  Exclude XSLT's (1.0) line returns and extra whitespace in text output
Exclude XSLT's (1.0) line returns and extra whitespace in text output

Time:04-17

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?

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

CodePudding user response:

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>
  • Related