如果我在 XML 文件中有架构位置,XSL FO 转换将不起作用



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

我的问题是:有没有办法忽略确定架构位置等的那几行代码???

提前谢谢你!

爪哇类:

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);
    }
}
如果使用

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>

如果您的 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) 的一部分,它只匹配元素名称而不匹配命名空间。

这个问题

每天问一次。只需搜索"XSLT 默认命名空间"。将元素放在命名空间中会更改其名称,并且转换只有在正确的命名空间中查找时才会找到它们。

最新更新