gpt4 book ai didi

c# - xslt 转换的正确语法

转载 作者:太空宇宙 更新时间:2023-11-03 11:39:14 25 4
gpt4 key购买 nike

这是我的代码,当我尝试解析我的 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com