c# - xslt 转换的正确语法

标签 c# .net xml .net-3.5 xslt

这是我的代码,当我尝试解析我的 xml 和 xslt 文件时出现错误 - 但是我只想知道我的语法/代码对于转换是否正确。我收到类似 - Page\r\n

之类的异常

当我手动测试 xslt 转换时使用 Visual Studio 一切正常...

var XslFo = XElementPassedInAsAParamOrSomething;

                //load the xsl
                XslCompiledTransform xslTrans = new XslCompiledTransform();
                using (XmlReader reader = XslFo.CreateReader())
                {
                    xslTrans.Load(reader);
                }

                //create the output stream
                MemoryStream result = new MemoryStream();

                //do the actual transform of xml
                using (XmlReader reader = XmlData.CreateReader())
                {
                    xslTrans.Transform(reader, null, result);
                }

                //return result as XElement (for dumping  db etc)
                using (XmlReader reader = XmlReader.Create(result))
                {
                    return XElement.Load(reader);
                }

注意:我的异常(exception)是“根元素丢失”。

编辑:谢谢 Dimitri - 我需要检查您的代码,但为了清楚起见,我认为最好提供我的代码:

我正在通过嵌入式资源加载我的两个 Xml(如果这样做有任何区别的话)

XML:

    <html>   
<head>
        <title>Spanish Review Handbook</title>   
</head>   
<body>
        <h3>Introduction</h3>
        <p>
          This handbook covers the major topics in Spanish, but is by
          no means complete.
        </p>   
</body> 
</html>

Xsl:

 <?xml version="1.0" encoding="utf-8"?>
    <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                    xmlns:fo="http://www.w3.org/1999/XSL/Format"
                    version="1.0">

      <xsl:output method="xml" indent="yes"/>
      <xsl:variable name="pagewidth" select="21.5"/>
      <xsl:variable name="bodywidth" select="19"/>
      <xsl:template match="html">
        <fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format">

      <fo:layout-master-set>
        <fo:simple-page-master master-name="leftPage"
          page-height="27.9cm"
          page-width="{$pagewidth}cm"
          margin-left="1cm"
          margin-right="2cm"
          margin-top="1cm"
          margin-bottom="1cm">
          <fo:region-before extent="1cm"/>
          <fo:region-after extent="1cm"/>
          <fo:region-body
            margin-top="1cm"
            margin-bottom="1cm" />
        </fo:simple-page-master>

        <fo:simple-page-master master-name="rightPage"
          page-height="27.9cm"
          page-width="{$pagewidth}cm"
          margin-left="2cm"
          margin-right="1cm"
          margin-top="0.5cm"
          margin-bottom="0.5cm">
          <fo:region-before extent="1cm"/>
          <fo:region-after extent="1cm"/>
          <fo:region-body
            margin-top="1cm"
            margin-bottom="1cm" />
        </fo:simple-page-master>

        <!-- Set up the sequence of pages -->
        <fo:page-sequence-master master-name="contents">
          <fo:repeatable-page-master-alternatives>
            <fo:conditional-page-master-reference
              master-name="leftPage"
              odd-or-even="odd"/>
            <fo:conditional-page-master-reference
              master-name="rightPage"
              odd-or-even="even"/>
          </fo:repeatable-page-master-alternatives>
        </fo:page-sequence-master>
      </fo:layout-master-set>

      <fo:page-sequence master-name="contents" initial-page-number="1">
        <xsl:apply-templates />
      </fo:page-sequence>
    </fo:root>
  </xsl:template>

  <xsl:template match="body">
    <fo:flow flow-name="xsl-region-body">
      <xsl:apply-templates/>
    </fo:flow>
  </xsl:template>

  <xsl:template match="h3">
    <fo:block font-size="14pt" font-weight="bold"
      space-before="6pt">
      <xsl:apply-templates/>
    </fo:block>
  </xsl:template>

  <xsl:template match="p">
    <fo:block text-indent="1em"
      space-before="4pt" space-after="4pt">
      <xsl:apply-templates/>
    </fo:block>
  </xsl:template>

  <xsl:template match="b">
    <fo:inline font-weight="bold">
      <xsl:apply-templates/>
    </fo:inline>
  </xsl:template>

  <xsl:template match="i">
    <fo:inline font-style="italic">
      <xsl:apply-templates/>
    </fo:inline>
  </xsl:template>

</xsl:stylesheet>

最佳答案

异常清楚地告诉您问题的原因:“缺少根元素”。

这意味着您正在尝试将没有顶级元素的内容解析为 XML(可能在顶级有多个元素)。

这可能发生在您的代码中的三个位置(无法说是哪一个,因为您既没有提供源 XML 文档,也没有提供 XSLT 样式表,也没有提供转换结果):

此处(xslt 样式表可能缺少顶部元素):

using (XmlReader reader = XslFo.CreateReader())                 
{                     
  xslTrans.Load(reader);                 
}  

此处(XML 源可能缺少顶部元素):

using (XmlReader reader = XmlData.CreateReader())                    
{                        
  xslTrans.Transform(reader, null, result);                    
} 

这里(转换的结果可能缺少一个顶级元素):

using (XmlReader reader = XmlReader.Create(result))                     
{                         
  return XElement.Load(reader);                     
}  

根据我的经验,错误发生在最后一段代码的可能性很大。

关于c# - xslt 转换的正确语法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5219559/

相关文章:

c# - session 是否使用 cookie?

c# - 消息路由、分区和并行处理

c# - 在 Singleton.Instance 后面调用异步方法

c# - C# 对象到 JSON 的模板化序列化

xml - "collapse all"在firefox或chrome中一键所有节点

xml - 通过 Delphi XML 数据绑定(bind)向导使用 XML 枚举

c# - 使用现有控件向窗体添加选项卡控件

.net - 代码访问安全是个笑话?

c# - 如何以经过身份验证的方式运行应用程序

c# - 将 Web 服务中的数据表写入 XML 的最佳方式?