0

我的 XSL-FO 转换不适用于下面指定的 XML。似乎那些 xmlns 和 schemaLocation 正在困扰转换。

<book
xmlns="http://www.example.org/book"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.example.org/book book.xsd">

    <title>Something</title>
</book>

但是,如果我像下面这样重写我的 XML,则转换运行顺利,并且我在 XSL 中的所有 XPath 都运行良好。

<book>
    <title>Something</title>
</book>

我的问题是:是否有某种方法可以忽略确定架构位置等的那几行代码????

先感谢您!

Java类:

public static void generirajPDF() {
    try {

        // Setup directories
        File baseDir = new File(".");
        File outDir = new File(baseDir, "pdf");
        outDir.mkdirs();

        // Setup input and output files
        File xmlfile = new File(baseDir, "WebContent/AvtoSolaZ2.xml");
        File xsltfile = new File(baseDir, "WebContent/AvtoSolaZaposleniXSL.xsl");
        File pdffile = new File(outDir, "Test.pdf");

        // configure fopFactory as desired
        FopFactory fopFactory = FopFactory.newInstance();

        FOUserAgent foUserAgent = fopFactory.newFOUserAgent();
        // configure foUserAgent as desired

        // Setup output
        OutputStream out = new java.io.FileOutputStream(pdffile);
        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(xsltfile));

            // Set the value of a <param> in the stylesheet
            transformer.setParameter("versionParam", "2.0");

            // Setup input for XSLT transformation
            Source src = new StreamSource(xmlfile);

            // 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();
        }

        if (pdffile.toString().endsWith(".pdf"))
            Runtime.getRuntime().exec("rundll32 url.dll,FileProtocolHandler " + pdffile);
        else {
            Desktop desktop = Desktop.getDesktop();
            desktop.open(pdffile);
        }

        System.out.println("Konec");

    } catch (Exception e) {
        e.printStackTrace(System.err);
        System.exit(-1);
    }
}
4

3 回答 3

2

如果您使用 XSLT 2.0 处理器运行您的 XSLT,则设置

<xsl:stylesheet
  xmlns:xsl="http://www.w3.org/1999/XSL/Transformation"
  xpath-default-namespace="http://www.example.org/book"
  version="2.0">

在样式表的根元素上,您无需更改代码中的匹配模式和 XPath 表达式。

如果您使用 XSLT 1.0 处理器,则需要重写代码以适应命名空间,例如

<xsl:stylesheet
  xmlns:xsl="http://www.w3.org/1999/XSL/Transformation"
  xmlns:df="http://www.example.org/book"
  exclude-result-prefixes="df"
  version="1.0">

<xsl:template match="df:book">
  <xsl:value-of select="df:title"/>
</xsl:template>
于 2012-11-18T18:32:08.060 回答
0

如果您的 XML 输入是

<book  xmlns="http://www.example.org/book" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.example.org/book book.xsd">
<title>Something</title>
</book>

样式表是

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/*[local-name()='book']">
    <root>Matched the root with name space why not others</root>
</xsl:template>
</xsl:stylesheet>

你的输出会像

<?xml version="1.0" encoding="UTF-8"?>
<root>Matched the root with name space why not others
</root>

local-name() 函数是 XSLT 1.0 (http://www.xsltfunctions.com/xsl/fn_local-name.html) 的一部分,它只匹配没有命名空间的元素名称。

于 2012-11-18T18:12:47.590 回答
0

这个问题大约每天被问一次。只需搜索“XSLT 默认命名空间”。将元素放入命名空间会更改它们的名称,并且只有在正确的命名空间中才能找到它们。

于 2012-11-19T08:59:17.660 回答