Umbraco输出文件大小

时间:2010-11-30 19:22:44

标签: umbraco xslt

我正在尝试为已上传的pdf文件输出正确的大小。 但唯一的输出是0 - 我做错了什么?

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE xsl:stylesheet [ <!ENTITY nbsp "&#x00A0;"> ]>
<xsl:stylesheet 
    version="1.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    xmlns:msxml="urn:schemas-microsoft-com:xslt" 
    xmlns:umbraco.library="urn:umbraco.library" xmlns:Exslt.ExsltCommon="urn:Exslt.ExsltCommon" xmlns:Exslt.ExsltDatesAndTimes="urn:Exslt.ExsltDatesAndTimes" xmlns:Exslt.ExsltMath="urn:Exslt.ExsltMath" xmlns:Exslt.ExsltRegularExpressions="urn:Exslt.ExsltRegularExpressions" xmlns:Exslt.ExsltStrings="urn:Exslt.ExsltStrings" xmlns:Exslt.ExsltSets="urn:Exslt.ExsltSets" xmlns:umbraco.contour="urn:umbraco.contour" 
    exclude-result-prefixes="msxml umbraco.library Exslt.ExsltCommon Exslt.ExsltDatesAndTimes Exslt.ExsltMath Exslt.ExsltRegularExpressions Exslt.ExsltStrings Exslt.ExsltSets umbraco.contour ">

<xsl:output method="xml" omit-xml-declaration="yes" />

<xsl:param name="currentPage"/>

<xsl:variable name="documentTypeAlias" select="string('PdfItem')"/>

<xsl:variable name="size" select="@currentpage/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>

<xsl:template match="/">
<xsl:for-each select="$currentPage/node [@nodeTypeAlias = $documentTypeAlias and string(data [@alias='umbracoNaviHide']) != '0']">
    <div class="pdf">
        <a>
<xsl:attribute name="class">pdfmenu</xsl:attribute>
<xsl:attribute name="href"><xsl:value-of select="umbraco.library:GetMedia(./data[@alias='pdf'], 0)/data [@alias = 'umbracoFile']"/></xsl:attribute>


            <strong><xsl:value-of select="@nodeName"/><span>
            (<xsl:value-of select="$sizeAndSuffix"/>)</span></strong>
            <em><xsl:value-of select="data [@alias = 'PDFBeskrivelse']"/></em>
        <img>
        <xsl:attribute name="src"><xsl:value-of select="data [@alias = 'PDFBillede']"/></xsl:attribute>
        <xsl:attribute name="alt"></xsl:attribute>
        <xsl:attribute name="height">200</xsl:attribute>
        <xsl:attribute name="width">141</xsl:attribute>
        </img>
        </a>        
    </div>


</xsl:for-each>
</xsl:template>
</xsl:stylesheet>

1 个答案:

答案 0 :(得分:0)

好吧,我将size变量移动到for-each循环中,因为它因文件而异并修复了一些拼写错误:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE xsl:stylesheet [
  <!ENTITY nbsp "&#x00A0;">
]>
<xsl:stylesheet
    version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:msxml="urn:schemas-microsoft-com:xslt"
    xmlns:umbraco.library="urn:umbraco.library" xmlns:Exslt.ExsltCommon="urn:Exslt.ExsltCommon" xmlns:Exslt.ExsltDatesAndTimes="urn:Exslt.ExsltDatesAndTimes" xmlns:Exslt.ExsltMath="urn:Exslt.ExsltMath" xmlns:Exslt.ExsltRegularExpressions="urn:Exslt.ExsltRegularExpressions" xmlns:Exslt.ExsltStrings="urn:Exslt.ExsltStrings" xmlns:Exslt.ExsltSets="urn:Exslt.ExsltSets" xmlns:umbraco.contour="urn:umbraco.contour"
    exclude-result-prefixes="msxml umbraco.library Exslt.ExsltCommon Exslt.ExsltDatesAndTimes Exslt.ExsltMath Exslt.ExsltRegularExpressions Exslt.ExsltStrings Exslt.ExsltSets umbraco.contour ">

  <xsl:output method="xml" omit-xml-declaration="yes" />

  <xsl:param name="currentPage"/>

  <xsl:variable name="documentTypeAlias" select="string('PdfItem')"/>

  <xsl:template match="/">
    <xsl:for-each select="$currentPage/node [@nodeTypeAlias = $documentTypeAlias and string(data [@alias='umbracoNaviHide']) != '0']">
      <xsl:if test="string(data [@alias = 'pdf']) != ''">

        <xsl:variable name="size" select="umbraco.library:GetMedia(data[@alias='pdf'], 0)/data [@alias = 'umbracoFile']" />

        <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>

        <div class="pdf">
          <a>
            <xsl:attribute name="class">pdfmenu</xsl:attribute>
            <xsl:attribute name="href">
              <xsl:value-of select="umbraco.library:GetMedia(data[@alias='pdf'], 0)/data [@alias = 'umbracoFile']"/>
            </xsl:attribute>

            <strong>
              <xsl:value-of select="@nodeName"/>
              <span>
                (<xsl:value-of select="$sizeAndSuffix"/>)
              </span>
            </strong>
            <em>
              <xsl:value-of select="data [@alias = 'PDFBeskrivelse']"/>
            </em>
            <img>
              <xsl:attribute name="src">
                <xsl:value-of select="data [@alias = 'PDFBillede']"/>
              </xsl:attribute>
              <xsl:attribute name="alt"></xsl:attribute>
              <xsl:attribute name="height">200</xsl:attribute>
              <xsl:attribute name="width">141</xsl:attribute>
            </img>
          </a>
        </div>

      </xsl:if>
    </xsl:for-each>
  </xsl:template>
</xsl:stylesheet>

这应该可以解决问题。