jbe*_*rd4 5 xslt pipeline transformation
我编写了一个使用15个XSL样式表管道的应用程序,并且我开始着手调整其性能。它设计为可移植的,因此可以在Web浏览器环境和桌面中运行。在桌面上,我认为将样式表分隔开作为多个转换的管道可能是有意义的,因为这允许每个单独的转换在其自己的线程中运行,这在具有多核的CPU上非常有效。但是,不仅浏览器环境是单线程的,而且在大多数浏览器中,暴露给JavaScript的XSL处理API要求将每个单独转换的结果解析回DOM对象,这似乎效率很低。我认为,如果可能的话,在浏览器环境中运行时将所有样式表组合为一个样式表将是有利的。我对如何使用exsl:node-set(大多数浏览器都支持)如何实现这一想法有所了解,但是我不清楚我想象中的技术是否可以推广。是否存在将XSL样式表的管道转换为单个XSL样式表的通用技术,从而保留完整管道的语义?自动化解决方案将是理想的。是否存在将XSL样式表的管道转换为单个XSL样式表的通用技术,从而保留完整管道的语义?自动化解决方案将是理想的。是否存在将XSL样式表的管道转换为单个XSL样式表的通用技术,从而保留完整管道的语义?自动化解决方案将是理想的。
有一种技术允许将独立的变换链接在一起,其中第 k 个变换的输出是第 (k+1) 个变换的输入。
这是一个简单的例子:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:ext="http://exslt.org/common"
exclude-result-prefixes="ext xsl">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:template match="node()|@*" name="identity">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="/">
<xsl:variable name="vrtfPass1">
<xsl:apply-templates select="node()"/>
</xsl:variable>
<xsl:apply-templates mode="pass2"
select="ext:node-set($vrtfPass1)/node()"/>
</xsl:template>
<xsl:template match="/*">
<xsl:copy>
<xsl:copy-of select="@*"/>
<one/>
<xsl:apply-templates/>
</xsl:copy>
</xsl:template>
<xsl:template match="node()|@*" mode="pass2">
<xsl:copy>
<xsl:apply-templates select="node()|@*" mode="pass2"/>
</xsl:copy>
</xsl:template>
<xsl:template match="/*/one" mode="pass2" >
<xsl:call-template name="identity"/>
<two/>
</xsl:template>
</xsl:stylesheet>
Run Code Online (Sandbox Code Playgroud)
当此转换应用于以下 XML 文档时:
<doc/>
Run Code Online (Sandbox Code Playgroud)
生成想要的结果(第一遍将元素添加<one/>为顶部元素的子元素,然后第二遍添加另一个子元素,, immediately after the element即在第一遍中创建的):
<doc>
<one/>
<two/>
</doc>
Run Code Online (Sandbox Code Playgroud)
FXSL中有一个非常适合执行此操作的模板/函数:这就是compose-flist模板。它采用初始数据参数和 N 个函数(模板)作为参数,并生成这些函数/模板的链式组合。
以下是 FXSL 库中的测试示例:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:f="http://fxsl.sf.net/"
xmlns:myFun1="f:myFun1"
xmlns:myFun2="f:myFun2"
xmlns:ext="http://exslt.org/common"
exclude-result-prefixes="xsl f ext myFun1 myFun2"
>
<xsl:import href="compose.xsl"/>
<xsl:import href="compose-flist.xsl"/>
<!-- to be applied on any xml source -->
<xsl:output method="text"/>
<myFun1:myFun1/>
<myFun2:myFun2/>
<xsl:template match="/">
<xsl:variable name="vFun1" select="document('')/*/myFun1:*[1]"/>
<xsl:variable name="vFun2" select="document('')/*/myFun2:*[1]"/>
Compose:
(*3).(*2) 3 =
<xsl:call-template name="compose">
<xsl:with-param name="pFun1" select="$vFun1"/>
<xsl:with-param name="pFun2" select="$vFun2"/>
<xsl:with-param name="pArg1" select="3"/>
</xsl:call-template>
<xsl:variable name="vrtfParam">
<xsl:copy-of select="$vFun1"/>
<xsl:copy-of select="$vFun2"/>
<xsl:copy-of select="$vFun1"/>
</xsl:variable>
Multi Compose:
(*3).(*2).(*3) 2 =
<xsl:call-template name="compose-flist">
<xsl:with-param name="pFunList" select="ext:node-set($vrtfParam)/*"/>
<xsl:with-param name="pArg1" select="2"/>
</xsl:call-template>
</xsl:template>
<xsl:template match="myFun1:*" mode="f:FXSL">
<xsl:param name="pArg1"/>
<xsl:value-of select="3 * $pArg1"/>
</xsl:template>
<xsl:template match="myFun2:*" mode="f:FXSL">
<xsl:param name="pArg1"/>
<xsl:value-of select="2 * $pArg1"/>
</xsl:template>
</xsl:stylesheet>
Run Code Online (Sandbox Code Playgroud)
当此转换应用于任何 xml 文档(未使用)时,会产生所需的正确结果:
Compose:
(*3).(*2) 3 =
18
Multi Compose:
(*3).(*2).(*3) 2 =
36
Run Code Online (Sandbox Code Playgroud)
请注意:在 XSLT 2.0 及更高版本中,不需要xxx:node-set()扩展,任何链式转换都可以包含在实际函数中。
| 归档时间: |
|
| 查看次数: |
1476 次 |
| 最近记录: |