开发者

.NET Xsl script transform results in 'System.Object' is not defined or imported

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 开发者_Go百科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<xsl:value-of select="./QuickLink/text()"/></loc>
      <lastmod><xsl:value-of select="./DateModified/text()" /></lastmod>
    </url>
  </xsl:for-each>      
</urlset>
</xsl:template>
</xsl:stylesheet>


I know this issue is very old, but maybe this helps someone who is searching for this problem.

I currently became the same compiling error but in totally different topic of C# programming.

I am using Sharp Develop 4.2 and had the same issue. For me the solution was to add "mscorlib" to the references of the project.

This error is a known issue of Microsoft, but dont know the current situation. Just found some discussions from 2010.

Somehow this reference was missing and i do not completely understand at the moment why other projects of me are working without explicit reference to mscorlib, but maybe this indeed is the bug itself ;-)

Best regards Thomas


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.


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</loc>
      <lastmod>
        <xsl:value-of select="nlbExtension:FormatDateTime(./date,'s')"/>
      </lastmod>
    </url>
  </xsl:for-each>      
</urlset>   

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜