2015-02-09 14 views
0

中進行動態布爾檢查從我的根模板獲得唯一的賬戶值,調用去trans模板得到了輸入xml我將有多個節點,我的要求是一旦調用從root模板轉到trans模板中,如果在多個元素之間找到accountId的匹配項,則只會調用一次帳戶詳細信息模板,而不考慮其他找到的匹配項。我需要針對以上要求的解決方案。如何在xslt

輸入樣本:

<elements><accountId>1</accountId></elements> 
    <elements><accountId>1</accountId></elements> 
    <elements><accountId>2</accountId></elements> 
    <elements><accountId>2</accountId></elements> 
    <elements><accountId>3</accountId></elements> 

下面的行應該在一些代碼,所以它只能調用一次被包裹

<xsl:call-template name="Account_details" /> 

下面是我對XSL完整的代碼

<xsl:template match="/"> 
     <xsl:variable name="unique-accounts" select="//*/*/*/accountId/text()[generate-id()=generate-id(key('account-by-id', .)[1])]"/> 
     <xsl:for-each select="$unique-accounts"> 
      <xsl:variable name="currentValue" select="current()"/> 
       <xsl:apply-templates select="//trans"> 
       <xsl:with-param name="passCurrentValue" select="$currentValue"/> 
       </xsl:apply-templates> 
     </xsl:for-each> 
</xsl:template> 

<xsl:template match="trans"> 
    <xsl:param name="passCurrentValue" /> 
    <xsl:variable name="booleanValue" select="true()"/> 

     <xsl:for-each select="elements"> 
       <xsl:if test="$passCurrentValue=/*/*/accountId"> 
        <xsl:if test="$booleanValue"> 
         <xsl:call-template name="Account_details" /> 
         <xsl:variable name="booleanValue" select="false()"></xsl:variable> 
        </xsl:if> 
     </xsl:for-each> 
</xsl:template> 

<xsl:template name="Account_details"> 
............. 
</xsl:template> 
+1

如果您使用的是XSLT 1.0,那麼XSLT 1.0不支持在賦值後更改變量值 – Saurav 2015-02-09 12:25:40

+0

哦......是否有任何方法可以檢查代碼片段是否正在第一次執行......比如添加計數器或東西 – Brittas 2015-02-09 12:29:02

+0

@Brittas爲什麼不告訴我們你想要在這裏學習什麼的大局 - 而不是關注那些不起作用(並且不能在XSLT中工作)的微代碼? – 2015-02-09 12:32:24

回答

0

隨着代碼

<xsl:template match="/"> 
<xsl:variable name="booleanCheck" select="true"></xsl:variable> 

變量booleanCheck是文檔節點/下面的節點集(XSLT 1.0)或名爲true的元素序列。所以除非你的XML輸入有一個名爲true的根元素,否則這個值是一個空的節點集或空序列。如果你想選擇一個布爾值,然後使用<xsl:variable name="booleanValue" select="true()"/>。見http://www.w3.org/TR/xpath/#function-true。然後你可以測試<xsl:if test="$booleanValue">。這應該解釋如何使用布爾值,無論是否適合您的上下文我不確定。