.NET Xsl 脚本转换结果为“System.Object”;未定义或导入

发布于 2024-08-18 04:06:58 字数 2062 浏览 4 评论 0 原文

XSL 新手但完全卡住了!

我有一个使用 c# 脚本函数格式化日期的转换,当我在 VS 2008 中运行“显示 xml 输出”时,这工作正常,输出正是我想要的。

但是,当我尝试使用代码运行它时,我收到错误

预定义类型“System.Object”不是 定义或导入

调用转换的函数看起来像这样,它非常基本并且在我开始使用脚本之前就可以工作

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

    XslCompiledTransform 变换 = new XslCompiledTransform();
    XsltSettings 设置 = new XsltSettings();
    设置.EnableScript = true;

    变换.Load(xsltFile,设置,空);

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

    //创建一个输出到内存流的XmlTextWriter
    流流 = new MemoryStream();
    XmlWriter xmlWriter = new XmlTextWriter(stream,> System.Text.Encoding.UTF8);


    变换.Transform(xmlTextReader, xmlWriter);

    流.Position = 0;

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

    返回 XmlDoc.OuterXml;
  }

XSL 转换是这样的..





    





    <网址>
      http://www.nlb.org
      
    
        



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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

无力看清 2024-08-25 04:06:58

我知道这个问题很老了,但这也许可以帮助正在寻找这个问题的人。

我目前遇到了相同的编译错误,但在完全不同的 C# 编程主题中。

我正在使用 Sharp Develop 4.2 并遇到了同样的问题。对我来说,解决方案是添加
“mscorlib”到项目的引用。

这个错误是微软的一个已知问题,但不知道目前的情况。刚刚找到了 2010 年的一些讨论。

不知何故,这个参考文献丢失了,我目前不完全理解为什么我的其他项目在没有明确参考 mscorlib 的情况下工作,但也许这确实是错误本身;-)

最好的问候
托马斯

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

酒与心事 2024-08-25 04:06:58

无法重现(事实上,您实际上并未在 xslt 中使用该扩展)。我测试了它,添加 (到 xslt):

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

并使用输入 xml:

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

并且它工作正常(我更改为 XmlConvert.ToDateTime 以匹配 xsd 格式,但无论哪种方式都工作正常)。

如果存在问题,则问题出在您未向我们展示的代码中。

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.

可是我不能没有你 2024-08-25 04:06:58

这是一个更简单的示例,(抱歉拼写错误),这在 VS2008 中同样有效,只需针对样式表运行 XML,但使用我得到的 C# 代码

未定义或导入预定义类型“System.Object”

代码

public void RunFileXSLT()
{
    // 将 books.xml 作为 XPathDocument 打开。
    XPathDocument doc = new XPathDocument("c:\\temp\\raw.xml");

    // 创建一个写入器来写入转换后的文件。
    XmlWriter writer = XmlWriter.Create("c:\\temp\\OutputTest.xml");

    // 创建并加载启用脚本执行的转换。
    XslCompiledTransform 变换 = new XslCompiledTransform();
    XsltSettings 设置 = new XsltSettings();
    设置.EnableScript = true;
    变换.Load(“c:\\temp\\Simple.xslt”,设置,null);

    // 执行转换。
    变换.Transform(doc, writer);
}

xml是这样的

;
  <项目>
     <日期>11/11/2009
  
  <项目>
   <日期>11/11/2009
  

变换是这样的




               


                 
 



   
  
    <网址>
      http://www.a-website.com
      <最后修改>
        
      
    
        
   

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>   

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文