Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8476391
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T18:09:54+00:00 2026-06-10T18:09:54+00:00

I’m trying to migrate a large app from XslTransform to compiled xsl files and

  • 0

I’m trying to migrate a large app from XslTransform to compiled xsl files and XslCompiledTransform.

The app uses the Xsl to create HTML files, and the transformation data (Xml) was passed to the Xsl with a XmlDataDocument, returned from the database.

I’ve change all that so now I do (at least temporarily):

C#

 public string ProcessCompiledXsl(XmlDataDocument xml)
 {
       StringBuilder stringControl = new StringBuilder();
       XslCompiledTransform xslTran = new XslCompiledTransform();

       xslTran.Load(
           System.Reflection.Assembly.Load("CompiledXsl").GetType(dllName)
       );

       xslTran.Transform(xml, this.Arguments, XmlWriter.Create(stringControl, othersettings), null);

       return stringControl.ToString();
 }

XSL (just an example)

...
  <xsl:output method="html" indent="yes"/>
  <xsl:template match="/">
       <xsl:for-each select="//Object/Table">
              <a href="#">
                     some text
              </a>
       </xsl:for-each>
  </xsl:template>

Problem

That works, but the xsl is stripping the whitespaces between the tags outputting:

<a href="#">
   some text
</a><a href="#">
   some text
</a><a href="#">
   some text
</a><a...etc

I’ve tried:

  • Using xml:space="preserve" but I couldn’t get it to work
  • Overriding the OutputSettings, but I didn’t get any good results (maybe I missed something)
  • Using an xsl:output method="xml", and that works, but creates self closing tags and a lot of other problems

So I don’t know what to do. Maybe I’m not doing something right.Any help it’s really appreciated.

Thanks!

EDIT

Just for future references, if you want to tackle this problem leaving every XSL intact, one could try this C# class I wrote, named CustomHtmlWriter.

Basically what I did is extend from XmlTextWriter and modify the methods that write the start and the end of every tag.

In this particular case, you would use it like this:

    StringBuilder sb = new StringBuilder();
    CustomHtmlWriter writer = new CustomHtmlWriter(sb);

    xslTran.Transform(nodeReader, this.Arguments, writer);

    return sb.ToString();

Hope it helps someone.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T18:09:56+00:00Added an answer on June 10, 2026 at 6:09 pm

    I. Solution 1:

    Let me first analyze the problem here:

    Given this source XML document (invented, as you haven’t provided any):

    <Object>
     <Table>
    
     </Table>
    
     <Table>
    
     </Table>
    
     <Table>
    
     </Table>
    
     <Table>
    
     </Table>
    </Object>
    

    This transformation:

    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
     <xsl:output method="html" indent="yes"/>
    
      <xsl:template match="/">
           <xsl:for-each select="//Object/Table">
                  <a href="#">
                         some text
                  </a>
           </xsl:for-each>
      </xsl:template>
    <!--
     <xsl:template match="Table">
       <a href="#">
        Table here
       </a>
     </xsl:template>
     -->
    </xsl:stylesheet>
    

    exactly reproduces the problem — the result is:

    <a href="#">
                         some text
                  </a><a href="#">
                         some text
                  </a><a href="#">
                         some text
                  </a><a href="#">
                         some text
                  </a>
    

    Now, just uncomment the commented template and comment out the first template:

    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
     <xsl:output method="html" indent="yes"/>
    <!--
      <xsl:template match="/">
           <xsl:for-each select="//Object/Table">
                  <a href="#">
                         some text
                  </a>
           </xsl:for-each>
      </xsl:template>
     -->
     <xsl:template match="Table">
       <a href="#">
        Table here
       </a>
     </xsl:template>
    </xsl:stylesheet>
    

    The result has the wanted indentation:

     <a href="#">
        Table here
       </a>
    
     <a href="#">
        Table here
       </a>
    
     <a href="#">
        Table here
       </a>
    
     <a href="#">
        Table here
       </a>
    

    And this was solution 1


    II. Solution 2:

    This solution may reduce to minimum the required modifications to your existing XSLT code:

    This is a two-pass transformation:

    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
     xmlns:ext="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="ext">
     <xsl:output method="html"/>
    
      <xsl:template match="/">
        <xsl:variable name="vrtfPass1">
           <xsl:for-each select="//Object/Table">
                  <a href="#">
                         some text
                  </a>
           </xsl:for-each>
        </xsl:variable>
    
        <xsl:apply-templates select=
            "ext:node-set($vrtfPass1)" mode="pass2"/>
      </xsl:template>
    
     <xsl:template match="node()|@*" mode="pass2">
      <xsl:copy>
       <xsl:apply-templates select="node()|@*" mode="pass2"/>
      </xsl:copy>
     </xsl:template>
    
      <xsl:template mode="pass2" match="*[preceding-sibling::node()[1][self::*]]">
       <xsl:text>&#xA;</xsl:text>
       <xsl:copy-of select="."/>
      </xsl:template>
    </xsl:stylesheet>
    

    The idea is that we don’t even touch the existing code, but capture its output and using a few lines of additional code only, we format the output to have the wanted, final appearance.

    When this transformation is applied on the same XML document, the same, wanted result is produced:

    <a href="#">
                         some text
                  </a>
    <a href="#">
                         some text
                  </a>
    <a href="#">
                         some text
                  </a>
    <a href="#">
                         some text
                  </a>
    

    Finally, here is a demonstration how this minor change can be introduced, without touching at all any existing XSLT code:

    Let’s have this existing code in c:\temp\delete\existing.xsl:

    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
     <xsl:output method="html"/>
    
      <xsl:template match="/">
        <xsl:for-each select="//Object/Table">
          <a href="#">
            some text
          </a>
        </xsl:for-each>
      </xsl:template>
    </xsl:stylesheet>
    

    If we run this we get the problematic output.

    Now, instead of running existing.xsl, we run this transformation:

    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
     xmlns:ext="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="ext">
     <xsl:import href="file:///c:/temp/delete/existing.xsl"/>
     <xsl:output method="html"/>
    
    
      <xsl:template match="/">
        <xsl:variable name="vrtfPass1">
           <xsl:apply-imports/>
        </xsl:variable>
    
        <xsl:apply-templates select=
            "ext:node-set($vrtfPass1)" mode="pass2"/>
      </xsl:template>
    
     <xsl:template match="node()|@*" mode="pass2">
      <xsl:copy>
       <xsl:apply-templates select="node()|@*" mode="pass2"/>
      </xsl:copy>
     </xsl:template>
    
      <xsl:template mode="pass2" match="*[preceding-sibling::node()[1][self::*]]">
       <xsl:text>&#xA;</xsl:text>
       <xsl:copy-of select="."/>
      </xsl:template>
    </xsl:stylesheet>
    

    The result is the wanted one and the existing code is untouched at all:

    <a href="#">
            some text
          </a>
    <a href="#">
            some text
          </a>
    <a href="#">
            some text
          </a>
    <a href="#">
            some text
          </a>
    

    Explanation:

    1. We import any existing code that is at the top level of the import-precedence hierarchy (not imported by other stylesheets), using xsl:import.

    2. We capture the output of the existing transformation in a variable. It has the infamous RTF (Result Tree Fragment) that needs to be converted to regular tree to be processed further.

    3. The key moment is performing xsl:apply-imports when capturing the output of the transformation. This ensures that any template from the existing code (even one that we override — such as the template matching /) will be selected for execution as in the case when the existing transformation is performed by itself).

    4. We convert the RTF into a regular tree using the msxsl:node-set() extension function (XslCompiledTransform also supports the EXSLT node-set() extension function).

    5. We perform our cosmetic adjustments on the so produced regular tree.

    Do Note:

    This represents a general algorithm for post-processing existing transformations without touching the existing code.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm trying to create an if statement in PHP that prevents a single post
I have thousands of HTML files to process using Groovy/Java and I need to
I have a bunch of posts stored in text files formatted in yaml/textile (from
I am trying to understand how to use SyndicationItem to display feed which is
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.