2011-05-02 74 views
2

我有下面這段XSLT代碼:XSLT變量不工作

<?xml version="1.0" encoding="utf-8"?> 

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns="http://www.w3.org/1999/xhtml"> 

<xsl:variable name="condition">(coasts='Adriatic Sea')or(coasts='Mediterranean Sea')</xsl:variable> 
<xsl:template match="cia"> 
    <html> 
    <head></head> 
    <body> 
     <table border="1"> 
     <tr> 
     <th>Country</th> 
     <th>Capital</th> 
     <th>Area</th> 
     <th>Population</th> 
     <th>Inflation rate</th> 
     </tr> 
     <xsl:for-each select="country"> 
     <xsl:if test="{$condition}"> 
     <tr> 
     <td> 
      <xsl:value-of select="@name"/> 
     </td> 
     <td> 
      <xsl:value-of select="@capital"/> 
     </td> 
     <td> 
      <xsl:value-of select="@total_area"/> 
     </td> 
     <td> 
      <xsl:value-of select="@population"/> 
     </td> 
     <td> 
      <xsl:value-of select="@inflation"/> 
     </td> 
     </tr> 
     </xsl:if> 
     </xsl:for-each> 
     </table> 
    </body> 
    </html> 
    </xsl:template> 
</xsl:stylesheet> 

如果直接放在if元素的代碼工作正常條件表達式,但是,我的問題是,當我給你同樣的條件表達式的變量,然後在if元素中引用它不起作用。難道我做錯了什麼?這可能嗎? 謝謝!

+0

@ user734702:如果您在進行某種動態評估之後,您將需要爲您的XSLT處理器查找或實現一些擴展功能。除非您提供可能導致另一種解決方案的表達式構造的細節。 – 2011-05-02 17:51:16

回答

1

有多種問題:

  • <xsl:if test="{$condition}">托架是不必要的;使用<xsl:if test="$condition">
  • 使用以下xsl:variable結構:

    <xsl:variable name="condition" 
         select="(coasts='Adriatic Sea')or(coasts='Mediterranean Sea')"/> 
    
  • 當你有你的xsl:if進行試驗相對於每個country的條件。頂級變量中不是這種情況。變量的值是表達式的結果,而不是表達式本身。如果你堅持一個變量,那麼在循環內初始化它。

請參見下面的樣式表:

<xsl:stylesheet version="1.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    xmlns="http://www.w3.org/1999/xhtml"> 
    <xsl:template match="cia"> 
     <html> 
      <head></head> 
      <body> 
       <table border="1"> 
        <tr> 
         <th>Country</th> 
         <th>Capital</th> 
         <th>Area</th> 
         <th>Population</th> 
         <th>Inflation rate</th> 
        </tr> 
        <xsl:for-each select="country"> 
         <xsl:variable name="condition" 
          select="(coasts='Adriatic Sea') or 
            (coasts='Mediterranean Sea')" /> 
         <xsl:if test="$condition"> 
          <tr> 
           <td> 
            <xsl:value-of select="@name" /> 
           </td> 
           <td> 
            <xsl:value-of select="@capital" /> 
           </td> 
           <td> 
            <xsl:value-of select="@total_area" /> 
           </td> 
           <td> 
            <xsl:value-of select="@population" /> 
           </td> 
           <td> 
            <xsl:value-of select="@inflation" /> 
           </td> 
          </tr> 
         </xsl:if> 
        </xsl:for-each> 
       </table> 
      </body> 
     </html> 
    </xsl:template> 
</xsl:stylesheet> 

輸入:

<cia> 
    <country name="test1" inflation="22"> 
     <coasts>Adriatic Sea</coasts> 
    </country> 
    <country name="test2" inflation="7"> 
     <coasts>No match</coasts> 
    </country> 
</cia> 

輸出(僅第一country通過測試):

那你不
<html xmlns="http://www.w3.org/1999/xhtml"> 
    <head></head> 
    <body> 
     <table border="1"> 
      <tr> 
       <th>Country</th> 
       <th>Capital</th> 
       <th>Area</th> 
       <th>Population</th> 
       <th>Inflation rate</th> 
      </tr> 
      <tr> 
       <td>test1</td> 
       <td></td> 
       <td></td> 
       <td></td> 
       <td>22</td> 
      </tr> 
     </table> 
    </body> 
</html> 

注實際上甚至需要一個單獨的條件;最好先選擇所需的元素。這個循環產生相同的輸出:

<xsl:for-each 
    select="country[(coasts='Adriatic Sea') or 
        coasts='Mediterranean Sea')]"> 
    <tr> 
     <td> 
      <xsl:value-of select="@name" /> 
     </td> 
     <td> 
      <xsl:value-of select="@capital" /> 
     </td> 
     <td> 
      <xsl:value-of select="@total_area" /> 
     </td> 
     <td> 
      <xsl:value-of select="@population" /> 
     </td> 
     <td> 
      <xsl:value-of select="@inflation" /> 
     </td> 
    </tr> 
</xsl:for-each> 
0

更換

   <xsl:for-each select="country"> 

   <xsl:apply templates select="country"/> 

此外,添加這些模板

<xsl:template match="country"/> 

<xsl:template match= 
    "country[coasts='Adriatic Sea' 
     or           
      coasts='Mediterranean Sea' 
     ]"> 
<xsl:template match= 
    "country[coasts='Adriatic Sea' 
     or 
      coasts='Mediterranean Sea' 
     ]"> 
    <tr> 
     <td> 
      <xsl:value-of select="@name"/> 
     </td> 
     <td> 
      <xsl:value-of select="@capital"/> 
     </td> 
     <td> 
      <xsl:value-of select="@total_area"/> 
     </td> 
     <td> 
      <xsl:value-of select="@population"/> 
     </td> 
     <td> 
      <xsl:value-of select="@inflation"/> 
     </td> 
    </tr> 
</xsl:template> 

您注意到<xsl:if>「神奇地」消失了嗎?

當然,這個代碼可以進一步改進,但是您還沒有提供要應用轉換的XML文檔的實例,也沒有提供期望的輸出。