XSLT 流式链式转换 [英] XSLT Streaming Chained Transform

查看:48
本文介绍了XSLT 流式链式转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Saxon EE 来转换一个非常大的文档,使用流式转换.我现在需要链接多个 XSLT.怎么做呢?不流式传输时,我使用 XSLTTransformer 类作为目标,进行链式转换.如果我没记错的话,我想我不能那样做,因为那样会创建一个结果树而不是结果流.谢谢,阿尼

I am using Saxon EE to transform a very large document, using streaming transform. I now need to chain multiple XSLTs. How does one go about doing that ? When not streaming, I have used the XSLTTransformer class as destination, to do chained transforms. If I am not mistaken, I guess I cannot do that, as that would create a result tree as against result stream. Thanks, Ani

推荐答案

将第一个转换的 SAX 输出事件通过管道传输到第二个转换的 SAX 输入事件.

Pipe the SAX output events of the 1st transform into the SAX input events of the 2nd transform.

我附上了一些显示这一点的示例 Scala 代码.

I've attached some example Scala code which shows this.

基本上它首先启动第二个 XSLT,它在幕后调用第一个 XSLT,初始输入文档捕获中间输出,然后作为输入实时提供给第二个 XSLT.不错.

Basically it kicks off the 2nd XSLT first, which behind the scenes invokes the 1st XSLT with the initial input document capturing the intermediate output which is then fed as the input to the 2nd XSLT in real time. Nice.

  • 它使用 JAXP 接口,所以没有 S9 API.
  • 我已经使用 1.2GB 的输入 XML 文件成功地对其进行了测试.

希望这会有所帮助.

顺便说一句,XSLT 3.0 非常棒!不错的选择.

XSLT 3.0 rocks by the way! Good choice.

import javax.xml.transform.sax.{SAXResult, SAXSource}
import javax.xml.transform.stream.{StreamResult, StreamSource}
import javax.xml.transform.{Source, Transformer}

import com.saxonica.config.StreamingTransformerFactory
import org.xml.sax._
import org.xml.sax.helpers.XMLFilterImpl

object Main extends App
{
  val transformer1 = transformer("transform-1.xsl")
  val transformer2 = transformer("transform-2.xsl")

  val inputXML = "big.xml"

  transformer2.transform(
    new SAXSource(
      new Transformer1OutputReader(transformer1, new StreamSource(inputXML)),
      null
    ),
    new StreamResult("out.xml")
  )

  def transformer(xslt : String) =
    new StreamingTransformerFactory().newTransformer(new StreamSource(xslt))

}


class Transformer1OutputReader(
  transformer1 : Transformer,
  source1 : Source) extends XMLFilterImpl
{
  def parseImpl() =
  {
    println("parseImpl()")

    val inputToSecondXslt : ContentHandler = getContentHandler

    transformer1.transform(
      source1,
      new SAXResult(inputToSecondXslt)
    )
  }

  override def parse(input : InputSource) = parseImpl
  override def parse(systemId : String) = parseImpl
  override def setFeature(name: String, value: Boolean) : Unit = {}
}

这篇关于XSLT 流式链式转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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