Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xslt/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
当名称空间是动态的时,如何在XSLT文件中提及名称空间?_Xslt_Namespaces_Xslt 1.0_Xml Namespaces_Apache Fop - Fatal编程技术网

当名称空间是动态的时,如何在XSLT文件中提及名称空间?

当名称空间是动态的时,如何在XSLT文件中提及名称空间?,xslt,namespaces,xslt-1.0,xml-namespaces,apache-fop,Xslt,Namespaces,Xslt 1.0,Xml Namespaces,Apache Fop,我需要将XML数据存储在DB中,然后从DB中检索XML并转换为PDF。为此,我将XSLT与ApacheFop结合使用。下面的代码可以很好地处理XML文件中的当前名称空间(我已将Books.xml文件中的名称空间复制并粘贴到Books.xsl。问题是,每当更新Web服务时,如果Books.xml文件的名称空间发生更改,则使用Apache FOP生成PDF时,具有旧名称空间的Books.xsl文件将出现异常 注意:如果我根据XML文件中的新名称空间更新Books.xsl文件名称空间,则PDF生成适用

我需要将XML数据存储在DB中,然后从DB中检索XML并转换为PDF。为此,我将XSLT与ApacheFop结合使用。下面的代码可以很好地处理XML文件中的当前名称空间(我已将Books.xml文件中的名称空间复制并粘贴到Books.xsl。问题是,每当更新Web服务时,如果Books.xml文件的名称空间发生更改,则使用Apache FOP生成PDF时,具有旧名称空间的Books.xsl文件将出现异常

注意:如果我根据XML文件中的新名称空间更新Books.xsl文件名称空间,则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/" >
任何帮助都将被提前感谢

谢谢,
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']" />

...
…它符合我的要求。LarsH非常感谢。
<?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>&#160;</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>
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 ExampleXML2PDF\n");
            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);
        }

    }

}
<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']" />