xslt 转换的正确语法

发布于 2024-10-20 19:16:17 字数 4687 浏览 1 评论 0原文

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

当我手动测试 xslt 转换一切正常时,

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);
                }

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

编辑:谢谢 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>

here is my code, I am getting an error when I try and parse my xml and xslt file- however I just want to know if my syntax/ code is correct for the transform. I am getting an exception with something like - Page\r\n

Using Visual studio when I manaully test the xslt transform everything works...

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);
                }

NOTE: My exception is "Root Element is missing".

EDIT: Thanks Dimitri - I will need to check your code, however just for clarity I think it is best I provide my code:

I am loading both my Xmls via Embedded resourse (if that does make any difference)

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>

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

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

发布评论

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

评论(1

执妄 2024-10-27 19:16:17

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

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

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

这里(xslt 样式表可能缺少top 元素):

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

这里(XML 源可能缺少 top 元素):

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

这里(转换结果可能缺少 top 元素):

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

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

The exception clearly tells you the reason of the problem: "Root Element is missing".

This means that you are trying to parse as XML something that doesn't have a top element (probably has more than one elements at the top level).

This can happen at three places in your code (not possible to say which, because you havent provided neither the source XML document, nor the XSLT stylesheet(s), nor the result of the transformation):

Here (the xslt stylesheet may lack a top element):

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

Here (the XML source may lack a top element):

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

And here (the result of the transformation may lack a top element):

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

Based from my experience, it is highly probable that the error happens at the last code snippet.

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