使用exslt evaluate评估xslt中的变量

时间:2013-07-16 16:58:06

标签: java xslt

我正在使用xslt动态评估(dyn:evaluate)但我有一个问题,这个评估函数在一个循环中,我想评估一个不同的上下文而不是循环上下文,比如

<xsl:variable name="recId" select="dyn:evaluate($targetContext,$list[1])"/> 但是dyn:evaluate只有一个输入,这是一个变量,所以我不得不做这样的事情 <xsl:variable name="recId" select="dyn:evaluate($list[1])"/>

这里也是我的模板

  

<xsl:template match="hl7:controlActEvent/hl7:subject">
<xsl:for-each select="java:org.apache.xalan.lib.ExsltStrings.tokenize($xpath, ';')">
<xsl:variable name="list" select="java:org.apache.xalan.lib.ExsltStrings.tokenize(., ',')"/>
here's the problem, evaluation inside loop
<xsl:variable name="recId" select="dyn:evaluate($list[1])"/>
<xsl:variable name="createDate" select="dyn:evaluate($list[2])"/>
<xsl:choose>
<xsl:when test="$createDate != '' ">
<xsl:variable name="detailNode" select="$postDomainDoc/record_detail[record_id = $recId]"/>
<xsl:choose>
<xsl:when test="$detailNode/consent_status = 'deny' ">
<hl7:subject typeCode="SUBJ" nullFlavor="MSK" xsi:nil="true"/>
</xsl:when>
<xsl:otherwise>
<xsl:apply-templates select="@*|node()"/>
</xsl:otherwise>
</xsl:choose>
</xsl:when>
<xsl:when test="$createDate = '' and $ehipDecision = 'deny'">
<hl7:subject typeCode="SUBJ" nullFlavor="MSK" xsi:nil="true"/>
</xsl:when>
<xsl:otherwise>
<xsl:apply-templates select="@*|node()"/>
</xsl:otherwise>
</xsl:choose>
</xsl:for-each>
</xsl:template>

任何建议。

1 个答案:

答案 0 :(得分:0)

如果要在模板匹配的节点的上下文中评估表达式,可以通过在for-each之外的变量中捕获该节点,然后“重置上下文”来获得大部分方法。 “使用嵌套for-each

<xsl:template match="hl7:controlActEvent/hl7:subject">
  <xsl:variable name="theSubject" select="." />
  <xsl:for-each select="java:org.apache.xalan.lib.ExsltStrings.tokenize($xpath, ';')">
    <xsl:variable name="list" select="java:org.apache.xalan.lib.ExsltStrings.tokenize(., ',')"/>
    <xsl:for-each select="$theSubject">
      <!-- rest of template goes here, . is now the hl7:subject again -->
    </xsl:for-each>
  </xsl:for-each>
</xsl:template>

这会让.返回正确的内容,但内部for-each内的position()将始终为1,如果您需要访问外部position(),则必须同样将其保存在变量中。