2012-10-25 41 views
1

我遇到了另一個XSL語句問題。我試圖添加一個條件,以便如果找到某個值,在這個實例中的「調用」,那麼它的行爲稍有不同。我知道這是一件簡單的事情,我做錯了,但不知道是什麼!xsl for-each語句的問題

基本上下面的代碼似乎還是返回了正確的布爾值卻仍然把通話費用寫進去。類和URL的位置都輸出正確。

在此先感謝!

XML

<rhs_options> 
<title>RHS title</title> 
<service> 
    <option>call</option> 
    <text>telephone number text</text> 
    <telephone>telephone number</telephone> 
    <link>telephone number link</link> 
</service> 
<service> 
    <option>branch</option> 
    <text>branch text</text> 
    <telephone/> 
    <link>branch link</link> 
</service> 
<service> 
    <option>online</option> 
    <text>online text</text> 
    <telephone/> 
    <link>online link</link> 
</service> 

XSL

<aside class="cta"> 
<h4><xsl:value-of select="/Properties/Data/Datum[@ID='ID1']/DCR[@Type='RHS_Options']/rhs_options/title"/></h4> 
    <xsl:for-each select="/Properties/Data/Datum[@ID='ID1']/DCR[@Type='RHS_Options']/rhs_options/service"> 
     <xsl:choose> 
      <xsl:when test="/Properties/Data/Datum[@ID='ID1']/DCR[@Type='RHS_Options']/rhs_options/service/option='call'"> 
       <p class="{./option}"> 
        <xsl:value-of select="./text"/> 
         <br/> 
        <xsl:value-of select="./telephone"/> 
         <br/> 
        <a href="{./link}"> 
         Call charges 
        </a> 
       </p> 
      </xsl:when> 
      <xsl:when test="/Properties/Data/Datum[@ID='ID1']/DCR[@Type='RHS_Options']/rhs_options/service/option='phone'or'online'"> 
       <p class="{./option}"> 
        <a href="{./link}"> 
         <xsl:value-of select="./text"/> 
        </a> 
       </p> 
      </xsl:when> 
     </xsl:choose> 
    </xsl:for-each> 

+0

我也嘗試將最後的更改爲但它沒有區別 – Barlow1984

回答

1

你想

<xsl:choose> 
     <xsl:when test="option='call'"> 
      <p class="{option}"> 
       <xsl:value-of select="text"/> 
        <br/> 
       <xsl:value-of select="telephone"/> 
        <br/> 
       <a href="{link}"> 
        Call charges 
       </a> 
      </p> 
     </xsl:when> 
     <xsl:when test="option='phone' or option = 'online'"> 
      <p class="{option}"> 
       <a href="{link}"> 
        <xsl:value-of select="text"/> 
       </a> 
      </p> 
     </xsl:when> 
    </xsl:choose> 

說明

所提供的代碼的問題是,絕對 XPath表達式使用,但它需要使用相對 XPath表達式。

當然,最好是使用'的xsl:for-每個*和使用任何明確的XSLT條件指令

這裏是代碼,它等效但更緊湊的和可維護:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
<xsl:output omit-xml-declaration="yes" indent="yes"/> 
<xsl:strip-space elements="*"/> 

<xsl:template match="/*"> 
    <aside class="cta"> 
     <h4><xsl:value-of select="title"/></h4> 
     <xsl:apply-templates/> 
    </aside> 
</xsl:template> 

<xsl:template match="service[option='call']"> 
    <p class="{option}"> 
    <xsl:value-of select="text"/> 
    <br/> 
    <xsl:value-of select="telephone"/> 
    <br/> 
    <a href="{link}"> 
     Call charges 
    </a> 
    </p> 
</xsl:template> 

<xsl:template match="service"> 
    <p class="{option}"> 
    <a href="{link}"> 
     <xsl:value-of select="text"/> 
    </a> 
    </p> 
</xsl:template> 
</xsl:stylesheet> 

當這個變換所提供的XML文檔應用:

<rhs_options> 
    <title>RHS title</title> 
    <service> 
     <option>call</option> 
     <text>telephone number text</text> 
     <telephone>telephone number</telephone> 
     <link>telephone number link</link> 
    </service> 
    <service> 
     <option>branch</option> 
     <text>branch text</text> 
     <telephone/> 
     <link>branch link</link> 
    </service> 
    <service> 
     <option>online</option> 
     <text>online text</text> 
     <telephone/> 
     <link>online link</link> 
    </service> 
</rhs_options> 

有用結果產生:

<aside class="cta"> 
    <h4>RHS title</h4>RHS title<p class="call">telephone number text<br/>telephone number<br/> 
     <a href="telephone number link"> 
     Call charges 
    </a> 
    </p> 
    <p class="branch"> 
     <a href="branch link">branch text</a> 
    </p> 
    <p class="online"> 
     <a href="online link">online text</a> 
    </p> 
</aside> 
+0

很好的答案,非常感謝。它使得它更清晰! – Barlow1984

+0

@ Barlow1984,不客氣。 –