【问题标题】:Add Stylesheet reference to XML Document in Linq?在 Linq 中添加对 XML 文档的样式表引用?
【发布时间】:2010-09-09 21:01:02
【问题描述】:

我创建了一个 XML Doc 并希望获得对 XSLT 文件的引用。

//<?xml-stylesheet type="text/xsl" href="OBReport.xslt"?>

到这个 XML 生成:

XElement xml = new XElement("ReportedOn",
                    from dl in EL.DocumentLog.ToList()
                    join o in EL.Organization
                    on dl.OrganizationID equals o.OrganizationId
                    where dl.ActionDate >= stDate &
                    dl.ActionDate <= enDate 
                    orderby dl.DefendantName, dl.DocumentName
                    select new XElement("persons",
                              new XAttribute("documentName", dl.DocumentName),
                              new XElement("defendantName", dl.DefendantName),
                              new XElement("actionDate", dl.ActionDate.ToString()),
                              new XElement("startDate", dl.StartDate.ToString()),
                           new XElement("endDate", dl.EndDate.ToString()),
                           new XElement("organizationName" , o.OrganizationName) ));

【问题讨论】:

  • @Henk Holterman:如果 OP 想要为 XSLT 样式表或 CSS 样式表添加 PI,这没有区别。此外,将 XSLT 样式表引用到 XML 文档中也不是 XSLT 规范的一部分。最后,OP 想用 LINQ 做到这一点,而不是 XSLT 本身。所以,请回滚您的标题和标签版本。
  • @Alejandro:不,处理指令是答案的一部分,而不是问题的一部分。我发现 PI 很神秘。
  • @Henk Holterman:我认为这个&lt;?xml-stylesheet type="text/xsl" href="OBReport.xslt"?&gt;(来自问题的文字)是 PI...。但这与我之前的论点无关
  • @Alejandro:问题标题也是 SEO 的一种形式。知道这是 ProcessingInstruction 的人不会搜索此问题。
  • @Henk Holterman:很公平!这就是我重新定义为“样式表参考”的原因。

标签: .net xml linq-to-xml


【解决方案1】:

添加一个XProcessingInstruction 元素。

而不是你的 XElement(它可以用作文档,但有限制),而是一个包围 XDocument。所以,在你的代码之后:

 XElement body = ...; // root XElement from your Linq statement 
 XDocument doc = new XDocument(
      new XProcessingInstruction("xml-stylesheet", "type='text/xsl' ref='hello.xsl'"), 
      body);  

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-22
    相关资源
    最近更新 更多