将ArrayList渲染为XSLT



我有一个对象PDFData与一些属性。其中一个是Arraylist,一个是String,另一个是自定义对象(带有一些基本属性)。

我需要用XSLT呈现这两个列表,以便用Apache-FOP创建PDF文件。我用谷歌搜索了很多方法来解决这个问题,但我什么也没找到。

渲染PDF的类是:

public class PDFRender {
public static final String SUMMARY_REPORT_XSL = "summary_report.xsl";
//private final Logger log = LoggerFactory.getLogger(getClass());
public ByteArrayInputStream createPDFFile(ByteArrayOutputStream xmlSource) throws IOException {
    ByteArrayInputStream inputStream = null;
    InputStream template = Thread.currentThread().getContextClassLoader().getResourceAsStream(SUMMARY_REPORT_XSL);
    StreamSource transformSource = new StreamSource(template);
    FopFactory fopFactory = FopFactory.newInstance();
    FOUserAgent foUserAgent = fopFactory.newFOUserAgent();
    ByteArrayOutputStream pdfOutStream = new ByteArrayOutputStream();
    StreamSource source = new StreamSource(new ByteArrayInputStream(xmlSource.toByteArray()));
    Transformer xslfoTransformer;
    try {
        TransformerFactory transfact = TransformerFactory.newInstance();
        xslfoTransformer = transfact.newTransformer(transformSource);
        Fop fop;
        try {
            fop = fopFactory.newFop(MimeConstants.MIME_PDF, foUserAgent, pdfOutStream);
            // Resulting SAX events (the generated FO) must be piped through to FOP
            Result res = new SAXResult(fop.getDefaultHandler());
            // Start XSLT transformation and FOP processing
            try {
                xslfoTransformer.transform(source, res);
                inputStream = new ByteArrayInputStream(pdfOutStream.toByteArray());
            } catch (TransformerException e) {
                String msg = "Caught exception transforming source to generate PDF";
                //log.error(msg, e);
                throw new RuntimeException(msg, e);
            }
        } catch (FOPException e) {
            String msg = "Caught FOP exception generating PDF";
            //log.error(msg, e);
            throw new RuntimeException(msg, e);
        }
    } catch (TransformerConfigurationException e) {
        String msg = "Caught exception transforming source to generate PDF";
        //log.error(msg, e);
        throw new RuntimeException(msg, e);
    }
    return inputStream;
}
public ByteArrayOutputStream getXMLSource(PDFData data) throws Exception {
    JAXBContext context;
    ByteArrayOutputStream outStream = new ByteArrayOutputStream();
    try {
        context = JAXBContext.newInstance(PDFData.class);
        Marshaller m = context.createMarshaller();
        m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
        m.marshal(data, outStream);
    } catch (JAXBException e) {
        String msg = "Caught exception marshalling application to generate PDF";
        //log.error(msg, e);
        throw new RuntimeException(msg, e);
    }
    return outStream;
}
}

PDFData如下:

@XmlRootElement(name = "PDFData")
public class PDFData {
public String clientName;
public String clientProfile;
public String clientLang;
public URL clientLogo;
public ObjectId verificationId;
public String verificationCountry;
public String verificationDocumentType;
public List<Page> verificationPages = new ArrayList<>();
public String verificationTemplateName;
public Long verificationTime;
public List<ActionResult> verificationActions = new ArrayList<>();
public List<String> verificationMrz = new ArrayList<>();
/* ... */
}

我的XSLT没有相关性,因为我还没有任何关于它的东西。也许只是为了评论它,是相关的,当我必须访问一些属性(例如clientName),我这样做:

<xsl:when test="clientName = 'Test client'">Orange</xsl:when>

我使用XSLT 1.1与Jar依赖'org.apache.xmlgraphics:fop:1.0'(版本1.1。gradle不能加载依赖项,我不知道为什么)。

摘要:有人知道如何将Java列表呈现为XSL模板吗?

提前感谢!

UPDATE 1:我找到了一个解决办法。它解决了我的一个问题。我可以得到所有的值。但现在我有另一个问题。我怎样才能在它们之间循环?

UPDATE 2: @KevinBrown认为我的XSLT可能很重要,所以这里是我的XSLT:

<xsl:stylesheet version="2.0"
            xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
            xmlns:fo="http://www.w3.org/1999/XSL/Format">
<xsl:template match="PDFData">
    <!-- Define variables -->
    <xsl:variable name="green" select="'green'" />
    <xsl:variable name="red" select="'#90AB76'" />
    <xsl:variable name="dark_gray" select="'#3C3F41'" />
    <xsl:variable name="light_gray" select="'#CCCCCC'" />
    <xsl:variable name="black" select="'#3D6B5C'" />
    <xsl:variable name="labelHeader">
        <xsl:choose>
            <xsl:when test="clientName = 'Test client'">Orange</xsl:when>
            <xsl:otherwise>Résultat Global: Le document est jugé rejecte</xsl:otherwise>
        </xsl:choose>
    </xsl:variable>

    <fo:root>
        <fo:layout-master-set>
            <fo:simple-page-master master-name="A4"
                                   page-height="297mm"
                                   page-width="210mm"
                                   margin-bottom="2.5cm">
                <fo:region-body margin-bottom="4mm"/>
                <fo:region-after extent="13mm" display-align="after"/>
            </fo:simple-page-master>
        </fo:layout-master-set>
        <fo:page-sequence master-reference="A4">
            <fo:flow flow-name="xsl-region-body">
                <fo:block font-family="any"
                          font-size="10pt">
                    <!-- Header -->
                    <fo:table table-layout="fixed"
                              width="100%"
                              margin-bottom="1.5em"
                              font-size="1.5em"
                              font-weight="bold"
                              background-color="{$dark_gray}">
                        <fo:table-column column-number="1" column-width="28%"/>
                        <fo:table-column column-number="2" column-width="10%"/>
                        <fo:table-column column-number="3" column-width="40%"/>
                        <fo:table-column column-number="4" column-width="1%"/>
                        <fo:table-column column-number="5" column-width="15%"/>
                        <fo:table-column column-number="6" column-width="1%"/>
                        <fo:table-body>
                            <!-- Empty -->
                            <fo:table-row height="2em">
                                <fo:table-cell>
                                    <fo:block>
                                    </fo:block>
                                </fo:table-cell>
                            </fo:table-row>
                            <fo:table-row color="{$light_gray}">
                                <!-- Logo -->
                                <fo:table-cell>
                                    <fo:block margin-left="1mm" font-weight="bold" text-align="center">
                                    </fo:block>
                                </fo:table-cell>
                                <!-- Company name -->
                                <fo:table-cell number-columns-spanned="2" background-color="{$light_gray}" color="{$dark_gray}" >
                                    <fo:block margin-left="1mm" margin="3mm 2mm 3mm 2mm" font-weight="bold">
                                        <xsl:value-of select="$labelHeader" />
                                    </fo:block>
                                </fo:table-cell>
                                <!-- Empty -->
                                <fo:table-cell>
                                    <fo:block margin-left="1mm" font-weight="bold">
                                    </fo:block>
                                </fo:table-cell>
                                <!-- Result -->
                                <fo:table-cell background-color="{$light_gray}" color="{$dark_gray}" >
                                    <fo:block margin-left="1mm"
                                              font-weight="bold"
                                              font-size="0.9em"
                                              text-align="center">Resultat</fo:block>
                                    <fo:block margin-left="1mm"
                                              font-weight="bold"
                                              font-size="0.9em"
                                              text-align="center"
                                              color="{$green}">Positif</fo:block>
                                </fo:table-cell>
                                <!-- Empty -->
                                <fo:table-cell>
                                    <fo:block margin-left="1mm" font-weight="bold">
                                    </fo:block>
                                </fo:table-cell>
                            </fo:table-row>
                            <!-- Empty -->
                            <fo:table-row height="2em">
                                <fo:table-cell>
                                    <fo:block>
                                    </fo:block>
                                </fo:table-cell>
                            </fo:table-row>
                        </fo:table-body>
                    </fo:table>
                    <!-- Verification results -->
                    <fo:table table-layout="fixed"
                              width="100%"
                              margin-bottom="0.5em"
                              margin-left="1em"
                              margin-right="1em"
                              font-size="1em">
                        <fo:table-column column-number="1" column-width="25%"/>
                        <fo:table-column column-number="2" column-width="25%"/>
                        <fo:table-column column-number="3" column-width="25%"/>
                        <fo:table-column column-number="4" column-width="25%"/>
                        <fo:table-body>
                            <!-- Header row -->
                            <fo:table-row height="0.1em">
                                <fo:table-cell number-columns-spanned="4"
                                               font-weight="bold"
                                               text-align="left"
                                               font-size="1.3em"
                                               padding-bottom="0.5em">
                                    <fo:block>Résultat des vérifications:</fo:block>
                                </fo:table-cell>
                            </fo:table-row>
                            <fo:table-row height="0.1em">
                                <fo:table-cell font-weight="bold"
                                               text-align="left"
                                               font-size="1.1em">
                                    <fo:block>Moment de réception:</fo:block>
                                </fo:table-cell>
                                <fo:table-cell text-align="left"
                                               font-size="1em">
                                    <fo:block>25-06-2015 15:38:14</fo:block>
                                </fo:table-cell>
                                <fo:table-cell font-weight="bold"
                                               text-align="left"
                                               font-size="1.1em">
                                    <fo:block>Moment de envoi:</fo:block>
                                </fo:table-cell>
                                <fo:table-cell text-align="left"
                                               font-size="1em">
                                    <fo:block>25-06-2015 15:38:47</fo:block>
                                </fo:table-cell>
                            </fo:table-row>
                            <fo:table-row height="0.1em">
                                <fo:table-cell font-weight="bold"
                                               text-align="left"
                                               font-size="1.1em">
                                    <fo:block>Durée de vérification:</fo:block>
                                </fo:table-cell>
                                <fo:table-cell text-align="left"
                                               font-size="1em">
                                    <fo:block>25s 187ms</fo:block>
                                </fo:table-cell>
                                <fo:table-cell font-weight="bold"
                                               text-align="left"
                                               font-size="1.1em">
                                    <fo:block>Modèle de document:</fo:block>
                                </fo:table-cell>
                                <fo:table-cell text-align="left"
                                               font-size="1em">
                                    <fo:block>Carte d'identité</fo:block>
                                </fo:table-cell>
                            </fo:table-row>
                            <fo:table-row>
                                <fo:table-cell number-columns-spanned="4" height="0.7em">
                                    <fo:block></fo:block>
                                </fo:table-cell>
                            </fo:table-row>
                            <fo:table-row height="0.1em">
                                <fo:table-cell font-weight="bold"
                                               text-align="left"
                                               font-size="1.1em">
                                    <fo:block>Vérifications positives:</fo:block>
                                </fo:table-cell>
                                <fo:table-cell text-align="left"
                                               font-size="1em">
                                    <fo:block>20</fo:block>
                                </fo:table-cell>
                                <fo:table-cell font-weight="bold"
                                               text-align="left"
                                               font-size="1.1em">
                                    <fo:block>Total des vérifications:</fo:block>
                                </fo:table-cell>
                                <fo:table-cell text-align="left"
                                               font-size="1em">
                                    <fo:block>20</fo:block>
                                </fo:table-cell>
                            </fo:table-row>
                            <fo:table-row>
                                <fo:table-cell number-columns-spanned="4" height="1.5em">
                                    <fo:block></fo:block>
                                </fo:table-cell>
                            </fo:table-row>
                        </fo:table-body>
                    </fo:table>
                    <!-- Line separator 1 -->
                    <fo:table table-layout="fixed"
                              width="auto"
                              margin-bottom="1.5em"
                              margin-left="0.5em"
                              margin-right="0.5em"
                              font-size="1.5em"
                              font-weight="bold"
                              border-top="1px solid {$dark_gray}">
                        <fo:table-column column-number="1" column-width="100%"/>
                        <fo:table-body>
                            <!-- Empty -->
                            <fo:table-row height="0.1em">
                                <fo:table-cell>
                                    <fo:block>
                                    </fo:block>
                                </fo:table-cell>
                            </fo:table-row>
                        </fo:table-body>
                    </fo:table>
                    <!-- MRZ -->
                    <fo:table table-layout="fixed"
                              width="auto"
                              margin-bottom="0.5em"
                              margin-left="1em"
                              margin-right="1em"
                              font-size="1em"
                              font-family="'arial'"
                            text-align="center">
                        <fo:table-column column-number="1" column-width="20%"/>
                        <fo:table-column column-number="2" column-width="60%" border="2px solid {$dark_gray}"/>
                        <fo:table-column column-number="3" column-width="20%"/>
                        <fo:table-body>
                            <!-- Header row -->
                            <fo:table-row height="0.1em">
                                <fo:table-cell height="1.5em">
                                    <fo:block></fo:block>
                                </fo:table-cell>
                                <fo:table-cell number-columns-spanned="1"
                                               font-weight="bold"
                                               text-align="left"
                                               font-size="1.3em"
                                               padding="0.5em"
                                               text-align-last="center"
                                               font-family="monospace">
                                    <fo:block>IDFRABEN&lt;HAMOUDA&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;783030</fo:block>
                                    <fo:block>1310783007866MAHJOUB&lt;&lt;&lt;&lt;&lt;&lt;&lt;8202109M9</fo:block>
                                </fo:table-cell>
                                <fo:table-cell height="1.5em">
                                    <fo:block></fo:block>
                                </fo:table-cell>
                            </fo:table-row>
                        </fo:table-body>
                    </fo:table>
                    <!-- Champs veriifications -->
                    <fo:table table-layout="fixed"
                              width="100%"
                              margin-bottom="0.5em"
                              margin-left="1em"
                              margin-right="1em"
                              font-size="1em">
                        <fo:table-column column-number="1" column-width="50%" border-right="1px solid {$dark_gray}"/>
                        <fo:table-column column-number="2" column-width="50%"/>
                        <fo:table-body>
                            <fo:table-row>
                                <fo:table-cell number-columns-spanned="2" height="1.5em">
                                    <fo:block>
<!-- HERE COMES ONE OF THE LISTS -->
</fo:block>
                                </fo:table-cell>
                            </fo:table-row>
                            <!-- Header row -->
                            <fo:table-row height="0.1em">
                                <fo:table-cell number-columns-spanned="2"
                                               font-weight="bold"
                                               text-align="left"
                                               font-size="1.3em"
                                               padding-bottom="0.5em">
                                    <fo:block>Vérifications des champs de la ZLA positives:
                                    </fo:block>
                                </fo:table-cell>
                            </fo:table-row>
                            <fo:table-row height="0.1em">
                                <fo:table-cell text-align="left"
                                               font-size="1em">
                                    <fo:block><!-- HERE COMES THE ANOTHER LIST --></fo:block>
                                </fo:table-cell>
                                <fo:table-cell text-align="left"
                                               font-size="1em">
                                    <fo:block>25-06-2015 15:38:14</fo:block>
                                </fo:table-cell>
                            </fo:table-row>
                        </fo:table-body>
                    </fo:table>
                    <!-- Visual verifications -->
                    <fo:table table-layout="fixed"
                              width="100%"
                              margin-bottom="0.5em"
                              margin-left="1em"
                              margin-right="1em"
                              font-size="1em">
                        <fo:table-column column-number="1" column-width="50%" border-right="1px solid {$dark_gray}"/>
                        <fo:table-column column-number="2" column-width="50%"/>
                        <fo:table-body>
                            <!-- Header row -->
                            <fo:table-row height="0.1em">
                                <fo:table-cell number-columns-spanned="2"
                                               font-weight="bold"
                                               text-align="left"
                                               font-size="1.3em"
                                               padding-bottom="0.5em">
                                    <fo:block>ZLA-Vérifications visuelles positives:</fo:block>
                                </fo:table-cell>
                            </fo:table-row>
                            <fo:table-row height="0.1em">
                                <fo:table-cell text-align="left"
                                               font-size="1em">
                                    <fo:block>Moment de réception:</fo:block>
                                </fo:table-cell>
                                <fo:table-cell text-align="left"
                                               font-size="1em">
                                    <fo:block>25-06-2015 15:38:14</fo:block>
                                </fo:table-cell>
                            </fo:table-row>
                            <fo:table-row>
                                <fo:table-cell number-columns-spanned="2" height="1.5em">
                                    <fo:block></fo:block>
                                </fo:table-cell>
                            </fo:table-row>
                        </fo:table-body>
                    </fo:table>
                    <!-- Line separator -->
                    <fo:table table-layout="fixed"
                              width="auto"
                              margin-bottom="1.5em"
                              margin-left="0.5em"
                              margin-right="0.5em"
                              font-size="1.5em"
                              font-weight="bold"
                              border-top="1px solid {$dark_gray}">
                        <fo:table-column column-number="1" column-width="100%"/>
                        <fo:table-body>
                            <!-- Empty -->
                            <fo:table-row height="0.1em">
                                <fo:table-cell>
                                    <fo:block>
                                    </fo:block>
                                </fo:table-cell>
                            </fo:table-row>
                        </fo:table-body>
                    </fo:table>
                    <!-- Front image (if there is only one image, will be here -->
                    <!-- Back image -->
                </fo:block>
            </fo:flow>
        </fo:page-sequence>
    </fo:root>
</xsl:template>

UPDATE 3: Woohooo!!这个周末我尝试了一些解决方案,我得到了一个循环:

<xsl:for-each select="verificationMrz">
    <xsl:variable name="i" select="position()" />
    <xsl:value-of select="$i" />
</xsl:for-each>

好的,太酷了!但是,当我试图访问列表与索引(<xsl:value-of select="verificationMrz[$i]" />),我没有得到任何值。我已经检查了<xsl:value-of select="verificationMrz[1]" />列表是否为空,它工作得很好。所以我认为我的问题是变量i,也许数据类型?我不知道…

解决!这是我一直在寻找的解决方案:

<xsl:for-each select="verificationMrz">
    <xsl:value-of select="current()" />
</xsl:for-each>

这将打印我的字符串列表。对于打印我的对象,我还不确定如何检索它,但我会尽快发布它我解决它。

相关内容

  • 没有找到相关文章

最新更新