current-dateTime() is an unknown XSLT function Creare una XSL Extension

E’ un errore dovuto al fatto che current-DateTime() è XSLT/XPath 2.0, IE7 e Firefox supportano solo XSLT/XPath 1.0.

Questo vale anche se lo usate da codice .Net.

Soluzione è creare una estensione per xsl che restituisca la data:

Ecco il codice di esempio in c#:

namespace WindowsFormsApplication1
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
        
            
        string inputXML = @"<root><date></date></root>";
        XPathDocument doc = new XPathDocument(new StringReader(inputXML));
        XslTransform xslt = new XslTransform();
        xslt.Load(@"e:\test.xsl");
        XsltArgumentList xslArgs = new XsltArgumentList();

        CustomDateNow custDate = new CustomDateNow();            
        xslArgs.AddExtensionObject("urn:DateNow", custDate) ;
        StringWriter sw = new StringWriter();
        xslt.Transform(doc, xslArgs, sw);

        MessageBox.Show(sw.ToString());

        }


    }

    public class CustomDateNow
    {    
    //function that gets called from XSLT    
    public string GetDate()
        {        
        return DateTime.Now.ToString() ;    
        }
    }
}
 
Ecco il file xsl di esempio:
 
 <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:myDateNow="urn:DateNow">
  <xsl:output method="text" /> 
 <xsl:template match="root">
  <xsl:apply-templates select="date" /> 
  </xsl:template>
 <xsl:template match="date">
  <xsl:text>Il valore della estensione :</xsl:text> 
  <xsl:value-of select="myDateNow:GetDate()" /> 
  </xsl:template>
  </xsl:stylesheet>

 

 

notare la dichiarazione in alto xmlns:myDateNow="urn:DateNow">

 

 

ecco il risultato:

 

image

 

Have Fun smile_wink

Published 12-01-2009 6:48 by NinoCrudele
Filed under: