Link to home
Start Free TrialLog in
Avatar of Jaime Olivares
Jaime OlivaresFlag for Peru

asked on

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
Avatar of Gertone (Geert Bormans)
Gertone (Geert Bormans)
Flag of Belgium image

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
and add an explicit new line where you need one

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

Open in new window

Avatar of Jaime Olivares

ASKER

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
ASKER CERTIFIED SOLUTION
Avatar of Gertone (Geert Bormans)
Gertone (Geert Bormans)
Flag of Belgium image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
<?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

full stylesheet corrected, however you might want to add some newlines here and there
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
Now I have no newlines, everything is messed. I just wanted to remove white spaces, not new lines
Although a definitive solution was not provided, I took several suggestions to produce the correct .MD file
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)
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.
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