none
How to remove unwanted whitespace from Xslt output

    Question

  • Hi there,
    I'm using the following xslt stylesheet in combination with XslCompiledTransform.
    I'm finding the output (also shown below) contains muliple unwanted lines of whitespace (spaces and LF characters). 
    Can anyone suggest the correct way to control this?
    Is it something I need to modify in my stylesheet (e.g. <xsl:text />), or something in the XmlWriterSettings?

    Thanks in advance,
    Peter.

    XSLT:
    Code Snippet

    <?xml version="1.0" encoding="utf-8"?>
    <xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:msxsl="urn:schemas-microsoft-com:xslt"
    xmlns:q="http://ediplc.com/campus/schemas/QualificationDefinition"
    xmlns:xaml="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:wf="http://schemas.microsoft.com/winfx/2006/xaml/workflow"
    xmlns:activity="Activities"
    exclude-result-prefixes="msxsl q"
    >
    <!-- Configure the Output settings -->
    <xsl:output method="xml" indent="yes" media-type="text/xml" encoding="utf-8" version="1.0" omit-xml-declaration="yes" />

    <!-- Define the global parameters we'll need -->
    <xsl:param name="WriteToConsole" />
    <xsl:param name="RocOperation" />
    <xsl:param name="QualCode" />

    <xsl:template match="q:Qualification">
    <base:BaseClaimWorkflow
    xmlns:base="ChildWorkflowLibrary"
    xmlns:activity="Activities"
    xmlns:core="clr-namespace:System;Assembly=mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
    xmlns:xaml="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:wf="http://schemas.microsoft.com/winfx/2006/xaml/workflow"
    xaml:Name="{$QualCode}"
    ClaimInputMessage="{{xaml:Null}}"
    ClaimOutputMessage="{{xaml:Null}}"
    QualificationStructure="{{xaml:Null}}"
    CandidateAchievement="{{xaml:Null}}"
    RocOperation="ProcessClaim"
    IsClaimTest="{{xaml:Null}}"
    >
    <xsl:if test="$WriteToConsole='True'">
    <activity:ConsoleClaimDefinition
    xaml:Name="consoleClaimDefinition1"
    Qualification="{{wf:ActivityBind {$QualCode},Path=QualificationStructure}}"
    />
    </xsl:if>
    <activity:RuleReport
    xaml:Name="ruleReport1"
    QualificationDefinition="{{wf:ActivityBind {$QualCode},Path=ClaimInputMessage}}"
    Response="{{wf:ActivityBind {$QualCode},Path=ClaimOutputMessage}}"
    >

    <xsl:apply-templates />

    </activity:RuleReport>
    </base:BaseClaimWorkflow>
    </xsl:template>
    <xsl:template match="q:RuleSet[@Combine='And']">
    <activity:CombinationAndRule
    xaml:Name="MustPassAll"
    Guid="{@Guid}"
    IsClaimTest="{{wf:ActivityBind {$QualCode},Path=IsClaimTest}}"
    >

    <xsl:apply-templates />

    </activity:CombinationAndRule>
    <xsl:text />
    </xsl:template>
    </xsl:stylesheet>


    Output xml:
    Code Snippet

    <base:BaseClaimWorkflow
    xaml:Name="CTLLS3"
    ClaimInputMessage="{xaml:Null}"
    ClaimOutputMessage="{xaml:Null}"
    QualificationStructure="{xaml:Null}"
    CandidateAchievement="{xaml:Null}"
    RocOperation="ProcessClaim"
    IsClaimTest="{xaml:Null}" xmlns:base="ChildWorkflowLibrary" xmlns:activity="Activities" xmlns:core="clr-namespace:System;Assembly=mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" xmlns:xaml="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:wf="http://schemas.microsoft.com/winfx/2006/xaml/workflow">
    <activity:ConsoleClaimDefinition
    xaml:Name="consoleClaimDefinition1"
    Qualification="{wf:ActivityBind CTLLS3,Path=QualificationStructure}" />
    <activity:RuleReport
    xaml:Name="ruleReport1"
    QualificationDefinition="{wf:ActivityBind CTLLS3,Path=ClaimInputMessage}"
    Response="{wf:ActivityBind CTLLS3,Path=ClaimOutputMessage}">

    <!-- ****** 50+ lines of whitespace, removed for brevity **** -->

    <activity:CombinationAndRule
    xaml:Name="MustPassAll"
    Guid="24072caf-8077-4c0f-8ef9-06446d175144"
    IsClaimTest="{wf:ActivityBind CTLLS3,Path=IsClaimTest}">


    <!-- etc... -->



    Thursday, July 31, 2008 10:36 AM

Answers

  • It is hard to diagnose the problem without seeing the input XML document but it is possible that the white space is copied over from the input to the output as you often do xsl: apply-templates where you process all child nodes, including text nodes, and as there is a built-in template that copies text nodes.

    To fix that, try

    Code Snippet

    <xsl:strip-space elements="*"/>

     

    Or add a template

    Code Snippet

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

     

    to avoid copying text nodes.

    Thursday, July 31, 2008 12:19 PM

All replies

  • It is hard to diagnose the problem without seeing the input XML document but it is possible that the white space is copied over from the input to the output as you often do xsl: apply-templates where you process all child nodes, including text nodes, and as there is a built-in template that copies text nodes.

    To fix that, try

    Code Snippet

    <xsl:strip-space elements="*"/>

     

    Or add a template

    Code Snippet

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

     

    to avoid copying text nodes.

    Thursday, July 31, 2008 12:19 PM
  • strip-space...

    Code Snippet

    <xsl:strip-space elements="*"/>


    ...did the job perfectly - many thanks!

    Thursday, July 31, 2008 12:50 PM