【发布时间】:2020-10-28 06:16:54
【问题描述】:
我是 XSLT 的新手,需要将 XML 文件转换为 PDF。我有一个使用 XSLT FO 文件将 XML 转换为 PDF 的 JAVA 程序。
下面是我的 XML。
<PDFReport xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<Type>ABC</Type>
<Number>001</Number>
<CustomerDetails>
<Country>USA</Country>
<Name>John Doe</Name>
</CustomerDetails>
<PurchasedGoods>Category A</PurchasedGoods>
<Amount>123456</Amount>
<CustomerDetails>
<Country>China</Country>
<Name>Stuart Lim</Name>
</CustomerDetails>
<PurchasedGoods>Category B</PurchasedGoods>
<Amount>987654</Amount>
</PDFReport>
我正在使用下面的 XSLT 文件转换为 XML 文件
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:fo="http://www.w3.org/1999/XSL/Format">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="/">
<fo:root>
<fo:layout-master-set>
<fo:simple-page-master master-name="simple" margin="0.5in">
<fo:region-body/>
</fo:simple-page-master>
</fo:layout-master-set>
<fo:page-sequence master-reference="simple">
<fo:flow flow-name="xsl-region-body">
<xsl:apply-templates/>
</fo:flow>
</fo:page-sequence>
</fo:root>
</xsl:template>
<xsl:template match="PDFReport">
<xsl:apply-templates select="./Type"/>
<xsl:apply-templates select="./Number"/>
<xsl:for-each select="./CustomerDetails">
<xsl:apply-templates select="."/>
</xsl:for-each>
<xsl:apply-templates select="./PurchasedGoods"/>
<xsl:apply-templates select="./Amount"/>
</xsl:template>
<xsl:template match="Type">
<fo:block text-align="left" font-family="Arial">
<fo:block>Type : <xsl:value-of select="."/></fo:block>
</fo:block>
</xsl:template>
<xsl:template match="Number">
<fo:block text-align="left" font-family="Arial">
<fo:block>Number : <xsl:value-of select="."/></fo:block>
</fo:block>
</xsl:template>
<xsl:template match="Country">
<fo:block text-align="left" font-family="Arial">
<fo:block>Country : <xsl:value-of select="."/></fo:block>
</fo:block>
</xsl:template>
<xsl:template match="Name">
<fo:block text-align="left" font-family="Arial">
<fo:block>Name : <xsl:value-of select="."/></fo:block>
</fo:block>
</xsl:template>
<xsl:template match="PurchasedGoods">
<fo:block text-align="left" font-family="Arial">
<fo:block>Purchased Goods : <xsl:value-of select="."/></fo:block>
</fo:block>
</xsl:template>
<xsl:template match="Amount">
<fo:block text-align="left" font-family="Arial">
<fo:block>Amount : <xsl:value-of select="."/></fo:block>
</fo:block>
</xsl:template>
</xsl:stylesheet>
我得到的 PDF 输出如下
Type : ABC
Number : 001
Country : USA
Name : John Doe
Country : China
Name : Stuart Lim
Purchased Goods : Category A
Purchased Goods : Category B
Amount : 123456
Amount : 987654
但是,我想要输出如下。
Type : ABC
Number : 001
Country : USA
Name : John Doe
Purchased Goods : Category A
Amount : 123456
Country : China
Name : Stuart Lim
Purchased Goods : Category B
Amount : 987654
在不改变XML结构的情况下可以做到以上几点。
谢谢
【问题讨论】: