[英]How to print source XML into a PDF using XSL-FO?
美好的一天! 我需要編寫一個xsl-fo模板,但我無法訪問源XML。 有沒有辦法將源XML打印成PDF,以便我可以從PDF中復制它然后粘貼到文件中? 它應該與源xml包含屬性的結構相同。 請問怎么做? 先感謝您! Vojtech
編輯:我有一個Web界面,我可以粘貼我的模板,生成PDF。 但我並不確切知道用作數據源的XML的結構是什么。 所以我需要編寫另一個模板來讀取輸入XML(元素,屬性,結構)並將其寫入PDF。 我想復制PDF的內容,然后將其保存到file.xml中,以便我可以研究它。
這是另一個大大簡化的選項; 只打印整個XML的副本。
例:
XML輸入
<doc attr="test">
<a>Lorem ipsum dolor sit amet...</a>
<b>
<c>Lorem ipsum dolor sit amet...</c>
<d>
<e attr="another test"/>
<f>Lorem ipsum dolor sit amet...</f>
</d>
</b>
</doc>
XSLT 1.0
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="/">
<fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format">
<fo:layout-master-set>
<fo:simple-page-master master-name="my-page" page-width="8.5in" page-height="11in">
<fo:region-body margin="1in" margin-top="1.5in" margin-bottom="1.5in"/>
</fo:simple-page-master>
</fo:layout-master-set>
<fo:page-sequence master-reference="my-page">
<fo:flow flow-name="xsl-region-body" font-family="monospace">
<fo:block white-space-collapse="false" white-space-treatment="preserve" linefeed-treatment="preserve">
<xsl:text disable-output-escaping="yes">
<![CDATA[
</xsl:text>
<xsl:copy-of select="/*"/>
<xsl:text disable-output-escaping="yes">
]]>
</xsl:text>
</fo:block>
</fo:flow>
</fo:page-sequence>
</fo:root>
</xsl:template>
</xsl:stylesheet>
XSL-FO輸出
<fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format">
<fo:layout-master-set>
<fo:simple-page-master master-name="my-page" page-width="8.5in" page-height="11in">
<fo:region-body margin="1in" margin-top="1.5in" margin-bottom="1.5in"/>
</fo:simple-page-master>
</fo:layout-master-set>
<fo:page-sequence master-reference="my-page">
<fo:flow flow-name="xsl-region-body" font-family="monospace">
<fo:block white-space-collapse="false" white-space-treatment="preserve" linefeed-treatment="preserve">
<![CDATA[
<doc attr="test">
<a>Lorem ipsum dolor sit amet...</a>
<b>
<c>Lorem ipsum dolor sit amet...</c>
<d>
<e attr="another test"/>
<f>Lorem ipsum dolor sit amet...</f>
</d>
</b>
</doc>
]]>
</fo:block>
</fo:flow>
</fo:page-sequence>
</fo:root>
PDF輸出 (Apache FOP)
在這里,您將找到一篇關於復制源XML的優秀文章。
我剛剛把它包裝成一個簡單的XSL-FO存根,所以完全歸功於原作者 。 這是完整的片段:
<?xml version="1.0" encoding="iso-8859-1"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:fo="http://www.w3.org/1999/XSL/Format">
<xsl:template match="/">
<fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format">
<fo:layout-master-set>
<fo:simple-page-master master-name="main">
<fo:region-body margin="1in"/>
</fo:simple-page-master>
</fo:layout-master-set>
<fo:page-sequence master-reference="main">
<fo:flow flow-name="xsl-region-body">
<fo:block text-align="left">
<xsl:apply-templates mode="escape"/>
</fo:block>
</fo:flow>
</fo:page-sequence>
</fo:root>
</xsl:template>
<xsl:template match="*" mode="escape">
<!-- Begin opening tag -->
<xsl:text><</xsl:text>
<xsl:value-of select="name()"/>
<!-- Namespaces -->
<xsl:for-each select="namespace::*">
<xsl:text> xmlns</xsl:text>
<xsl:if test="name() != ''">
<xsl:text>:</xsl:text>
<xsl:value-of select="name()"/>
</xsl:if>
<xsl:text>='</xsl:text>
<xsl:call-template name="escape-xml">
<xsl:with-param name="text" select="."/>
</xsl:call-template>
<xsl:text>'</xsl:text>
</xsl:for-each>
<!-- Attributes -->
<xsl:for-each select="@*">
<xsl:text> </xsl:text>
<xsl:value-of select="name()"/>
<xsl:text>='</xsl:text>
<xsl:call-template name="escape-xml">
<xsl:with-param name="text" select="."/>
</xsl:call-template>
<xsl:text>'</xsl:text>
</xsl:for-each>
<!-- End opening tag -->
<xsl:text>></xsl:text>
<!-- Content (child elements, text nodes, and PIs) -->
<xsl:apply-templates select="node()" mode="escape" />
<!-- Closing tag -->
<xsl:text></</xsl:text>
<xsl:value-of select="name()"/>
<xsl:text>></xsl:text>
</xsl:template>
<xsl:template match="text()" mode="escape">
<xsl:call-template name="escape-xml">
<xsl:with-param name="text" select="."/>
</xsl:call-template>
</xsl:template>
<xsl:template match="processing-instruction()" mode="escape">
<xsl:text><?</xsl:text>
<xsl:value-of select="name()"/>
<xsl:text> </xsl:text>
<xsl:call-template name="escape-xml">
<xsl:with-param name="text" select="."/>
</xsl:call-template>
<xsl:text>?></xsl:text>
</xsl:template>
<xsl:template name="escape-xml">
<xsl:param name="text"/>
<xsl:if test="$text != ''">
<xsl:variable name="head" select="substring($text, 1, 1)"/>
<xsl:variable name="tail" select="substring($text, 2)"/>
<xsl:choose>
<xsl:when test="$head = '&'">&amp;</xsl:when>
<xsl:when test="$head = '<'">&lt;</xsl:when>
<xsl:when test="$head = '>'">&gt;</xsl:when>
<xsl:when test="$head = '"'">&quot;</xsl:when>
<xsl:when test="$head = "'"">&apos;</xsl:when>
<xsl:otherwise><xsl:value-of select="$head"/></xsl:otherwise>
</xsl:choose>
<xsl:call-template name="escape-xml">
<xsl:with-param name="text" select="$tail"/>
</xsl:call-template>
</xsl:if>
</xsl:template>
</xsl:stylesheet>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.