Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java XSLT FO程序,将XML数据结构化为PDF_Java_Xml_Xslt_Xsl Fo - Fatal编程技术网

Java XSLT FO程序,将XML数据结构化为PDF

Java 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

我是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>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>