FW: Extending XSLT using C#

XSLT is a transformation language for XML. It allows server systems to transform the source XML tree into a more suitable form for clients. XSLT uses node patterns to match against templates to perform its transformations. Though it makes complex transformations relatively simple there are some situations where we might have to use some custom classes.

Some of the situations where we might need to extend XSLT are:

1) Call custom business logic
2) Perform different actions depending on Permissions
3) Perform complex formatting for dates, strings etc
4) Or even call a webservice!!


Steps to extend XSLT

1) Create the custom object to use from within XSLT(in C#)

CustomDate custDate = new CustomDate();

2) Provide a custom namespace declaration for the custom class within XSLTs namespace declaration(in XSLT file)

<xsl:transform
        version="1.0"
        xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
        xmlns:myCustDate="urn:custDate">
 
3) Pass an instance of the custom object to XSLT, with the same namespace as in last step(in C#) 
xslArgs.AddExtensionObject("urn:custDate", custDate) ;
4) Use the object from within XSLT(in XSLT file) 

<xsl:value-of select="myCustDate:GetDateDiff(./joiningdate)"/>

Sample Code

For our example let us assume we have a XSLT sheet where we need to manipulate dates. We need to show the number of days the employee has been with the company. Since XSLT has no native date manipulation functions, let us use an extension object for our task.

   1:  using System ; 
   2:  using System.IO ; 
   3:  using System.Xml ; 
   4:  using System.Xml.Xsl ; 
   5:  using System.Xml.XPath ;
   6:   
   7:  public class XsltExtension{
   8:   
   9:      public static void Main(string[] args){         
  10:          if (args.Length == 2){            
  11:              Transform(args[0], args[1]) ;            
  12:          }else{            
  13:              PrintUsage() ;            
  14:          } 
  15:      } 
  16:      
  17:      public static void Transform(string sXmlPath, string sXslPath){         
  18:          try{             
  19:              //load the Xml doc 
  20:              XPathDocument myXPathDoc = new XPathDocument(sXmlPath) ;
  21:              XslTransform myXslTrans = new XslTransform() ; 
  22:              
  23:              //load the Xsl 
  24:              myXslTrans.Load(sXslPath) ;             
  25:              XsltArgumentList xslArgs = new XsltArgumentList() ; 
  26:              
  27:              //create custom object 
  28:              CustomDate custDate = new CustomDate() ; 
  29:              
  30:              //pass an instance of the custom object 
  31:              xslArgs.AddExtensionObject("urn:custDate", custDate) ; 
  32:                          
  33:              //create the output stream 
  34:              XmlTextWriter myWriter = new XmlTextWriter("extendXSLT.html", null) ; 
  35:              
  36:              //pass the args,do the actual transform of Xml 
  37:              myXslTrans.Transform(myXPathDoc,xslArgs, myWriter) ;        
  38:   
  39:              myWriter.Close() ;
  40:   
  41:          }catch(Exception e){
  42:   
  43:              Console.WriteLine("Exception: {0}", e.ToString()); 
  44:          } 
  45:          
  46:      } 
  47:      
  48:      public static void PrintUsage(){ 
  49:          Console.WriteLine("Usage: XsltExtension.exe <xml path> >xsl path<") ; 
  50:      }
  51:   
  52:  }
  53:   
  54:  //our custom class 
  55:  public class CustomDate{ 
  56:      
  57:      //function that gets called from XSLT 
  58:      public string GetDateDiff(string xslDate){ 
  59:      
  60:          DateTime dtDOB = DateTime.ParseExact(xslDate, "dd/MM/yyyy", null);  //DateTime.Parse(xslDate);        
  61:          DateTime dtNow = DateTime.Today ;        
  62:          TimeSpan tsAge = dtNow.Subtract(dtDOB);        
  63:          return tsAge.Days.ToString() ; 
  64:      }
  65:  } 

Compile this code and use the provided members.xml and memberdisplay.xsl to run this console application. You should see a extendXSLT.html file within the same folder. Open this file and notice that our class CustomDate has been called to calculate the number of days the employee has been in the company.
Summary :
XSLT is a powerful transformation language for XML, however using extension objects in .NET and C# should ensure that we could easily accomplish what would be impossible or hard with XSLT alone.

-------------------------------------------------------------

Members.xml:

<root>
  <member>
    <name>Employee1</name>
    <joiningdate>01/01/1970</joiningdate>
    <role>CTO</role>
  </member>
  <member>
    <name>Employee2</name>
    <joiningdate>24/07/1978</joiningdate>
    <role>Web Developer</role>
  </member>
  <member>
    <name>Employee3</name>
    <joiningdate>15/12/1980</joiningdate>
    <role>Tester</role>
  </member>
</root>

MemberDisplay.xslt:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl"
xmlns:myCustDate="urn:custDate">

  <xsl:output method="html" omit-xml-declaration="yes" />

  <xsl:template match="/">
    <html>
      <head>
        <style>
          TABLE.tblMaster
          {
          border-style: solid;
          border- 1px 1px 1px 1px;
          border-style: solid;
          border-color:  #99CCCC;
          padding: 4px 6px;
          text-align: left;
          font-family:Tahoma,Arial;
          font-size:9pt;

          }
          TD.tdHeader
          {
          FONT-WEIGHT: bolder;
          FONT-FAMILY: Arial;
          BACKGROUND-COLOR: lightgrey;
          TEXT-ALIGN: center
          }
        </style>
      </head>
      <body>
        <table width="50%" class="tblMaster">
          <tr >
            <td class="tdHeader">Employee</td>
            <td class="tdHeader">Join date</td>
            <td class="tdHeader">Days in company</td>
            <td class="tdHeader">Role</td>
          </tr>
          <xsl:for-each select="/root/member">

            <tr >
              <td>
                <xsl:value-of select="./name"/>
              </td>

              <td>
                <xsl:value-of select="./joiningdate"/>
              </td>

              <td>
                <xsl:value-of select="myCustDate:GetDateDiff(./joiningdate)"/>
              </td>

              <td>
                <xsl:value-of select="./role"/>
              </td>
            </tr>

          </xsl:for-each>

        </table>
      </body>
    </html>
  </xsl:template>
</xsl:stylesheet>
原文地址:https://www.cnblogs.com/aot/p/2258840.html