Remove leading spaces in text generated from XSLT

I am researching in transforming a "xml documentation" file produced by Visual Studio into a "markdown" (.md) file, by using a xml stylesheet. I have good progress, but I have a major issue: cannot find the way to remove some leading whitespaces, as shown here:

#xmlsample  
##SomeClass
        Class level summary documentation goes here.
      
        Longer comments can be associated with a type or member
        through the remarks tag      

Open in new window

All those lines shall be left aligned, without any leading space.
I am attaching the input file, the stylesheet, the current output file, and the C# snippet used to perform the transformation.

Thanks in advance.
documentation.xsl
sample.xml
sample.txt
Program.cs
LVL 55
Jaime OlivaresSoftware ArchitectAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Geert BormansInformation ArchitectCommented:
You should avoid having free floating text added to the result tree

instead of
#<xsl:value-of select="name"/>

Open in new window


do

<xsl:text>#</xsl:text>
<xsl:value-of select="name"/>

Open in new window


in order to avoid whitespace added by the processor
0
Geert BormansInformation ArchitectCommented:
and add an explicit new line where you need one

<xsl:text>###Methods&#10;</xsl:text>

Open in new window

0
Jaime OlivaresSoftware ArchitectAuthor Commented:
I don't think that's the problem here, you can see the # are left aligned
The problem is with the 'default' content, which has an implicit template
0
Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

Geert BormansInformation ArchitectCommented:
and add a template for text() that normalizes the lot
 <xsl:template match="text()">
    <xsl:value-of select="normalize-space()"/>
  </xsl:template>
 

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Geert BormansInformation ArchitectCommented:
<?xml version="1.0" encoding="iso-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="text" omit-xml-declaration="yes" indent="no" />
  <xsl:strip-space elements="*"/>
  <!-- Assembly template -->
  <xsl:template match="assembly">
    <xsl:text>#</xsl:text>
    <xsl:value-of select="name"/>
    <xsl:apply-templates select="//member[contains(@name,'T:')]"/>
  </xsl:template>
  <!-- Type template -->
  <xsl:template match="//member[contains(@name,'T:')]">
    <xsl:variable name="FullMemberName" select="substring-after(@name, ':')"/>
    
    <xsl:variable name="MemberName">
      <xsl:choose>
        <xsl:when test="contains(@name, '.')">
          <xsl:value-of select="substring-after(@name, '.')"/>
        </xsl:when>
        <xsl:otherwise>
          <xsl:value-of select="substring-after(@name, ':')"/>
        </xsl:otherwise>
      </xsl:choose>
    </xsl:variable>  
    <xsl:text>##</xsl:text>
    <xsl:value-of select="$MemberName"/>
    <xsl:apply-templates/>
    
    <!-- Fields -->
    <xsl:if test="//member[contains(@name,concat('F:',$FullMemberName))]">
      <xsl:text>###Fields&#10;</xsl:text>
      <xsl:for-each select="//member[contains(@name,concat('F:',$FullMemberName))]">
        <xsl:text>####</xsl:text><xsl:value-of select="substring-after(@name, concat('F:',$FullMemberName,'.'))"/>
        <xsl:apply-templates/>
      </xsl:for-each>
    </xsl:if>
    
    <!-- Properties -->
    <xsl:if test="//member[contains(@name,concat('P:',$FullMemberName))]">
      <xsl:text>###Properties&#10;</xsl:text>
      <xsl:for-each select="//member[contains(@name,concat('P:',$FullMemberName))]">
        <xsl:text>####</xsl:text><xsl:value-of select="substring-after(@name, concat('P:',$FullMemberName,'.'))"/>
        <xsl:apply-templates/>
      </xsl:for-each>
    </xsl:if>
    
    <!-- Methods -->  
    <xsl:if test="//member[contains(@name,concat('M:',$FullMemberName))]">
      <xsl:text>###Methods&#10;</xsl:text>
      
      <xsl:for-each select="//member[contains(@name,concat('M:',$FullMemberName))]">
        
        <!-- If this is a constructor, display the type name (instead of "#ctor"), or display the method name -->
        <xsl:choose>
          <xsl:when test="contains(@name, '#ctor')">
            <xsl:text>####Constructor:&#10;</xsl:text>
            <xsl:value-of select="$MemberName"/>
            <xsl:value-of select="substring-after(@name, '#ctor')"/>
          </xsl:when>
          <xsl:otherwise>
            <xsl:text>####</xsl:text><xsl:value-of select="substring-after(@name, concat('M:',$FullMemberName,'.'))"/>
          </xsl:otherwise>
        </xsl:choose>
        
        <xsl:apply-templates select="summary"/>
        
        <xsl:if test="count(param)!=0">
          <xsl:text>#####Parameters&#10;</xsl:text>
          <xsl:apply-templates select="param"/>
        </xsl:if>
        
        <xsl:if test="count(returns)!=0">
          <xsl:text>#####Return Value&#10;</xsl:text>
          <xsl:apply-templates select="returns"/>
        </xsl:if>
        
        <xsl:if test="count(exception)!=0">
          <xsl:text>#####Exceptions&#10;</xsl:text>
          <xsl:apply-templates select="exception"/>
        </xsl:if>
        
        <xsl:if test="count(example)!=0">
          <xsl:text>#####Example&#10;</xsl:text>
          <xsl:apply-templates select="example"/>
        </xsl:if>
        
      </xsl:for-each>
      
    </xsl:if>
  </xsl:template>
  
  <xsl:template match="c">
    `<xsl:apply-templates />`
  </xsl:template>
  
  <xsl:template match="code">
    ```
    <xsl:apply-templates />
    ```
  </xsl:template>
  
  <xsl:template match="example">
    <xsl:text>**Example:** &#10;</xsl:text>
    <xsl:apply-templates />
  </xsl:template>
  
  <xsl:template match="exception">
    <xsl:text>**</xsl:text><xsl:value-of select="substring-after(@cref,'T:')"/><xsl:text>:**</xsl:text>
    <xsl:apply-templates />
  </xsl:template>
  
  <xsl:template match="include">
    <xsl:text>[External file]({@file})&#10;</xsl:text>
  </xsl:template>
  
  <xsl:template match="para">
    <xsl:apply-templates />
  </xsl:template>
  
  <xsl:template match="param">
    <xsl:text>**</xsl:text>
    <xsl:value-of select="@name"/>
    <xsl:text>:**</xsl:text>
    <xsl:apply-templates />
  </xsl:template>
  
  <xsl:template match="paramref">
    <xsl:text>*</xsl:text>
    <xsl:value-of select="@name" />
    <xsl:text>*</xsl:text>
  </xsl:template>
  
  <xsl:template match="permission">
    <xsl:text>**Permission:**&#10;</xsl:text>
    <xsl:text>*</xsl:text>
    <xsl:value-of select="@cref" />
    <xsl:text>*</xsl:text>
    <xsl:apply-templates />
  </xsl:template>
  
  <xsl:template match="remarks">
    <xsl:apply-templates />
  </xsl:template>
  
  <xsl:template match="returns">
    <xsl:text>**Return Value:**&#10;</xsl:text>
    <xsl:apply-templates />
  </xsl:template>
  
  <xsl:template match="see">
    <xsl:text>*See: </xsl:text>
    <xsl:value-of select="@cref" />
    <xsl:text>*</xsl:text>
  </xsl:template>
  
  <xsl:template match="seealso">
    <xsl:text>See also: </xsl:text>
    <xsl:value-of select="@cref" />
  </xsl:template>
  
  <xsl:template match="summary">
    <xsl:apply-templates />
  </xsl:template>
  
  <!-- List management -->
  <xsl:template match="list">
    <xsl:choose>
      <xsl:when test="@type='bullet'">
        <xsl:for-each select="listheader">
          <xsl:text>- **</xsl:text>
          <xsl:value-of select="term"/><xsl:text>:** </xsl:text>
          <xsl:value-of select="definition"/>
        </xsl:for-each>
        <xsl:for-each select="list">
          <xsl:text>- **</xsl:text><xsl:value-of select="term"/><xsl:text>:** </xsl:text><xsl:value-of select="definition"/>
        </xsl:for-each>
      </xsl:when>
      <xsl:when test="@type='number'">
        <xsl:for-each select="listheader">
          <xsl:value-of select="position()"/>. **<xsl:value-of select="term"/><xsl:text>:** </xsl:text><xsl:value-of select="definition"/>
        </xsl:for-each>
        <xsl:for-each select="list">
          <xsl:value-of select="position()"/>. **<xsl:value-of select="term"/><xsl:text>:** </xsl:text><xsl:value-of select="definition"/>
        </xsl:for-each>
      </xsl:when>
      <xsl:when test="@type='table'">
        <xsl:for-each select="listheader">
          <xsl:text>|</xsl:text><xsl:value-of select="term"/><xsl:text>|</xsl:text><xsl:value-of select="definition"/><xsl:text>|</xsl:text>
          <xsl:text>|----------|----------|&#10;</xsl:text>
        </xsl:for-each>
        <xsl:for-each select="list">
          <xsl:text>|**</xsl:text><xsl:value-of select="term"/><xsl:text>:**|</xsl:text><xsl:value-of select="definition"/><xsl:text>|</xsl:text>
        </xsl:for-each>
      </xsl:when>
    </xsl:choose>
  </xsl:template>
  
  <xsl:template match="text()">
    <xsl:value-of select="normalize-space()"/>
  </xsl:template>
  
</xsl:stylesheet>

Open in new window

0
Geert BormansInformation ArchitectCommented:
full stylesheet corrected, however you might want to add some newlines here and there
0
Geert BormansInformation ArchitectCommented:
I don't think that's the problem here, you can see the # are left aligned

I disagree, you don't control how for instance the microsoft parser ruins the whitespace inside the XSLT
It is a golden rule to always put text that needs to be copied directly to the output tree in an xsl:text element to avoid spurious whitespace
0
Jaime OlivaresSoftware ArchitectAuthor Commented:
Now I have no newlines, everything is messed. I just wanted to remove white spaces, not new lines
0
Jaime OlivaresSoftware ArchitectAuthor Commented:
Although a definitive solution was not provided, I took several suggestions to produce the correct .MD file
0
Geert BormansInformation ArchitectCommented:
Hi Jaime, you need to know a couple of things on Experts Exchange policy
Although a definitive solution was not provided, I took several suggestions to produce the correct
Very strange, Experts Exchange is not meant to provide you every line of code you could possibly need. Experts Exchange is meant to help you help yourself. That is what I did and did well. This is exactly what you should expect.
I think you largely underestimate the quality of the different suggestions you have been given. Please take note of them in you future XSLT challenges
(I would have expected you would have some more appreciation for the code editing on each floating text node, I did middle of the night)
0
Jaime OlivaresSoftware ArchitectAuthor Commented:
Geert, I am very aware of EE purpose, I have been here for 15 years, and have provided thousands of answers by myself.
I asked for a specific issue, and your multiple responses didn't provide a clear answer but many ideas on how to solve it. None of them is the right answer.
A big bunch of suggestions is not an answer, some of them totally messed up my almost-working file.
Anyway, thanks for the insights.
0
Geert BormansInformation ArchitectCommented:
Hi Jaime, not going to quarrel over this, I considered the sequence of comments an interesting learning process.

Anyway, reading the thread all over this morning it strikes me that
I seem to miss my final reply yesterday night
It is strange, this was tested to work and posted and seen on the forum.
Missing this one, I start to understand the B :-)

For what it is worth, if you replace the text() template I added before with the one below,
 you leave the newlines intact, you might need to remove the "&#10;" I added all over

<xsl:template match="text()">
    <xsl:value-of select="translate(., ' ', '')"/>
  </xsl:template>

Open in new window


I really don't understand why this comment is now missing from the forum, I did not delete it
Anyhow, for what it is worth
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
XML

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.