使用参数从 Java 执行 XSLT 转换

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7681037/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 20:57:04  来源:igfitidea点击:

Execute XSLT Transform from Java with parameter

javaxslt

提问by schmimd04

I am executing an XSLT transform from within my java web application with no problems, as follows:

我正在从我的 java web 应用程序中执行 XSLT 转换,没有问题,如下所示:

Transformer transformer = TransformerFactory.newInstance().newTransformer(xslSource);
transformer.transform(xmlInput, xmlOutput);

In my XSLT transform I am now adding a call to the document()function to load the response from a RESTful web service:

在我的 XSLT 转换中,我现在添加对document()函数的调用以加载来自 RESTful Web 服务的响应:

<!-- do stuff -->
<xsl:variable name="url">
   http://server/service?id=<xsl:value-of select="@id"/>
</xsl:variable>
<xsl:call-template name="doMoreStuff">
   <xsl:with-param name="param1" select="document($url)/foo"/>
</xsl:call-template>

Ok cool, no problem. But now, I want to read the base URL from a utils class in java and pass it in to the stylesheet.

好吧,没问题。但是现在,我想从 java 中的 utils 类读取基本 URL 并将其传递给样式表。

//java
String baseUrl = myUtils.getBaseUrl();

<!-- xslt -->
<xsl:variable name="url">
   <xsl:value-of select="$baseUrl"/>
   <xsl:text>/service?id=</xsl:text>
   <xsl:value-of select="@id"/>
</xsl:variable>

Any suggestion on how to do this? My Java utils class loads the value from a myApp.properties file on the classpath, but I'm not sure I can utilize that from XSLT...

关于如何做到这一点的任何建议?我的 Java utils 类从类路径上的 myApp.properties 文件加载值,但我不确定我是否可以从 XSLT 中使用它...

回答by Mads Hansen

Declare an xsl:paramin your stylesheet, so that the baseUrlvalue can be passed at invocation time:

xsl:param在样式表中声明 an ,以便baseUrl可以在调用时传递值:

<xsl:param name="baseUrl" />

Set the parameter on the Transformerobject:

Transformer对象上设置参数:

Transformer transformer = TransformerFactory.newInstance().newTransformer(xslSource);
transformer.setParameter('baseUrl', myUtils.getBaseUrl());
transformer.transform(xmlInput, xmlOutput);

If you are using XSLT 2.0, then you may consider using the resolve-uri()function when you are constructing the urlvariable value:

如果您使用的是 XSLT 2.0,那么您可以resolve-uri()在构造url变量值时考虑使用该函数:

<xsl:variable name="url" 
              select="resolve-uri(concat('/service?id=', @id), $baseUrl)" />

resolve-uri()can help compensate for trailing slashes, hashtags, and other things in the baseUrlthat might otherwise result an invalid URL to be constructed by simply concatenating the $baseUrlwith the fragment and @id.

resolve-uri()可以帮助补偿尾随斜杠、主题标签和其他内容,baseUrl否则可能会导致通过简单地将$baseUrl与片段和连接来构造无效的 URL@id

回答by Clafou

Call setParameteron your Transformer instance, with the name and value of your parameter. Then inside your XSLT document declare the parameter using <xsl:param name="yourParamName" />and you can then use it in your XSLT for example this way: <xsl:value-of select="$yourParamName" />

setParameter使用参数的名称和值调用您的 Transformer 实例。然后在您的 XSLT 文档中声明参数 using <xsl:param name="yourParamName" />,然后您可以在 XSLT 中使用它,例如:<xsl:value-of select="$yourParamName" />