如何从 Java 应用程序使用 XSLT 3.0? [英] How to use XSLT 3.0 from a Java application?

查看:36
本文介绍了如何从 Java 应用程序使用 XSLT 3.0?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用来处理 XSLTXML 文件的通用 java 代码是:

The general java code i use to process XSLT and XML files are :

public static final String transformXmlDocument(String inputXmlString,
            File xsltFile) {

        TransformerFactory factory = TransformerFactory.newInstance();
        StreamSource xslt = new StreamSource(xsltFile);

        StreamSource text = new StreamSource(new StringReader(inputXmlString));
        StringWriter writer = new StringWriter();
        StreamResult textOP = new StreamResult(writer);

        try {
            Transformer transformer = factory.newTransformer(xslt);
            transformer.transform(text, textOP);
        } catch (TransformerConfigurationException e) {
            e.printStackTrace();
        } catch (TransformerException e2) {
            e2.printStackTrace();
        }
        String results = writer.toString();

        return results;
}

我必须处理 3.0 版本的 XSLT 才能使用以下功能:

I have to process an XSLT of 3.0 version to use the following function :

解析-xml-fragment()

parse-xml-fragment()

对于这个版本的 XSLT 会抛出错误说:

It throws error for this version of XSLT saying:

parse-xml-fragment() 未作为函数找到

parse-xml-fragment() not found as a function

我的输入 XML :

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<data>
  <![CDATA[<pi>hi</pi>]]>
</data>

XSLT 代码:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
     xmlns:xs="http://www.w3.org/2001/XMLSchema"
     xmlns:data="http://example.com/data"
     xmlns:text="http://exselt.net/text"
     xmlns:err="http://www.w3.org/2005/xqt-errors"
     exclude-result-prefixes="xs xsl data text err"
     version="3.0">

<xsl:output indent="yes"/>

     <xsl:template match="/">
         <xsl:variable name="sample">
            <xsl:copy-of select="parse-xml-fragment('&lt;gi&gt;surface&lt;/gi&gt;&lt;gi&gt;surface&lt;/gi&gt;&lt;gi&gt;surface&lt;/gi&gt;')" />
         </xsl:variable>
         <final>
            <xsl:copy-of select="data/pi"/>
             <xsl:for-each select="$sample/gi">
                 <pi><xsl:value-of select="."/></pi>
            </xsl:for-each> 
         </final>
     </xsl:template>

</xsl:stylesheet>

预期输出:

<final>
    <pi>hi</pi>
    <pi>surface</pi>
    <pi>surface</pi>
    <pi>surface</pi>
  </final>

谁能提供解决方案?

推荐答案

您需要确保 Saxon 9.8 HE 或 PE 或 EE 在您的课程路径中,HE 可在 SourceforgeMaven,来自 saxonica.com.请参阅 http://saxonica.com/html/documentation/about/installationjava/installingjava.html 以及 http://saxonica.com/html/documentation/using-xsl/embedding/jaxp-transformation.html 推荐,一旦你安装了一个特定的版本,使用例如http://saxonica.com/html/documentation/javadoc/net/sf/saxon/TransformerFactoryImpl.html 直接而不是依赖于 JAXP 类加载器机制,因此假设您安装了 Saxon 9.8 HE,您可以替换

You will need to make sure Saxon 9.8 HE or PE or EE is on your class path, HE is available on Sourceforge and Maven, the commercial editions PE and EE from saxonica.com. See http://saxonica.com/html/documentation/about/installationjava/installingjava.html and also http://saxonica.com/html/documentation/using-xsl/embedding/jaxp-transformation.html which recommend, once you have installed a particular edition, to use e.g. http://saxonica.com/html/documentation/javadoc/net/sf/saxon/TransformerFactoryImpl.html directly instead of relying on the JAXP class loader mechanism, so assuming you have Saxon 9.8 HE installed you can replace

    TransformerFactory factory = TransformerFactory.newInstance();

    TransformerFactory factory = new net.sf.saxon.TransformerFactoryImpl();

这篇关于如何从 Java 应用程序使用 XSLT 3.0?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆