2014-02-14 26 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>元素中;我不确定这是件好事。