2012-01-29 48 views
2

下面是我輸出PDF文件鏈接和文件大小的Umbraco xslt的一部分。我遇到的問題是定位媒體文件的大小屬性。 size屬性具有umbracoBytes的別名。使用XSLT從Umbraco的媒體文件夾獲取文件大小

我似乎無法以此爲目標。

所以我現在輸出的是打開PDF的鏈接,但不是文件大小。

任何人誰可以提供幫助將不勝感激。謝謝。

<td> 
     <xsl:if test="document= ''"> 
     <xsl:value-of select="@nodeName"/> 
     </xsl:if> 
     <xsl:if test="document != ''"> 
     <a target="_blank"> 
     <xsl:attribute name="href"> 
      <xsl:value-of select="umbraco.library:GetMedia(document, 'false')/umbracoFile"/> 
     </xsl:attribute> 
     <xsl:value-of select="@nodeName"/>  
     <xsl:variable name="size" select="data [@alias = 'umbracoBytes']" /> 
      <xsl:variable name="sizeAndSuffix"> 
       <xsl:choose> 
         <xsl:when test="$size &gt;= 1073741824"> 
           <xsl:value-of select="format-number($size div 1073741824,'#,###')"/> 
           <xsl:text>GB</xsl:text> 
         </xsl:when> 
         <xsl:when test="$size &gt;= 1048576"> 
           <xsl:value-of select="format-number($size div 1048576,'#,###')"/> 
           <xsl:text>MB</xsl:text> 
         </xsl:when> 
         <xsl:when test="$size &gt;= 1024"> 
           <xsl:value-of select="format-number($size div 1024,'#,###')"/> 
           <xsl:text>KB</xsl:text> 
         </xsl:when> 
         <xsl:when test="$size &gt; 0 and $size &lt; 1024"> 
           <xsl:value-of select="format-number($size div 0,'#,###')"/> 
           <xsl:text>Bytes</xsl:text> 
         </xsl:when> 
         <xsl:otherwise> 
           <xsl:text>0 Bytes</xsl:text> 
         </xsl:otherwise> 
       </xsl:choose> 
     </xsl:variable> 

     </a> 
     </xsl:if> 
    </td> 
+0

您使用的是哪種版本的Umbraco? – 2012-01-29 13:10:49

+0

問題是您還沒有指定源XML文檔 - 對於不同的文檔,結果將會不同,且沒有人可以重新生成結果。此外,提供的代碼只是一個片段(甚至不是一個模板)。請提供一個完整的(最小首選)源XML文檔和完整的想要的結果,並說明轉換必須實現的要求。 – 2012-01-29 15:38:42

回答

1

我設法去解決它。

我除去纏繞變量和創建的變量名爲size其選擇別名UmbracoBytes(文件大小)

我然後通過選擇功能傳遞變量和該輸出正確的大小擴展。

感謝您的所有建議,我真的很感激!

<xsl:variable name="size" select="umbraco.library:GetMedia(document, 'false')/umbracoBytes"/> 

      <xsl:choose> 
        <xsl:when test="$size &gt;= 1073741824"> 
          <xsl:value-of select="format-number($size div 1073741824,'#,###')"/> 
          <xsl:text>GB</xsl:text> 
        </xsl:when> 
        <xsl:when test="$size &gt;= 1048576"> 
          <xsl:value-of select="format-number($size div 1048576,'#,###')"/> 
          <xsl:text>MB</xsl:text> 
        </xsl:when> 
        <xsl:when test="$size &gt;= 1024"> 
          <xsl:value-of select="format-number($size div 1024,'#,###')"/> 
          <xsl:text>KB</xsl:text> 
        </xsl:when> 
        <xsl:when test="$size &gt; 0 and $size &lt; 1024"> 
          <xsl:value-of select="format-number($size div 0,'#,###')"/> 
          <xsl:text>Bytes</xsl:text> 
        </xsl:when> 
        <xsl:otherwise> 
          <xsl:text>0 Bytes</xsl:text> 
        </xsl:otherwise> 
      </xsl:choose> 
1

的問題(我認爲)是因爲你沒有任何目標在前面加上你的$size變量選擇屬性,因此默認情況下它使用$currentPage或在循環迭代的電流值。

嘗試將GetMedia語句分配給變量,然後從中獲取數據。你的代碼似乎有不同版本的Umbraco使用的語法,所以我不能完全知道你正在使用哪一個。不同版本的Umbraco使用不同的底層XML結構。

如果使用<一把umbraco 4.5.1

<xsl:variable name="myDocument" select="umbraco.library:GetMedia(document, 'false')" /> 
<a target="_blank">   
    <xsl:attribute name="href"> 
     <xsl:value-of select="$myDocument/data[@alias='umbracoFile']"/> 
    </xsl:attribute> 
    ... 
    <xsl:variable name="size" select="$myDocument/data [@alias = 'umbracoBytes']" /> 
    ... 
</a> 

如果使用> = 4.5.1一把umbraco

<xsl:variable name="myDocument" select="umbraco.library:GetMedia(document, 'false')" /> 
<a target="_blank">   
    <xsl:attribute name="href"> 
     <xsl:value-of select="$myDocument/umbracoFile"/> 
    </xsl:attribute> 
    ... 
    <xsl:variable name="size" select="$myDocument/umbracoBytes" /> 
    ... 
</a>