2014-02-14 25 views
0

嗨,我想獲得具有類型html的文件。我很好。但我的問題,當沒有文件與HTML它顯示爲空。但我想顯示一些消息,如「沒有提交htmls」。我會在正常的編程語言中使用計數器變量。在這裏我無法做到這一點,請幫我解決這個問題。謝謝xslt中的計數器變量功能

注意:需要xslt 1.0。

XML:

<?xml version="1.0" encoding="UTF-8"?> 
<Files> 
    <Path class="com.interwoven.cssdk.filesys.CSAreaRelativePath" path="MS/homedemo.html" type="html" /> 
    <Path class="com.interwoven.cssdk.filesys.CSAreaRelativePath" path="MS/test125.html" type="html" /> 
    <Path class="com.interwoven.cssdk.filesys.CSAreaRelativePath" path="iwov-resources/css/akz.css" type="css" /> 
    <Path class="com.interwoven.cssdk.filesys.CSAreaRelativePath" path="iwov-resources/css/animate.css" type="css" /> 
    <Path class="com.interwoven.cssdk.filesys.CSAreaRelativePath" path="iwov-resources/css/base.css" type="css" /> 
    <Path class="com.interwoven.cssdk.filesys.CSAreaRelativePath" path="iwov-resources/css/font-awesome-ie7.css" type="css" /> 
</Files> 

XSLT:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
    <xsl:template match="/"> 
     <html> 
      <p> 
       <xsl:apply-templates select="//Files" /> 
      </p> 
     </html> 
    </xsl:template> 
    <xsl:template match="//Files"> 
     <h1 style="background: #979797;border-radius: 5px;color: #FFF;text-shadow: 1px 1px 1px #000;padding: 5px;font-size:16px;">US files are now live and you can access them using the following URLs:</h1> 
     <ol> 
      <xsl:for-each select="//Files/Path">   
       <xsl:variable name="type"> 
        <xsl:choose> 
         <xsl:when test="@type"> 
          <xsl:value-of select="@type" /> 
         </xsl:when> 
         <xsl:otherwise> 
          <xsl:value-of select="'html'" /> 
         </xsl:otherwise> 
        </xsl:choose> 
       </xsl:variable> 
       <xsl:variable name="path" select="@path" /> 

         <xsl:choose> 
          <xsl:when test="$type='html'"> 
           <li>  
            <a class="iw-base-link"> 
             <xsl:attribute name="href"> 
              <xsl:value-of select="$path"/> 
             </xsl:attribute> 
             <xsl:value-of select="$path" /> 
            </a> 
           </li> 
          </xsl:when> 
         </xsl:choose> 
      </xsl:for-each>       
     </ol> 
     </xsl:template> 
</xsl:stylesheet> 

當我們有類型的HTML文件,然後顯示在下面的事情:

US files are now live and you can access them using the following URLs: 

1.MS/homedemo.html 
2.MS/test125.html 

當我們沒有類型的HTML文件,然後顯示在下面的事情:

US files are now live and you can access them using the following URLs: 

NO html files were submitted. 

回答

4

您的XSLT比它需要更復雜一點。而不是做一個的xsl:在每個路徑然後加入測試環路該類型內的for-each,邏輯添加到所述XSL的「選擇」:的for-each本身

<xsl:for-each select="Path[@type='html' or not(@type)]"> 

請注意,這是一個「相對」表達式,相對於您當前所處的文件元素。

這意味着你不需要類型的循環變量聲明,或的xsl:選擇。更重要的是,它意味着你實際上可以使用一個變量來保存要遍歷

<xsl:variable name="files" select="Path[@type='html' or not(@type)]" /> 

然後,您可以使用XSL節點:選擇檢查這是否在任何節點或不

<xsl:choose> 
     <xsl:when test="count($files) = 0"> 
      No files 
     </xsl:when> 
     <xsl:otherwise> 
      <!-- You existing loop here --> 
     </xsl:otherwise> 
    <xsl:choose> 

試試這個XSLT

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
    <xsl:template match="/"> 
     <html> 
      <p> 
       <xsl:apply-templates select="//Files" /> 
      </p> 
     </html> 
    </xsl:template> 
    <xsl:template match="//Files"> 
     <xsl:variable name="files" select="Path[@type='html' or not(@type)]" /> 
     <xsl:choose> 
      <xsl:when test="count($files) = 0">No files</xsl:when> 
      <xsl:otherwise> 
     <h1 style="background: #979797;border-radius: 5px;color: #FFF;text-shadow: 1px 1px 1px #000;padding: 5px;font-size:16px;">US files are now live and you can access them using the following URLs:</h1> 
     <ol> 
      <xsl:for-each select="$files">   
       <xsl:variable name="path" select="@path" /> 
       <li>  
        <a class="iw-base-link" name="{$path}"> 
         <xsl:value-of select="$path" /> 
        </a> 
       </li> 
      </xsl:for-each> 
     </ol> 
     </xsl:otherwise> 
     </xsl:choose> 
     </xsl:template> 
</xsl:stylesheet> 

也請注意使用「屬性值模板」的創建超鏈接,馬XSLT更簡單。花括號表示要評估的表達式而不是字面輸出。

1

其實是有一個count()函數中的XPath:

<ol> 
    <xsl:choose> 
     <xsl:when test="count(//Files/Path) > 0"> 
      <xsl:for-each select="//Files/Path">   
       .......... 
      </xsl:for-each> 
     </xsl:when> 
     <xsl:otherwise> 
       NO html files were submitted. 
     </xsl:otherwise> 
    </xsl:choose> 
</ol> 

所以,你可以只寫一個whenotherwise case :)

+0

它是xslt 1.0版本 –

+0

@susheel是的。 – parakmiakos

+0

讓我檢查它的工作原理.. –

2

怎麼樣(很多)更簡單?

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

<xsl:variable name="HTMLfiles" select="/Files/Path[@type='html']" /> 

<xsl:template match="/"> 
<html> 
<xsl:choose> 
    <xsl:when test="$HTMLfiles"> 
     <h1 style="background: #979797;border-radius: 5px;color: #FFF;text-shadow: 1px 1px 1px #000;padding: 5px;font-size:16px;">US files are now live and you can access them using the following URLs:</h1> 
     <ol>  
      <xsl:apply-templates select="$HTMLfiles" /> 
     </ol> 
     </xsl:when> 
    <xsl:otherwise> 
     <h1 style="background: #979797;border-radius: 5px;color: #FFF;text-shadow: 1px 1px 1px #000;padding: 5px;font-size:16px;">NO html files were submitted.</h1> 
    </xsl:otherwise> 
</xsl:choose> 
</html> 
</xsl:template> 

<xsl:template match="Path"> 
    <li>  
     <a class="iw-base-link"> 
      <xsl:attribute name="href"> 
       <xsl:value-of select="@path"/> 
      </xsl:attribute> 
      <xsl:value-of select="@path" /> 
     </a> 
    </li> 
</xsl:template> 

</xsl:stylesheet> 

請注意,您的XSLT放置一個<h1>一個<p>元素中;我不確定這是件好事。