我需要在DB中存储XML数据,然后从DB检索XML并转换为PDF。为此,我将XSLT与Apache FOP一起使用。下面的代码可以很好地处理XML文件中的当前名称空间(我已经将名称空间从Books.xml文件复制并粘贴到Books.xsl)。问题是每当WebService被更新时,如果Books.xml文件的命名空间被改变,那么我的Books。使用Apache FOP生成PDF时,使用旧命名空间的xsl文件会产生异常。
注:如果我更新我的书。根据XML文件中的新名称空间,PDF生成适用于新的XML内容,但不适用于旧的Books.xml内容。
示例: Webservice更新前Books.xml中的旧命名空间如下
<Book xmlns="http://book.com/one/1.1/" xmlns:ns2="http://test2.com/one/1.1/" xmlns:ns3="http://test3.com/one/1.1/" >
并假设XML文件中的新名称空间如下
<Book xmlns="http://book.com/one/1.2/" xmlns:ns2="http://test2.com/one/1.2/" xmlns:ns3="http://test3.com/one/1.2/" >
我的XML内容(BOOK.xml)
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Book xmlns="http://book.com/one/1.2/" xmlns:ns2="http://test2.com/one/1.2/" xmlns:ns3="http://test3.com/one/1.2/" >
<Title>Java</Title>
<ISBN>123</ISBN>
<Author>Test</Author>
<BookImage>
<ns2:ImageName>images1.jpeg</ns2:ImageName>
<ns2:ImageContent>/aN2r9pT8HwKXpmvvmt2ifpz5xvmt2ifpz5xukZHBb9Wn4FL0xwW/VpD=</ns2:ImageContent>
</BookImage>
<BookImage>
<ns2:ImageName>images2.jpeg</ns2:ImageName>
<ns2:ImageContent>/aN2r9pT8HwKXpmvvmt2ifpz5xvmt2ifpz5xukZHBb9Wn4FL0xwW/VpD=</ns2:ImageContent>
</BookImage>
</Book>
我的xsl内容(Books.xsl)
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:b="http://book.com/one/1.1/" xmlns:ns2="http://test2.com/one/1.1/" xmlns:ns3="http://test3.com/one/1.1/">
<xsl:template match="b:Book">
<fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format">
<fo:layout-master-set>
<fo:simple-page-master master-name="ex"
page-height="29.7cm" page-width="21cm" margin-top="2cm"
margin-bottom="2cm" margin-left="2cm" margin-right="2cm">
<fo:region-body />
</fo:simple-page-master>
</fo:layout-master-set>
<fo:page-sequence master-reference="ex">
<fo:flow flow-name="xsl-region-body">
<fo:block space-after="15mm">
</fo:block>
<fo:block font-size="16pt" font-weight="bold" space-after="5mm">
Book Details
</fo:block>
<fo:block font-size="10pt">
<fo:table table-layout="fixed" width="100%"
border-collapse="collapse" border="solid 1px black">
<fo:table-body>
<fo:table-row>
<fo:table-cell border="solid 1px black" padding="1mm">
<fo:block font-weight="bold">Title :</fo:block>
</fo:table-cell>
<fo:table-cell padding="1mm" border="solid 1px black">
<fo:block>
<xsl:value-of select="b:Title" />
</fo:block>
</fo:table-cell>
</fo:table-row>
<fo:table-row>
<fo:table-cell border="solid 1px black" padding="1mm">
<fo:block font-weight="bold">ISBN :
</fo:block>
</fo:table-cell>
<fo:table-cell padding="1mm" border="solid 1px black">
<fo:block>
<xsl:value-of select="b:ISBN" />
</fo:block>
</fo:table-cell>
</fo:table-row>
<fo:table-row>
<fo:table-cell border="solid 1px black" padding="1mm">
<fo:block font-weight="bold">Author :</fo:block>
</fo:table-cell>
<fo:table-cell padding="1mm" border="solid 1px black">
<fo:block>
<xsl:value-of select="b:Author" />
</fo:block>
</fo:table-cell>
</fo:table-row>
<fo:table-row>
<fo:table-cell border="solid 1px black" padding="1mm">
<fo:block font-weight="bold">Image Names :</fo:block>
</fo:table-cell>
<fo:table-cell padding="1mm" border="solid 1px black">
<fo:block>
<xsl:for-each select="b:BookImage">
<xsl:value-of select="ns2:ImageName"/>
<xsl:text> </xsl:text>
</xsl:for-each>
</fo:block>
</fo:table-cell>
</fo:table-row>
</fo:table-body>
</fo:table>
</fo:block>
</fo:flow>
</fo:page-sequence>
</fo:root>
</xsl:template>
</xsl:stylesheet>
, Java代码如下
import java.io.File;
import java.io.InputStream;
import java.io.OutputStream;
import javax.xml.transform.Result;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.sax.SAXResult;
import javax.xml.transform.stream.StreamSource;
import org.apache.fop.apps.FOUserAgent;
import org.apache.fop.apps.Fop;
import org.apache.fop.apps.FopFactory;
import org.apache.fop.apps.MimeConstants;
/**
* This class demonstrates the conversion of an XML file to PDF using
* JAXP (XSLT) and FOP (XSL-FO).
*/
public class ExampleXML2PDF {
/**
* Main method.
* @param args command-line arguments
*/
public static void main(String[] args) {
ExampleXML2PDF exampleXML2PDF = new ExampleXML2PDF();
exampleXML2PDF.xmlToPDF();
}
public void xmlToPDF(){
try {
System.out.println("FOP ExampleXML2PDFn");
System.out.println("Preparing...");
System.out.println("Transforming...");
// configure fopFactory as desired
final FopFactory fopFactory = FopFactory.newInstance();
FOUserAgent foUserAgent = fopFactory.newFOUserAgent();
// configure foUserAgent as desired
InputStream xslInputStream = getClass().getResourceAsStream("/resources/Book.xsl");
InputStream xmlInputStream = getClass().getResourceAsStream("/resources/Book.xml");
// Setup output
OutputStream out = new java.io.FileOutputStream(new File("bin/resources/BookXML2PDF.pdf"));
out = new java.io.BufferedOutputStream(out);
try {
// Construct fop with desired output format
Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF, foUserAgent, out);
// Setup XSLT
TransformerFactory factory = TransformerFactory.newInstance();
Transformer transformer = factory.newTransformer(new StreamSource(xslInputStream));
// Setup input for XSLT transformation
Source src = new StreamSource(xmlInputStream);
// Resulting SAX events (the generated FO) must be piped through to FOP
Result res = new SAXResult(fop.getDefaultHandler());
// Start XSLT transformation and FOP processing
transformer.transform(src, res);
} finally {
out.close();
}
System.out.println("Success!");
} catch (Exception e) {
e.printStackTrace(System.err);
System.exit(-1);
}
}
}
请提前帮助。
谢谢,RaoPotla
如果您提前知道可能的命名空间集是什么,并且它是一个很小的集合,您可以这样做:
<xsl:template match="b:Book | ns1:Book | ns2:Book">
...
<xsl:value-of select="b:Title | ns1:Title | ns2:Title" />
如果您必须多次这样做,这会变得有点混乱,但它的优点是它不会接受来自任何名称空间的元素(例如,没有名称空间的元素)。这可能不是一个优势,这取决于你的情况。
如果您事先不知道所有可能的名称空间是什么,您可以使用更通用的解决方案,它完全忽略名称空间:
<xsl:template match="*[local-name() = 'Book']">
...
<xsl:value-of select="*[local-name() = 'Title']" />