Take the 2-minute tour ×

I am struggling with understanding an XSLT transformation. Currently I receive a serialized object in XML format, then apply XSLT and send new XML back the program. But now I need to eliminate XSLT step and to do the transformation internally in the program. The problem is I've seen XSLT sheet for the second time. Transformation sheet look really simmple but I still cannot undersnad what is going on there.

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:t="http://tempuri.org/">
  <xsl:strip-space elements="*"/>
  <xsl:template name="CopyEverything" match="node()|@*">
  <xsl:template match="/t:Data/Flagged">
    <xsl:element name="Flagged">
      <xsl:apply-templates select="/t:Data/Covers/node()|@*"/>
  <xsl:template match="/t:Data/FlaggedDetails">
    <xsl:element name="FlaggedDetails">
      <xsl:apply-templates select="/t:Data/TotalFlaggedDetails/node()|@*"/>
  <xsl:template match="/t:Data/System/RArray">
      <xsl:for-each select="/t:Data/System/RArray/Elem">
        <xsl:call-template name="CopyEverything"/>
      <xsl:for-each select="/t:Data/Elem/Elem">
        <xsl:variable name="currentCode" select="Code" />
        <xsl:variable name="showAlways" select="ShowAlways" />
        <xsl:if test="count(/t:Data/System/RArray/Elem[Code=$currentCode])=0">
          <xsl:call-template name="CopyEverything"/>

As far as I understand something from Data/Flagged and Data/FlaggedDetails is copied to Data/System/RArray, I cannot translate this logic to C#. I have to eliminate serialization stemps, so I move objects between collections (if this is what is going on) without XSLT. Could someone help me on this?

share|improve this question
add comment

3 Answers

up vote 2 down vote accepted

I wouldn't advise "translating" an XSLT transformation into C# due to many reasons:

This isn't always easy due to the incompatibilities between the two languages:

  1. Think how to simulate templates and match patterns in C# ?

  2. How to simulate the XSLT processing model?

  3. How to simulate importing stylesheet modules and determining which are the objects with highest import precedence?

  4. How to simulate keys?

  5. You will have to implement in C# a number of useful XPath (and XSLT) standard functions -- all the string functions, such as translate(), normalize-space(), substring-before(), substring-after(), ..., etc.

  6. Instructions such as <xsl:number> and functions such as format-number().

So, if you have the time needed to invest in all this and the result is successful (which is not too-likely), the translation would be several times longer than the original and in most cases -- completely not-understandable, not extensible, not maintainable.

I also doubt that a translation would run significantly faster than the original -- it can run slower in some cases (for example, not implementing keys efficiently).

Conclusion: I strongly advise against engaging in such destructive activity.

share|improve this answer
add comment

You can do the XSTL transformation inside your C# program using the XslCompiledTransform class (reference : http://msdn.microsoft.com/en-us/library/system.xml.xsl.xslcompiledtransform.aspx).

share|improve this answer
Thanks! The thing is I have to do it wihout XSLT to eliminate serialization step. So I have to do the same XSLT does, i.e. moving object from one place to another in C#, but I don't understand what is going on. –  Ypsilon IV Sep 20 '12 at 8:57
add comment

Try to use Xsl To .NET Code Generator; it is an add-in to Microsoft Visual Studio 2010, it helps you generate C# code from an XSL template.

share|improve this answer
add comment

Your Answer


