在Grails服务中使用XSLT进行转换

时间:2010-11-02 19:29:46

标签: grails xslt

我想在我的grails web应用程序中编写一个服务,将xslt应用于某些xml。以下代码抛出 java.lang.ArrayIndexOutOfBoundsException:-1

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

class RdfService {

    static transactional = false

 def transform(url){
  def String wsdl= url.toString().toURL().text
  def File xslt= new File("lib/SAWSDL_GRDDL.xsl")
  def TransformerFactory factory = TransformerFactory.newInstance()
  def Transformer transformer = factory.newTransformer(new StreamSource(new FileReader(xslt)))
  def StringWriter out= new StringWriter()
  transformer.transform(new StreamSource(new StringReader(wsdl)), new StreamResult(out))
  println out
 }
}

但是,当我使用相同的值测试以下类似的代码时,我得到了所需的结果:

import javax.xml.transform.TransformerFactory
import javax.xml.transform.stream.StreamResult
import javax.xml.transform.stream.StreamSource



def xml= new File("data/wsdl/EtaService.wsdl").text
def xslt= new File("data/SAWSDL_GRDDL.xsl")
def factory = TransformerFactory.newInstance()
def transformer = factory.newTransformer(new StreamSource(new FileReader(xslt)))
def out= new StringWriter()
transformer.transform( new StreamSource(new StringReader(xml)), 
        new StreamResult(out))
println out

我的代码最初是为了更有效率而努力,但此时我只是想让事情发挥作用: - )

1 个答案:

答案 0 :(得分:0)

这是一个黑客,但我发现如果我删除了XML标记,在文档中它会适当地处理它。

wsdl = wsdl.replace('<?xml version="1.0" encoding="UTF-8"?>', '')