2012-04-20 69 views
0

我的XSL是像XSL conditon檢查中應用模板

<root> 
    <tests> 
     <test id="222"> 
     <actionId>233</actionId> 
     </test> 
     <test id="22"> 
     <actionId>23333</actionId> 
     </test> 
    </tests> 
    <confs> 
     <conf id="2211"></conf> 
    </confs> 
</root> 

    <xsl:key name="confId" 
     match="confs/conf" 
     use="@id"/
<xsl:template match="main"> 
Test: 
<xsl:apply-templates select="tests/test" /> 
</xsl:template> 
<xsl:template match="tests/test"> 
<xsl:choose> 
      <xsl:when test="actionId[.='']"> 
       <li> 
        <xsl:choose> 
         <xsl:when test="key('confId', @id)"> 
          Used </xsl:when> 
         <xsl:otherwise> Not found</xsl:otherwise> 
        </xsl:choose> 
       </li> 
      </xsl:when> 
      <xsl:otherwise> <li>Not at all found</li></xsl:otherwise> 
     </xsl:choose> 
</xsl:template> 
現在

權「並非在所有發現的」打印信息在每次迭代此模板匹配的actionId不爲空,這是我需要打印只有一次,如果不執行條件。

期望輸出是

  • 並非在所有發現
  • 只打印時actionId在測試/測試模板從未發現匹配,即,一旦
  • 並非在所有發現
  • 未在模板

    +0

    我說:你執行前檢查條件模板並在那裏打印。 – riffnl 2012-04-20 10:35:57

    +0

    但我迭代節點檢查測試模板中的條件 – Unni 2012-04-20 10:50:27

    +0

    不完整的代碼和缺乏任何源XML - 你認爲人們能夠理解你所問的任何東西嗎?如果提供完整的代碼和源XML文檔,則可以解釋觀察到的行爲的原因。我們需要查看密鑰的定義以及如何選擇模板來執行。請編輯問題並提供缺少的信息。 – 2012-04-20 12:55:18

    回答

    0

    每次迭代match =「test」模板被激活時,它只能使用源文檔中的測試元素或模板參數或全局變量提供的信息。它不能使用以前激活的任何知識,因爲XSLT是無狀態的。

    如果要處理與其他元素不同的第一個測試元素,請編寫兩個不同的模板規則。

    如果要處理與actionId第一測試元件[。=「」]特別,然後寫與其匹配的模板規則:

    <xsl:template match="test[actionId[.=''][1]"> 
    
    +0

    我曾嘗試過兩種不同的模板規則,但每次找到一個測試元素時,它都會打印出根本未找到的應該打印的消息只有一次像我試過的一樣就像 Unni 2012-04-20 11:39:53

    +0

    任何其他選項,我嘗試將更有幫助 – Unni 2012-04-23 10:47:45

    +0

    我錯過了什麼? – Unni 2012-05-23 11:53:47