Question

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.

Was it helpful?

Solution

There are some things in XSLT 2.0 that aren't supported in the built in libraries (there was discussion on the mono mailing list about this but I can't find the information anymore). But most people never run into the corner cases that aren't supported.

Another option is to check out the open source http://saxon.sourceforge.net/ which has great support for 2.0.

EDIT (AB): the above accepted answer may be confusing. There's no support at all and there are no plans in that direction for any of the XPath 2.0 or XSLT 2.0 functions in .NET.

OTHER TIPS

I believe the answer in this discussion is misleading. I think .NET 3.5 doesn't support most XSL/T 2.0 functions (if any at all).

An example:

A call to a 2.0 function gives the following error message under .NET 3.5:

'current-dateTime()' is an unknown XSLT function.

I think the answer above is wrong. I can't find any evidence that Microsoft supports XSLT 2.0. XSLT != XPath.

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)

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

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 <xml path> >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:

 <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.xsl:

<xsl:transform
        version="1.0"
        xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
        xmlns:myCustDate="urn:custDate">

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

    <xsl:template match="/">
        <html>
            <head>
                <style>
                    TABLE.tblMaster
                    {
                        border-style: solid; 
                        border-width: 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:transform>        

When discussing .NET support for XSLT 2.0, XPath 2.0, and XQuery 1.0, it is important to distinguish between the languages themselves and the Data Model (XDM). The .NET 3.5 Framework supports the Data Model, but not the languages. As it was recently explained to me via email correspondence by Microsoft's Pawel Kadluczka:

The sentence "instances of the XQuery 1.0 and XPath 2.0 Data Model" may be confusing but I believe it refers to W3C XQuery 1.0 and XPath 2.0 Data Model (XDM) spec (http://www.w3.org/TR/xpath-datamodel) that reads:

[Definition: Every instance of the data model is a sequence.].

[Definition: A sequence is an ordered collection of zero or more items.] A sequence cannot be a member of a sequence. A single item appearing on its own is modeled as a sequence containing one item. Sequences are defined in 2.5 Sequences.

[Definition: An item is either a node or an atomic value],

In the case of XPath API - XPathNodeIterator is the sequence while XPathItem (XPathNavigator) represents the item.

Yes, 3.5 XPathNavigator supports XSLT 2.0.

http://msdn.microsoft.com/en-us/library/system.xml.xpath.xpathnavigator.aspx

"The XPathNavigator class in the System.Xml.XPath namespace is an abstract class which defines a cursor model for navigating and editing XML information items as instances of the XQuery 1.0 and XPath 2.0 Data Model."

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top