views:

291

answers:

2

Hi

XSL noobie but uttery stuck!

I have a transform that formats a date using c# scripting function , this works fine when I am in VS 2008 and run "show xml output", the output is exactly what I want.

However , when i try to run this using code I get the error

Predefined type 'System.Object' is not defined or imported

To function to call the transform looks like this , it's pretty basic and worked before I started to use scripting

public static string RunXSLT(string xsltFile, string inputXML)
{

    XslCompiledTransform transform = new XslCompiledTransform();
    XsltSettings settings = new XsltSettings();
    settings.EnableScript = true;

    transform.Load(xsltFile, settings, null);

    StringReader sReader = new StringReader(inputXML);
    XmlTextReader xmlTextReader = new XmlTextReader(sReader);

    //Create an XmlTextWriter which outputs to memory stream
    Stream stream = new MemoryStream();
    XmlWriter xmlWriter = new XmlTextWriter(stream,> System.Text.Encoding.UTF8);


    transform.Transform(xmlTextReader, xmlWriter);

    stream.Position = 0;

    XmlDocument XmlDoc = new XmlDocument();
    XmlDoc.Load(stream);

    return XmlDoc.OuterXml;
  }

The XSL transform is this..

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"
            xmlns:msxsl="urn:schemas-microsoft-com:xslt"
            xmlns:nlbExtension="urn:nlbExtension"
            exclude-result-prefixes="msxsl nlbExtension">

<xsl:output method="xml" indent="yes"/>

<msxsl:script implements-prefix="nlbExtension" language="C#">    
<![CDATA[
  public string FormatDateTime(string xsdDateTime, string format)
  {
      DateTime date = DateTime.Parse(xsdDateTime);

      return date.ToString(format); 
   }
]]>
</msxsl:script>

<xsl:template match="/">
<urlset xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9
  http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd" 
  xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"

  <xsl:for-each select="./Collection/Content" >
    <url>
      <loc>http://www.nlb.org&lt;xsl:value-of select="./QuickLink/text()"/></loc>
      <lastmod><xsl:value-of select="./DateModified/text()" /></lastmod>
    </url>
  </xsl:for-each>      
</urlset>
</xsl:template>
</xsl:stylesheet>
+1  A: 

Cannot reproduce (indeed, you don't actually use the extension in your xslt). I tested it, adding (to the xslt):

<xsl:value-of select="nlbExtension:FormatDateTime(.,'dd MMM yyyy')"/>

and using the input xml:

string xml = new XElement("xml", DateTime.Now).ToString();

And it worked fine (I changed to XmlConvert.ToDateTime to match xsd format, but it worked OK either way).

If there is a problem, it is in code that you aren't showing us.

Marc Gravell
A: 

Hi Marc

Here is a simpler example , (sorry about typo) , again this works in VS2008 just running the XML against the style sheet but using the C# code I get

Predefined type 'System.Object' is not defined or imported

Code

public void RunFileXSLT()
{
    // Open books.xml as an XPathDocument.
    XPathDocument doc = new XPathDocument("c:\\temp\\raw.xml");

    // Create a writer for writing the transformed file.
    XmlWriter writer = XmlWriter.Create("c:\\temp\\OutputTest.xml");

    // Create and load the transform with script execution enabled.
    XslCompiledTransform transform = new XslCompiledTransform();
    XsltSettings settings = new XsltSettings();
    settings.EnableScript = true;
    transform.Load("c:\\temp\\Simple.xslt", settings, null);

    // Execute the transformation.
    transform.Transform(doc, writer);
}

the xml is this

<xml>
  <item>
     <date>11/11/2009</date>
  </item>
  <item>
   <date>11/11/2009</date>
  </item>
</xml>

the transform is this

<?xml version="1.0" encoding="ISO-8859-1"?>

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"
            xmlns:msxsl="urn:schemas-microsoft-com:xslt"
            xmlns:nlbExtension="urn:nlbExtension"
            exclude-result-prefixes="msxsl nlbExtension">
               <xsl:output method="xml" indent="yes"/>


             <msxsl:script implements-prefix="nlbExtension" language="C#">    
<![CDATA[
  public string FormatDateTime(string xsdDateTime, string format)
  {
      DateTime date = DateTime.Parse(xsdDateTime);

      return date.ToString(format); 
   }
]]>   </msxsl:script>


<xsl:template match="/">
   <urlset xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9
       http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd"
      xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" >
  <xsl:for-each select="./xml/item" >
    <url>
      <loc>http://www.a-website.com&lt;/loc&gt;
      <lastmod>
        <xsl:value-of select="nlbExtension:FormatDateTime(./date,'s')"/>
      </lastmod>
    </url>
  </xsl:for-each>      
</urlset>   

Chris
Works fine for me: `<?xml version="1.0" encoding="utf-8"?><urlset xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd" xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><url><loc>http://www.a-website.com</loc><lastmod>2009-11-11T00:00:00</lastmod></url><url><loc>http://www.a-website.com</loc><lastmod>2009-11-11T00:00:00</lastmod></url></urlset>`
Marc Gravell
hmm crikey , I wonder what the problem is then. The XSL looks right and the .NET is pretty much cut-paste from MSDN example ,.net installation problem?? Grasping at straws here
Chris