2008-12-23 57 views
3

我喜欢.NET的webcontrols和你操纵的东西,这是共识,但XML和XSL是如此之大,因为你的UI逻辑是平台&语言无关,所以有一天我将应用程序更改为PHP,Java或任何我可以重用所有的表示逻辑。另外,XSL可以在渲染之前调用.NET(或其他)方法。何时使用xml而不是HTML使用xsl?

什么时候通常使用XML/XSL?为什么不能更频繁地使用它?

回答

2

而不是HTML?

我经常使用它来代替asp.net控件,因为它提供了对2.0和V中的关注点的分离,您不会在.NET 2.0中获得开箱即用的功能。

显然有一百万与asp.net控件无关的其他用途。


编辑:实现

public class xsltmanager 
{ 
    /* constructor (singleton) which defines a file watcher for *.xsl in the path of your choice */ 

    //just a mutex for thread safety 
    private object Mutex = new object(); 

    //caching XslCompiledTransforms 
    private Dictionary<string, XslCompiledTransform> cTransforms = new Dictionary<string, XslCompiledTransform>(); 

    public XslCompiledTransform fetch(string identifier) 
    {  
     if (!this.cTransforms.ContainsKey(identifier)) 
     { 
      lock (this.Mutex) 
      { 
       if (!this.cTransforms.ContainsKey(identifier)) 
       { 
        XslCompiledTransform xslDoc = new XslCompiledTransform(); 
        xslDoc.Load(/* file path based on identifier */); 

        this.cTransforms.Add(identifier, xslDoc); 
       } 
      } 
     } 
     return this.cTransforms[identifier]; 
    } 

    /* other util xslt methods - namespace wash, doc merge, whatever */ 
} 

public class myPage : System.Web.UI.Page 
{ 
    protected void Page_Load(object sender, EventArgs e) 
    { 
     //get source data 
     XPathDocument xPathDoc = myGetXMLMethod(); 

     //transform params 
     XsltArgumentList oArgs = new XsltArgumentList(); 

     /* add params as required */ 

     //fetching and executing the transform directly to the Response here 
     xsltmanager.instance.get(@"foo\bar\baz").Transform(xPathDoc, oArgs, Response.OutputStream); 
    } 
} 
+0

你如何将它与.NET集成? – netadictos 2008-12-23 11:31:42

+0

本质上只是在代码隐藏中针对Response.OutputStream进行转换。 – annakata 2008-12-23 12:09:25

1

没有那么多在ASP.NET的东西,但在此之前(与VB6)我曾几乎完全(在服务器)使用它来将XML转换为HTML。我一直发现它非常灵活。我也将其作为代码生成器引擎在我的“protocol buffers”项目中使用:主框架生成xml,然后使用xsl转换来吐出C#。我听说有人说他们没有找到直观的xsl,但我真的很喜欢它,并且它是处理xml时的默认工具。

现在,我正在寻找很多ASP.NET MVC,它并不一定适用于xsl - 尽管在某些方面,<%=foo.Name%><xsl:value-of select="Name"/>之间没有太大的区别。

-3

有使用XML/XSL巨大的开销,也有很多缺点的草图。

  1. 您需要一个完整的XML数据集来确保XSL仅用作逻辑引擎。
  2. 其次,XSL逻辑控制较差,版本间不一致。
  3. 三,组合是一个相当繁重的过程,不适合大型网站。

如果您关心逻辑分离,请使用一些模板语言(不是XSL)。

2

第一个近似值,无论何时我需要将信息呈现为HTML,我都会使用XSLT。几乎每次我在过去七年中都偏离了这一点,我都很后悔。我在Python中使用HTML生成的简短经验是我遇到的唯一可能可以替代它的东西。