如果模板富应产生输出,使用输出为标志的任何解决方案将无法正常工作。在这种情况下,如果您使用的是基于Java的XSLT处理器(例如Saxon或Xalan),则可以使用可变的Java对象。
但请注意,这有它自己的困难。下面给出的转换使用全局标志,可能不足以满足所有用例。我想在bla模板中实例化标志并将其作为参数传递给foo和bar,但是我无法在Xalan中使用它。另请注意,我在xsl:value-of
中调用了Java设置器,因为否则该调用可能会被优化掉(请参阅Cannot access updated Java object from Saxon XSLT processor)。
<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:myflag="java:mypackage.MyFlag">
<xsl:variable name="foo-flag" select="myflag:new()" />
<xsl:template match="bla">
<xsl:apply-templates select="foo"/> <!-- depending on the contents of foo... -->
<xsl:apply-templates select="bar"/> <!-- ... different things should happen in bar -->
</xsl:template>
<xsl:template match="foo">
<xsl:choose>
<xsl:when ...>
<xsl:value-of select="myflag:set($foo-flag, true())" />
...
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="myflag:set($foo-flag, false())" />
...
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template match="bar">
<xsl:choose>
<xsl:when test="myflag:get($foo-flag)">
...
</xsl:when>
<xsl:otherwise>
...
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:transform>
MyFlag类的最基本版本只是一个可变的布尔包装器。
public class MyFlag {
private boolean flag;
public void set(boolean flag){
this.flag = flag;
}
public boolean get(){ return flag; }
}
请提供更多信息。问题严重不足。 – 2010-04-14 21:50:04