日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 >

xml转pdf xsl取值问题 语法问题

發(fā)布時間:2023/12/15 37 豆豆
生活随笔 收集整理的這篇文章主要介紹了 xml转pdf xsl取值问题 语法问题 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

將xml文件轉(zhuǎn)為pdf 以及xsl取值問題 語法問題

文章目錄

  • 將xml文件轉(zhuǎn)為pdf 以及xsl取值問題 語法問題
  • 前言
  • 一、轉(zhuǎn)化思路
  • 二、使用步驟
    • 1.引入maven依賴
    • 2.創(chuàng)建xsl文件
  • 總結(jié)


前言


一、轉(zhuǎn)化思路

需要創(chuàng)建一個.xsl文件作為pdf模板,同時xsl文件中讀取xml中的數(shù)據(jù)

二、使用步驟

1.引入maven依賴

代碼如下(示例):

<dependency><groupId>javax.xml.ws</groupId><artifactId>jaxws-api</artifactId><scope>test</scope> </dependency> <dependency><groupId>org.apache.xmlgraphics</groupId><artifactId>fop</artifactId><version>2.1</version> </dependency>

2.創(chuàng)建xsl文件

xls模板生成的圖的大致是這樣的,我做了部分截取,同時貼上的xsl文件也只是取了一部分
fo:table標(biāo)簽理解為table就好,fo:table-row就是行, 根據(jù)自己的需求加就行這個是簡單

含有一些隱私信息我做了簡化:

<?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format"version="1.0"><xsl:output encoding="UTF-8" indent="yes" method="xml" standalone="no" omit-xml-declaration="no"/><xsl:template match="InvoiceXML"><fo:root language="EN"><fo:layout-master-set><fo:simple-page-master master-name="A4-portrail" page-height="297mm" page-width="210mm" margin-top="5mm"margin-bottom="5mm" margin-left="5mm" margin-right="5mm"><fo:region-body margin-top="25mm" margin-bottom="20mm"/><fo:region-before region-name="xsl-region-before" extent="25mm" display-align="before"precedence="true"/><fo:region-after region-name="xsl-region-after" extent="25mm" display-align="before"precedence="true"/></fo:simple-page-master></fo:layout-master-set><fo:page-sequence master-reference="A4-portrail"><fo:static-content flow-name="xsl-region-before"><fo:block border-top-width="2pt" border-top-style="solid" border-top-color="#d6d6d6"text-align="center"></fo:block></fo:static-content><fo:flow flow-name="xsl-region-body" border-collapse="collapse" reference-orientation="0"><fo:list-block><fo:list-item relative-align="before"><fo:list-item-label end-indent="label-end()"><fo:block><fo:external-graphic src="file:///C:/Users/test/Downloads/logo.png" width="10mm"height="10mm" content-width="scale-to-fit"/></fo:block></fo:list-item-label><fo:list-item-body start-indent="body-start()"><fo:block margin-left="2mm" margin-top="4mm" font-size="15pt">Invoice</fo:block></fo:list-item-body></fo:list-item></fo:list-block><fo:table table-layout="fixed" width="100%" font-size="8pt"><fo:table-column column-width="proportional-column-width(20)"/><fo:table-column column-width="proportional-column-width(25)"/><fo:table-column column-width="proportional-column-width(20)"/><fo:table-column column-width="proportional-column-width(35)"/><fo:table-body><fo:table-row><fo:table-cell text-align="left" display-align="center" padding-left="2mm"><fo:block>ID</fo:block></fo:table-cell><fo:table-cell text-align="left" display-align="center" padding-left="2mm"><fo:block><!-- 取值的意思就是按照元素從父元素到子元素--><xsl:value-of select="invoiceDetails/id"/></fo:block></fo:table-cell><fo:table-cell text-align="left" display-align="center" padding-left="2mm"><fo:block>Buyer</fo:block></fo:table-cell><fo:table-cell text-align="left" display-align="center" padding-right="2mm"><fo:block><xsl:value-of select="buyer/details/buyerName"/></fo:block></fo:table-cell></fo:table-row><fo:table-row><fo:table-cell text-align="left" display-align="center" padding-left="2mm"><fo:block>End Date</fo:block></fo:table-cell><fo:table-cell text-align="left" display-align="center" padding-left="2mm"><fo:block><xsl:value-of select="concat(substring-after(translate(invoiceDetails/worker/endDate,'-','/'),'/'),concat('/',substring-before(translate(invoiceDetails/worker/endDate,'-','/'),'/')))"/></fo:block></fo:table-cell><fo:table-cell text-align="left" display-align="center" padding-left="2mm"><fo:block>Submitted By</fo:block></fo:table-cell><fo:table-cell text-align="left" display-align="center" padding-right="2mm"><fo:block><xsl:value-of select="costCenterAmount[@type='adjustment']/amount"/></fo:block></fo:table-cell></fo:table-row></fo:table-body></fo:table><fo:table table-layout="fixed" width="100%" font-size="8pt" margin-top="10mm"><fo:table-column column-width="proportional-column-width(15)"/><fo:table-column column-width="proportional-column-width(10)"/><fo:table-column column-width="proportional-column-width(10)"/><fo:table-column column-width="proportional-column-width(10)"/><fo:table-body><fo:table-row font-size="10pt" background-color="#d6d6d6" border-bottom-width="0.2mm"border-bottom-style="solid" border-bottom-color="black"><fo:table-cell text-align="left" display-align="center" padding-left="2mm"><fo:block>Cost Allocation (EUR)</fo:block></fo:table-cell><fo:table-cell text-align="left" display-align="center" padding-left="2mm"><fo:block></fo:block></fo:table-cell><fo:table-cell text-align="left" display-align="center" padding-left="2mm"><fo:block></fo:block></fo:table-cell><fo:table-cell text-align="left" display-align="center" padding-left="2mm"><fo:block></fo:block></fo:table-cell></fo:table-row><fo:table-row border-bottom-width="0.001mm" border-bottom-style="solid"border-bottom-color="white"><fo:table-cell text-align="left" display-align="center" padding-left="2mm"><fo:block>Cost Center</fo:block></fo:table-cell><fo:table-cell text-align="right" display-align="center" padding-left="2mm"><fo:block>Detail Amount</fo:block></fo:table-cell><fo:table-cell text-align="right" display-align="center" padding-left="2mm"><fo:block>Adjustment</fo:block></fo:table-cell><fo:table-cell text-align="right" display-align="center" padding-left="2mm"><fo:block>Amount</fo:block></fo:table-cell></fo:table-row><xsl:for-each select="accounting/costCenterAllocation/costCenter"><fo:table-row border-bottom-width="0.1mm" border-bottom-style="solid"border-bottom-color="black"><fo:table-cell text-align="left" display-align="center" padding-left="2mm"><fo:block><xsl:value-ofselect="//lineItemDetails/lineItemDetails/timeSheets/timeSheetHoursWorked/billable/costCenterName"/></fo:block></fo:table-cell><fo:table-cell text-align="right" display-align="center" padding-left="2mm"><fo:block><!-- <costCenterAmount type="detailAmount" fgLabel="Detail Amount">--><xsl:value-of select="costCenterAmount[@type='detailAmount']/amount"/></fo:block></fo:table-cell><fo:table-cell text-align="right" display-align="center" padding-left="2mm"><fo:block><!-- <costCenterAmount type="adjustment" fgLabel="Adjustment">--><xsl:value-of select="costCenterAmount[@type='adjustment']/amount"/></fo:block></fo:table-cell><fo:table-cell text-align="right" display-align="center" padding-left="2mm"><fo:block><!-- <costCenterAmount type="amount" fgLabel="Amount">--><xsl:value-of select="costCenterAmount[@type='amount']/amount"/></fo:block></fo:table-cell></fo:table-row></xsl:for-each></fo:table-body></fo:flow></fo:page-sequence></fo:root></xsl:template> </xsl:stylesheet>

java的代碼如下

package com.demo.invoice; import org.apache.fop.apps.*;import javax.xml.transform.*; import javax.xml.transform.sax.SAXResult; import javax.xml.transform.stream.StreamSource; import java.io.File; import java.io.IOException; import java.io.OutputStream;/*** @author shenyuan* @date 2022-03-08 5:38*/ public class SwissDataConvertPdf {private static String INPUT_PATH="src/main/resources/input";private static String OUTPUT_PATH="src/main/resources/output";public static void main(String [] args){try{convertPdf();}catch (Exception e){e.printStackTrace();}}public static void convertPdf () throws IOException, FOPException, TransformerException {// 這個定義好的模板文件File xsltFile = new File( INPUT_PATH+"/invoice_template_Swiss.xsl");long length = xsltFile.length();// 這個就是那個xml數(shù)據(jù)文件StreamSource xmlSource = new StreamSource(new File(INPUT_PATH + "/Swiss_data.xml"));FopFactory fopFactory = FopFactory.newInstance(new File(".").toURI());FOUserAgent foUserAgent = fopFactory.newFOUserAgent();// 輸出的pdfOutputStream out;out = new java.io.FileOutputStream(OUTPUT_PATH + "/ebay_invoice_Swiss.pdf");try {Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF, foUserAgent, out);TransformerFactory factory = TransformerFactory.newInstance();Transformer transformer = factory.newTransformer(new StreamSource(xsltFile));Result res = new SAXResult(fop.getDefaultHandler());transformer.transform(xmlSource, res);} finally {out.close();}} }

總結(jié)

可能大家對xls取值會有一些疑問我舉例子說名

//這是xsl中取值的 可以去我貼的xls代碼中找<xsl:value-of select="invoiceDetails/id"/> //對應(yīng)的 xml元素是這樣的 <invoiceDetails label="Invoice Details"><id label="ID">123455</id> </invoiceDetails> //大家又有一個問題假如這樣的層級有好多個呢 <invoiceDetails label="Invoice Details"><id label="ID">123455</id><id label="IDS">23444</id> </invoiceDetails> //xsl提供了這樣的語法,以此類推<xsl:value-of select="invoiceDetails/id[@label='IDS']"/> //可能又有這樣的問題,表格中的行數(shù)是動態(tài)的 我需要查到所有的元素拼接到table上 //沒關(guān)系 xsl提供了 <xsl:for-each> 去我貼的xsl代碼中查找 //accounting/costCenterAllocation/costCenter 是外層元素<xsl:for-each select="accounting/costCenterAllocation/costCenter">//這是接著for-foreach元素的子元素 意思就是遍歷每個 accounting/costCenterAllocation/costCenter元素中的 每個costCenterAmount<xsl:value-of select="costCenterAmount[@type='detailAmount']/amount"/>//如果有的行數(shù)據(jù)不是在for-each遍歷的子元素下呢// 看以下的代碼 lineItemDetail和accounting平級的 //lineItemDetail就能跳出到 accounting從lineItemDetail元素開始查找<xsl:value-of select="//lineItemDetails/lineItemDetails/timeSheets/timeSheetHoursWorked/billable/costCenterName"/>//關(guān)于對取到的值進(jìn)行處理 xsl數(shù)值函數(shù)與字符串函數(shù) 我只舉一個例子<xsl:value-of select="invoiceDetails/worker/endDate"/>//把取到的值中的-轉(zhuǎn)換為/,用字符串轉(zhuǎn)換函數(shù)這樣寫,支持函數(shù)嵌套<xsl:value-of select="translate(invoiceDetails/worker/endDate,'-','/')"/>//最后一個問題 xsl配置 例如<xsl:template match="InvoiceXML">這個別忘了InvoiceXML對應(yīng)xml的根元素

總結(jié)

以上是生活随笔為你收集整理的xml转pdf xsl取值问题 语法问题的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。