【发布时间】:2010-07-12 23:28:02
【问题描述】:
我有一系列中等大小的XML文档,主要是文本,有几个节点代表要扩展的宏,例如:
<foo>Some text <macro>A1</macro> ... <macro>B2</macro> ...etc...</foo>
我的目标是用相应的 XML 替换每个宏。通常它是一个具有不同属性的<img> 标签,但也可以是其他一些 HTML。
样式表是以编程方式生成的,一种方法是为每个宏设置一个模板,例如
<xsl:template match="macro[.='A1']">
<!-- content goes here -->
</xsl:template>
<xsl:template match="macro[.='A2']">
<!-- other content goes here -->
</xsl:template>
<xsl:template match="macro[.='B2']">
<!-- etc... -->
</xsl:template>
它工作得很好,但它最多可以有一百个宏,而且性能不是很好(我正在使用 libxslt。)我尝试了几种替代方法,例如:
<xsl:template match="macro">
<xsl:choose>
<xsl:when test=".='A1'">
<!-- content goes here -->
</xsl:when>
<xsl:when test=".='A2'">
<!-- other content goes here -->
</xsl:when>
<xsl:when test=".='B2'">
<!-- etc... -->
</xsl:when>
</xsl:choose>
</xsl:template>
它的性能略高一些。我尝试添加另一个级别的分支,例如:
<xsl:template match="macro">
<xsl:choose>
<xsl:when test="substring(.,1,1) = 'A'">
<xsl:choose>
<xsl:when test=".='A1'">
<!-- content goes here -->
</xsl:when>
<xsl:when test=".='A2'">
<!-- other content goes here -->
</xsl:when>
</xsl:choose>
</xsl:when>
<xsl:when test=".='B2'">
<!-- etc... -->
</xsl:when>
</xsl:choose>
</xsl:template>
它的加载速度稍慢(XSL 更大且更复杂),但执行速度稍快(每个分支可以消除几种情况。)
现在我想知道,有没有更好的方法来做到这一点?我有大约 50-100 个宏。通常,转换是使用 libxslt 执行的,但我不能使用来自其他 XSLT 引擎的专有扩展。
欢迎任何意见:)
【问题讨论】:
标签: optimization xslt