1

我正在努力理解 XSLT 转换。目前我收到一个 XML 格式的序列化对象,然后应用 XSLT 并将新的 XML 发送回程序。但现在我需要消除 XSLT 步骤并在程序内部进行转换。问题是我第二次看到 XSLT 表。转换表看起来很简单,但我仍然无法理解那里发生了什么。

<?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>

据我了解,从 Data/Flagged 和 Data/FlaggedDetails 复制到 Data/System/RArray,我无法将此逻辑转换为 C#。我必须消除序列化词干,所以我在没有 XSLT 的情况下在集合之间移动对象(如果这是正在发生的事情)。有人可以帮我吗?

4

3 回答 3

2

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.

于 2012-09-20T12:32:14.330 回答
1

您可以使用该类在您的 C# 程序中进行 XSTL 转换XslCompiledTransform(参考:http: //msdn.microsoft.com/en-us/library/system.xml.xsl.xslcompiledtransform.aspx)。

于 2012-09-20T08:52:06.903 回答
0

尝试使用Xsl To .NET 代码生成器它是Microsoft Visual Studio 2010的插件,可帮助您C#XSL模板生成代码。

于 2012-09-20T09:11:16.070 回答