Search code examples
c#xsltxml-serialization

Translate XSLT transformation to C#


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.

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:t="http://tempuri.org/">
  <xsl:output omit-xml-declaration="yes" indent="yes"/>
  <xsl:strip-space elements="*"/>
  <xsl:template name="CopyEverything" match="node()|@*">
    <xsl:copy>
      <xsl:apply-templates select="node()|@*"/>
    </xsl:copy>
  </xsl:template>
  <xsl:template match="/t:Data/Flagged">
    <xsl:element name="Flagged">
      <xsl:apply-templates select="/t:Data/Covers/node()|@*"/>
    </xsl:element>
  </xsl:template>
  <xsl:template match="/t:Data/FlaggedDetails">
    <xsl:element name="FlaggedDetails">
      <xsl:apply-templates select="/t:Data/TotalFlaggedDetails/node()|@*"/>
    </xsl:element>
  </xsl:template>
  <xsl:template match="/t:Data/System/RArray">
    <xsl:element name="{local-name()}">
      <xsl:for-each select="/t:Data/System/RArray/Elem">
        <xsl:call-template name="CopyEverything"/>
      </xsl:for-each>
      <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"/>
        </xsl:if>
      </xsl:for-each>
    </xsl:element>
  </xsl:template>
</xsl:stylesheet>

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?


Solution

  • 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.