Java XML Transform transformXmlByXslt(StreamSource source, URI xslFile)

Here you can find the source of transformXmlByXslt(StreamSource source, URI xslFile)

Description

Transforms a xml file by xslt and returns the result as string.

License

Open Source License

Parameter

Parameter Description
source The xml file to transform.
xslFile The xsl file.

Return

The Result of the transformation as String object.

Declaration

static ByteArrayOutputStream transformXmlByXslt(StreamSource source, URI xslFile)
        throws TransformerException, IOException 

Method Source Code


//package com.java2s;
/*/*from w  w  w .  ja v  a 2 s  . c  o  m*/
 * (c) Kitodo. Key to digital objects e. V. <contact@kitodo.org>
 *
 * This file is part of the Kitodo project.
 *
 * It is licensed under GNU General Public License version 3 or later.
 *
 * For the full copyright and license information, please read the
 * GPL3-License.txt file that was distributed with this source code.
 */

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.net.URI;

import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;

public class Main {
    /**
     * Transforms a xml file by xslt and returns the result as string.
     *
     * @param source  The xml file to transform.
     * @param xslFile The xsl file.
     * @return The Result of the transformation as String object.
     */
    static ByteArrayOutputStream transformXmlByXslt(StreamSource source, URI xslFile)
            throws TransformerException, IOException {
        StreamSource xsltSource = new StreamSource(xslFile.getPath());
        TransformerFactory factory = TransformerFactory.newInstance();
        Transformer transformer = factory.newTransformer(xsltSource);
        try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) {
            StreamResult result = new StreamResult(outputStream);
            transformer.transform(source, result);
            return outputStream;
        }
    }
}

Related

  1. transformToOutputStream(Node node, OutputStream os, boolean omitXmlDeclaration)
  2. transformToString(Source xmlSource, Source xslSource)
  3. transformViaXslt(String xml, String xslFile)
  4. transformXml(final StreamSource xslSrc, final StreamSource docSrc, final Map params, final URIResolver resolver)
  5. transformXML(Reader xml, Reader xsl)
  6. write_plain_content_tag(String name, String content, AttributesImpl atts, TransformerHandler hd)
  7. writeByTransformer(Node node, Writer writer, int indent)