Are .NET 3.5 XPath classes and methods XSLT 2.0 compatible?

后端 未结 6 1817
萌比男神i
萌比男神i 2021-01-06 15:06

I\'d like to use regular expressions in selecting elements using the match function. I\'d prefer not to use an external library (such as saxon) to do this.

6条回答
  •  鱼传尺愫
    2021-01-06 15:55

    For future reference, here's a nice page on extending xpath/xquery in .net:

    http://www.csharpfriends.com/Articles/getArticle.aspx?articleID=64

    I don't trust this to last, so I copy it here:


    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)

    
    

    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)

    
    

    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.

    using System ;
    using System.IO ;
    using System.Xml ;
    using System.Xml.Xsl ;
    using System.Xml.XPath ;
    
    public class XsltExtension{
    
        public static void Main(string[] args){
    
            if (args.Length == 2){
    
                Transform(args[0], args[1]) ;
    
            }else{
    
                PrintUsage() ;
    
            }
        }
    
        public static void Transform(string sXmlPath, string sXslPath){
    
            try{
    
                //load the Xml doc
                XPathDocument myXPathDoc = new XPathDocument(sXmlPath) ;
    
                XslTransform myXslTrans = new XslTransform() ;
    
                //load the Xsl 
                myXslTrans.Load(sXslPath) ;
    
                XsltArgumentList xslArgs = new XsltArgumentList() ;
    
                //create custom object
                CustomDate custDate = new CustomDate() ;
    
                //pass an instance of the custom object
                xslArgs.AddExtensionObject("urn:custDate", custDate) ;
    
                //create the output stream
                XmlTextWriter myWriter = new XmlTextWriter("extendXSLT.html", null) ;
    
                //pass the args,do the actual transform of Xml
                myXslTrans.Transform(myXPathDoc,xslArgs, myWriter) ;        
    
                myWriter.Close() ;
    
            }catch(Exception e){
    
                Console.WriteLine("Exception: {0}", e.ToString());
            }
    
        }
    
        public static void PrintUsage(){
            Console.WriteLine("Usage: XsltExtension.exe  >xsl path<") ;
        }
    
    }
    
    //our custom class
    public class CustomDate{
    
        //function that gets called from XSLT
        public string GetDateDiff(string xslDate){
    
            DateTime dtDOB = DateTime.Parse(xslDate) ;
    
            DateTime dtNow = DateTime.Today ;
    
            TimeSpan tsAge = dtNow.Subtract(dtDOB) ;
    
            return tsAge.Days.ToString() ;
        }
    
    }
    

    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 powerfull 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:

     
        
            Employee1
            01/01/1970
            CTO
        
        
            Employee2
            24/07/1978
            Web Developer
        
        
            Employee3
            15/12/1980
            Tester
        
    
    

    Memberdisplay.xsl:

    
    
         
    
        
            
                
                    
                
                
                    
    
                            
    Employee Join date Days in company Role

提交回复
热议问题