在Java中,从TransformerFactory用于创建对象到处理XSLT,它具有以下方法:
newTransformer它创建了 Transformer对象,可以将XML转换为结果.newTemplates它创建了Templates可以创建的对象Transformer.Transformer明确说明的文档:
变压器可以多次使用.
我的应用程序使用相同的XSLT处理各种不同的XML.在程序开始时,我用newTransformer它创建一个Transformer然后重新使用它来处理所有XML(确保它是同步的,所以我只在一个线程中使用它;并reset()在每次处理之前调用它的方法.).
这样我就不会为每个XML I进程重新编译XSLT.
那么这个newTemplates和Templates对象的重点是什么?我应该使用它,并Transformer为每个XML 创建一个新对象吗?
假设您有一个高度合成的任务,在没有适当的输入XML的情况下打印1到1.000.000之间的数字.当然,由于具有讽刺意味的堆栈溢出,直接递归将失败.
我想出了下面列出的解决方案:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text"/>
<xsl:variable name="end" select="number(1000000)"/>
<xsl:template match="/">
<xsl:call-template name="batches"/>
</xsl:template>
<xsl:template name="batches">
<xsl:param name="start" select="number(1)"/>
<xsl:param name="stop" select="$end"/>
<xsl:param name="ololo"/>
<xsl:if test="$start <= ($end)">
<xsl:choose>
<xsl:when test="$stop = 0">
<xsl:value-of select="$start"/>:<xsl:value-of select="$ololo"/>
<xsl:text>
</xsl:text>
</xsl:when>
<xsl:otherwise>
<xsl:call-template name="batches">
<xsl:with-param name="start" select="$start"/>
<xsl:with-param name="stop" select="floor($stop div 2)"/>
<xsl:with-param name="ololo" select=" 'A' "/>
</xsl:call-template>
<xsl:call-template name="batches">
<xsl:with-param name="start" select="floor($stop div 2) + $start + 1"/>
<xsl:with-param name="stop" select="floor($stop div 2)"/>
<xsl:with-param name="ololo" select=" 'B' "/> …Run Code Online (Sandbox Code Playgroud)