有人可以解释一下struts2 XSL ResultType是如何工作的吗?

时间:2023-02-04 08:19:08

I'm trying to figure out how to use the struts2 XSL ResultType. I just can't figure out how it is supposed to work and aside from the official documentation, I've found maybe two articles online that tried to explain it (unsuccessfully for me).

我正在试图弄清楚如何使用struts2 XSL ResultType。我只是无法弄清楚它应该如何工作,除了官方文档,我发现可能有两篇文章试图解释它(对我来说不成功)。

My understanding of how it works is I create an xsl stylesheet, then I have a struts action setup that somehow returns an xml document. The action applies the assigned xsl stylesheet to the xml document and it outputs the desired xhtml to the browser. Is this correct?

我对它是如何工作的理解是我创建了一个xsl样式表,然后我有一个struts动作设置,它以某种方式返回一个xml文档。该操作将指定的xsl样式表应用于xml文档,并将所需的xhtml输出到浏览器。它是否正确?

If my understanding is correct, can someone provide a clear, concise explanation of how I set up my action class to return an xml document that can be transformed?

如果我的理解是正确的,有人可以提供一个清晰,简明的解释,说明如何设置我的动作类以返回可以转换的xml文档吗?

If my understanding is incorrect, can someone please correct me and explain how it is supposed to be used?

如果我的理解不正确,有人可以纠正我并解释它应该如何使用?

Thanks!

1 个解决方案

#1


The XSLTResult internally produces the XML to postprocess with the given template. If you use an XSLT file that doesn't actually transform anything, you get the raw result:

XSLTResult在内部生成XML以使用给定模板进行后处理。如果您使用的XSLT文件实际上没有转换任何内容,则会得到原始结果:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:template match="result">
    <xsl:copy-of select="."></xsl:copy-of>
  </xsl:template>
</xsl:stylesheet>

I use this template for all my XML results. If this seems silly to you, I have to agree. I found a number of tutorials on the interwebs which said that you could just drop the locationparameter from the result definition, but this led to the following error for me:

我将此模板用于所有XML结果。如果这对你来说很愚蠢,我必须同意。我在interwebs上发现了一些教程,说你可以从结果定义中删除locationparameter,但这导致了以下错误:

javax.xml.transform.TransformerException: Operation not supported. - [unknown location]

javax.xml.transform.TransformerException:不支持操作。 - [未知位置]

#1


The XSLTResult internally produces the XML to postprocess with the given template. If you use an XSLT file that doesn't actually transform anything, you get the raw result:

XSLTResult在内部生成XML以使用给定模板进行后处理。如果您使用的XSLT文件实际上没有转换任何内容,则会得到原始结果:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:template match="result">
    <xsl:copy-of select="."></xsl:copy-of>
  </xsl:template>
</xsl:stylesheet>

I use this template for all my XML results. If this seems silly to you, I have to agree. I found a number of tutorials on the interwebs which said that you could just drop the locationparameter from the result definition, but this led to the following error for me:

我将此模板用于所有XML结果。如果这对你来说很愚蠢,我必须同意。我在interwebs上发现了一些教程,说你可以从结果定义中删除locationparameter,但这导致了以下错误:

javax.xml.transform.TransformerException: Operation not supported. - [unknown location]

javax.xml.transform.TransformerException:不支持操作。 - [未知位置]