Java XSLT FO程序,将XML数据结构化为PDF
我是XSLT新手,需要将XML文件转换为PDF。我有一个JAVA程序,它使用XSLT FO文件将XML转换为PDF 下面是我的XMLJava XSLT FO程序,将XML数据结构化为PDF,java,xml,xslt,xsl-fo,Java,Xml,Xslt,Xsl Fo,我是XSLT新手,需要将XML文件转换为PDF。我有一个JAVA程序,它使用XSLT FO文件将XML转换为PDF 下面是我的XML <PDFReport xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <Type>ABC</Type> <Number>001</Number> <CustomerDetails> <Country&g
<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>
但是,我希望输出如下
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结构的情况下完成上述操作
谢谢您得到了当前的结果,因为您正在指导按照
xsl:apply templates的顺序处理元素
:
<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>
此
将按文档顺序处理客户详细信息的子节点。(在您显示的样式表中没有CustomerDetails
的模板,情况就是这样。)
或者,如果Country
始终是CustomerDetails
的第一个子项,则可以在fo:block
上为Country
指定前面的空格
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
<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="CustomerDetails">
<fo:block space-before="1em">
<xsl:apply-templates />
</fo:block>
</xsl:template>