Spring mvc Saxon似乎忽略了SpringMVC应用程序中的某些XSL调用,导致FOP异常

Spring mvc Saxon似乎忽略了SpringMVC应用程序中的某些XSL调用,导致FOP异常,spring-mvc,xslt,xsl-fo,saxon,apache-fop,Spring Mvc,Xslt,Xsl Fo,Saxon,Apache Fop,在我的Spring MVC web应用程序中,我试图使用我的一个服务类中的格式化对象转换xml文件时出错: net.sf.saxon.trans.XPathException: org.apache.fop.fo.ValidationException: "fo:root" is missing child elements. Required content model: (layout-master-set, declarations?, bookmark-tree?, (page-sequ

在我的Spring MVC web应用程序中,我试图使用我的一个服务类中的格式化对象转换xml文件时出错:

net.sf.saxon.trans.XPathException: org.apache.fop.fo.ValidationException: "fo:root" is missing child elements. Required content model: (layout-master-set, declarations?, bookmark-tree?, (page-sequence|fox:external-document)+) (See position 30:-1)
引发异常的Java代码用于按照我的格式化对象中概述的规则将xml文件转换为pdf有问题的Java代码:

FopFactory f = FopFactory.newInstance();
//Load the fop configuration file (for installing custom fonts)
DefaultConfigurationBuilder cfgBuilder = new DefaultConfigurationBuilder();
Configuration cfg = cfgBuilder.buildFromFile(new File("src/main/webapp/resources/fop-config.xml"));
f.setUserConfig(cfg);

FOUserAgent agent = f.newFOUserAgent();         

OutputStream os = new FileOutputStream(new File(resultFileName));
os = new BufferedOutputStream(os);

Fop fop = f.newFop(MimeConstants.MIME_PDF, agent, os);

//Load the formatting object
TransformerFactory fac = new net.sf.saxon.TransformerFactoryImpl();
Transformer t = fac.newTransformer(new StreamSource(new File("src/main/webapp/resources/fo.xsl")));

File file = new File(fileName);
Source src = new StreamSource(file);
Result r = new SAXResult(fop.getDefaultHandler());

//Perform the transformation and close resources
t.transform(src, r);
os.close();
<fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format">
    <fo:layout-master-set>
        <fo:simple-page-master master-name="letter-first"
            page-height="11in" page-width="8.5in" margin-top="0.25in">

            <fo:region-body margin-top="1.0in" margin-bottom="0.75in"
                margin-left="0.60in" margin-right="0.60in" />
            <fo:region-before extent="0.7in" />
            <fo:region-after extent="0.4in" />
        </fo:simple-page-master>

        <fo:simple-page-master master-name="letter-rest"
            page-height="11in" page-width="8.5in" margin-top="0.25in">

            <fo:region-body margin-top="0.5in" margin-bottom="0.75in"
                margin-left="0.60in" margin-right="0.60in" />
            <fo:region-after extent="0.4in" />
        </fo:simple-page-master>
        <fo:page-sequence-master master-name="letter">
            <fo:single-page-master-reference
                master-reference="letter-first" />
            <fo:repeatable-page-master-reference
                master-reference="letter-rest" />
        </fo:page-sequence-master>
    </fo:layout-master-set>

    <!-- Drop in the body -->
    <xsl:apply-templates />
</fo:root>
<xsl:template match="template">
    <fo:page-sequence master-reference="letter"
    <dependency>
        <groupId>org.apache.xmlgraphics</groupId>
        <artifactId>fop</artifactId>
        <version>1.1</version>
        <exclusions>
            <exclusion>
                <groupId>org.apache.avalon.framework</groupId>
                <artifactId>avalon-framework-api</artifactId>
            </exclusion>
            <exclusion>
                <groupId>org.apache.avalon.framework</groupId>
                <artifactId>avalon-framework-impl</artifactId>
            </exclusion>
        </exclusions>
    </dependency>

    <dependency>
        <groupId>net.sourceforge.saxon</groupId>
        <artifactId>saxon</artifactId>
        <version>9.1.0.8</version>
    </dependency>
这是我的格式化对象中FOP似乎不喜欢的部分:

FopFactory f = FopFactory.newInstance();
//Load the fop configuration file (for installing custom fonts)
DefaultConfigurationBuilder cfgBuilder = new DefaultConfigurationBuilder();
Configuration cfg = cfgBuilder.buildFromFile(new File("src/main/webapp/resources/fop-config.xml"));
f.setUserConfig(cfg);

FOUserAgent agent = f.newFOUserAgent();         

OutputStream os = new FileOutputStream(new File(resultFileName));
os = new BufferedOutputStream(os);

Fop fop = f.newFop(MimeConstants.MIME_PDF, agent, os);

//Load the formatting object
TransformerFactory fac = new net.sf.saxon.TransformerFactoryImpl();
Transformer t = fac.newTransformer(new StreamSource(new File("src/main/webapp/resources/fo.xsl")));

File file = new File(fileName);
Source src = new StreamSource(file);
Result r = new SAXResult(fop.getDefaultHandler());

//Perform the transformation and close resources
t.transform(src, r);
os.close();
<fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format">
    <fo:layout-master-set>
        <fo:simple-page-master master-name="letter-first"
            page-height="11in" page-width="8.5in" margin-top="0.25in">

            <fo:region-body margin-top="1.0in" margin-bottom="0.75in"
                margin-left="0.60in" margin-right="0.60in" />
            <fo:region-before extent="0.7in" />
            <fo:region-after extent="0.4in" />
        </fo:simple-page-master>

        <fo:simple-page-master master-name="letter-rest"
            page-height="11in" page-width="8.5in" margin-top="0.25in">

            <fo:region-body margin-top="0.5in" margin-bottom="0.75in"
                margin-left="0.60in" margin-right="0.60in" />
            <fo:region-after extent="0.4in" />
        </fo:simple-page-master>
        <fo:page-sequence-master master-name="letter">
            <fo:single-page-master-reference
                master-reference="letter-first" />
            <fo:repeatable-page-master-reference
                master-reference="letter-rest" />
        </fo:page-sequence-master>
    </fo:layout-master-set>

    <!-- Drop in the body -->
    <xsl:apply-templates />
</fo:root>
<xsl:template match="template">
    <fo:page-sequence master-reference="letter"
    <dependency>
        <groupId>org.apache.xmlgraphics</groupId>
        <artifactId>fop</artifactId>
        <version>1.1</version>
        <exclusions>
            <exclusion>
                <groupId>org.apache.avalon.framework</groupId>
                <artifactId>avalon-framework-api</artifactId>
            </exclusion>
            <exclusion>
                <groupId>org.apache.avalon.framework</groupId>
                <artifactId>avalon-framework-impl</artifactId>
            </exclusion>
        </exclusions>
    </dependency>

    <dependency>
        <groupId>net.sourceforge.saxon</groupId>
        <artifactId>saxon</artifactId>
        <version>9.1.0.8</version>
    </dependency>

我知道异常试图告诉我缺少fo:page sequence元素。但是,这个元素存在于我试图在根元素中应用的模板中在上面代码的正下方,我声明我的模板:

FopFactory f = FopFactory.newInstance();
//Load the fop configuration file (for installing custom fonts)
DefaultConfigurationBuilder cfgBuilder = new DefaultConfigurationBuilder();
Configuration cfg = cfgBuilder.buildFromFile(new File("src/main/webapp/resources/fop-config.xml"));
f.setUserConfig(cfg);

FOUserAgent agent = f.newFOUserAgent();         

OutputStream os = new FileOutputStream(new File(resultFileName));
os = new BufferedOutputStream(os);

Fop fop = f.newFop(MimeConstants.MIME_PDF, agent, os);

//Load the formatting object
TransformerFactory fac = new net.sf.saxon.TransformerFactoryImpl();
Transformer t = fac.newTransformer(new StreamSource(new File("src/main/webapp/resources/fo.xsl")));

File file = new File(fileName);
Source src = new StreamSource(file);
Result r = new SAXResult(fop.getDefaultHandler());

//Perform the transformation and close resources
t.transform(src, r);
os.close();
<fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format">
    <fo:layout-master-set>
        <fo:simple-page-master master-name="letter-first"
            page-height="11in" page-width="8.5in" margin-top="0.25in">

            <fo:region-body margin-top="1.0in" margin-bottom="0.75in"
                margin-left="0.60in" margin-right="0.60in" />
            <fo:region-before extent="0.7in" />
            <fo:region-after extent="0.4in" />
        </fo:simple-page-master>

        <fo:simple-page-master master-name="letter-rest"
            page-height="11in" page-width="8.5in" margin-top="0.25in">

            <fo:region-body margin-top="0.5in" margin-bottom="0.75in"
                margin-left="0.60in" margin-right="0.60in" />
            <fo:region-after extent="0.4in" />
        </fo:simple-page-master>
        <fo:page-sequence-master master-name="letter">
            <fo:single-page-master-reference
                master-reference="letter-first" />
            <fo:repeatable-page-master-reference
                master-reference="letter-rest" />
        </fo:page-sequence-master>
    </fo:layout-master-set>

    <!-- Drop in the body -->
    <xsl:apply-templates />
</fo:root>
<xsl:template match="template">
    <fo:page-sequence master-reference="letter"
    <dependency>
        <groupId>org.apache.xmlgraphics</groupId>
        <artifactId>fop</artifactId>
        <version>1.1</version>
        <exclusions>
            <exclusion>
                <groupId>org.apache.avalon.framework</groupId>
                <artifactId>avalon-framework-api</artifactId>
            </exclusion>
            <exclusion>
                <groupId>org.apache.avalon.framework</groupId>
                <artifactId>avalon-framework-impl</artifactId>
            </exclusion>
        </exclusions>
    </dependency>

    <dependency>
        <groupId>net.sourceforge.saxon</groupId>
        <artifactId>saxon</artifactId>
        <version>9.1.0.8</version>
    </dependency>


我从未更新过这个,但我发现问题在于输入文件实际上没有声明正确的名称空间,正如Martin在评论中所建议的那样。我以为我使用的是一个特定的输入文件,结果发现我使用的是一个完全不同的文件。

在使用它之前,您不应该定义名称空间声明“fo”吗?我在样式表标签中定义了它。我发布的XSL-FO代码正是FOP抱怨的部分,但我的整个FO.XSL文件相当长,包含了所有必需的名称空间声明。对不起,没有想法。我担心涉及多个产品交互(FOP、Saxon、Spring)的系统问题不太适合StackOverflow过程。如果不先复制它,就不可能解决此问题,创建复制可能很棘手。假设错误消息是正确的,那么我将假设您在错误发生时处理的输入没有由模板使用
match=“template”
处理的元素。所以不知何故,元素丢失了,可能是因为不同的输入或仅仅是不同的名称空间。@MartinHonnen我认为情况并非如此<代码>
应该应用特定的模板,无论匹配模式是什么,如果我理解正确的话。