高效的XSLT管道,带参数,在Java中 [英] Efficient XSLT pipeline, with params, in Java

查看:84
本文介绍了高效的XSLT管道,带参数,在Java中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题的最高答案描述了一种在Java中实现高效的XSLT管道的技术:

The top answer to this question describes a technique to implement an efficient XSLT pipeline in Java:

Java中的高效XSLT管道(或将结果重定向到源)

不幸的是,虽然Transformer似乎暴露了一个用于设置XSLT参数的API,但这似乎没有任何效果。例如,我有以下代码:

Unfortunately, while Transformer seems to expose an API for setting XSLT parameters, this does not seem to have any effect. For example, I have the following code:

Transformer.java

Transformer.java

import javax.xml.transform.sax.SAXTransformerFactory;
import javax.xml.transform.Templates;
import javax.xml.transform.sax.TransformerHandler; 
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
import javax.xml.transform.sax.SAXResult;
import javax.xml.transform.Transformer;
import java.io.File;
public class MyTransformer {
    public static void main(String[] args) throws javax.xml.transform.TransformerConfigurationException, javax.xml.transform.TransformerException{
        SAXTransformerFactory stf = (SAXTransformerFactory)TransformerFactory.newInstance();

        // These templates objects could be reused and obtained from elsewhere.
        Templates templates1 = stf.newTemplates(new StreamSource( new File("MyStylesheet1.xslt")));
        Templates templates2 = stf.newTemplates(new StreamSource(new File("MyStylesheet2.xslt")));

        TransformerHandler th1 = stf.newTransformerHandler(templates1);
        TransformerHandler th2 = stf.newTransformerHandler(templates2);

        th1.setResult(new SAXResult(th2));
        th2.setResult(new StreamResult(System.out));

        Transformer t = stf.newTransformer();

            //SETTING PARAMETERS HERE
        t.setParameter("foo","this is from param 1");
        t.setParameter("bar","this is from param 2");

        t.transform(new StreamSource(new File("in.xml")), new SAXResult(th1));

        // th1 feeds th2, which in turn feeds System.out.
    }
}

MyStylesheet1.xslt

MyStylesheet1.xslt

<?xml version="1.0"?>
<stylesheet xmlns="http://www.w3.org/1999/XSL/Transform"  xmlns:foo="urn:foo" version="1.0">
    <output method="xml"/>

    <param name="foo"/>

    <template match="@*|node()">
        <copy>
            <apply-templates select="@*|node()"/>
        </copy>
    </template>

    <template match="foo:my/foo:hello">
        <copy>
            <foo:world>
                foo is : <value-of select="$foo"/>
            </foo:world>
        </copy>

    </template>
</stylesheet>

MyStylesheet2.xslt

MyStylesheet2.xslt

<?xml version="1.0"?>
<stylesheet xmlns="http://www.w3.org/1999/XSL/Transform" xmlns:foo="urn:foo" version="1.0">
    <output method="xml"/>

    <param name="bar"/>

    <template match="@*|node()">
        <copy>
            <apply-templates select="@*|node()"/>
        </copy>
    </template>

    <template match="foo:my/foo:hello/foo:world">
        <copy>
            <apply-templates select="@*|node()"/>

            <attribute name="attr">
                <value-of select="$bar"/>
            </attribute>
        </copy>

    </template>
</stylesheet>

in.xml

<my xmlns="urn:foo">
    <hello/>
</my>

其中给出以下输出:

<?xml version="1.0" encoding="UTF-8"?><my xmlns="urn:foo">
        <hello><foo:world xmlns:foo="urn:foo">foo is : </foo:world></hello>
</my>

正如你可以看到foo:world / @ attr为空,并且foo的文本内容:world说foo是:。预期的行为是它们应该已经填充了传递给setParameter方法的参数。

As you can see foo:world/@attr is empty, and the text contents of foo:world says "foo is:". The expected behaviour is that they should have been populated with the parameters passed into the setParameter method.

有没有办法使用这种技术设置XSL转换参数。如果没有,可以有人推荐一种替代技术来在Java中有效地转换样式表,以便XSLT参数也可以设置?

Is there a way to set XSL transformation parameters using this technique. If not, could anyone recommend an alternative technique for transforming stylesheets efficiently in Java, such that XSLT parameters may also be set?

推荐答案

问题是每个TransformerHandler都有一个与之关联的单独的Transformer。你的第二个模板有一个问题,但是这是一个例子,我猜这并不重要。你想要:

The problem is that each TransformerHandler has a separate Transformer associated with it. There's a problem with your 2nd template, but as this is an example I guess that doesn't matter. You want:

//SETTING PARAMETERS HERE
th1.getTransformer().setParameter("foo","this is from param 1");
th2.getTransformer().setParameter("bar","this is from param 2");

请注意,您也不需要创建第三个变压器,您只需启动变换链th1.getTransformer()

Note that you also don't need to create a 3rd transformer you can just start the transform chain on th1.getTransformer()

这篇关于高效的XSLT管道,带参数,在Java中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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