Umbraco 输出文件大小

发布于 2024-10-04 20:52:37 字数 3693 浏览 2 评论 0原文

我正在尝试为已上传的 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>

I'm trying to output the correct size for pdf files that has been uploaded.
But the only output is 0 - what am I doing wrong?

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE xsl:stylesheet [ <!ENTITY nbsp " "> ]>
<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 >= 1073741824">
                        <xsl:value-of select="format-number($size div 1073741824,'#,###')"/>
                        <xsl:text>GB</xsl:text>
                </xsl:when>
                <xsl:when test="$size >= 1048576">
                        <xsl:value-of select="format-number($size div 1048576,'#,###')"/>
                        <xsl:text>MB</xsl:text>
                </xsl:when>
                <xsl:when test="$size >= 1024">
                        <xsl:value-of select="format-number($size div 1024,'#,###')"/>
                        <xsl:text>KB</xsl:text>
                </xsl:when>
                <xsl:when test="$size > 0 and $size < 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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

听不够的曲调 2024-10-11 20:52:37

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

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE xsl:stylesheet [
  <!ENTITY nbsp " ">
]>
<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 >= 1073741824">
              <xsl:value-of select="format-number($size div 1073741824,'#,###')"/>
              <xsl:text>GB</xsl:text>
            </xsl:when>
            <xsl:when test="$size >= 1048576">
              <xsl:value-of select="format-number($size div 1048576,'#,###')"/>
              <xsl:text>MB</xsl:text>
            </xsl:when>
            <xsl:when test="$size >= 1024">
              <xsl:value-of select="format-number($size div 1024,'#,###')"/>
              <xsl:text>KB</xsl:text>
            </xsl:when>
            <xsl:when test="$size > 0 and $size < 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>

这应该可以解决问题。

Alright, I've move the size variable into the for-each loop, as it varies per file and fixed a few typos:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE xsl:stylesheet [
  <!ENTITY nbsp " ">
]>
<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 >= 1073741824">
              <xsl:value-of select="format-number($size div 1073741824,'#,###')"/>
              <xsl:text>GB</xsl:text>
            </xsl:when>
            <xsl:when test="$size >= 1048576">
              <xsl:value-of select="format-number($size div 1048576,'#,###')"/>
              <xsl:text>MB</xsl:text>
            </xsl:when>
            <xsl:when test="$size >= 1024">
              <xsl:value-of select="format-number($size div 1024,'#,###')"/>
              <xsl:text>KB</xsl:text>
            </xsl:when>
            <xsl:when test="$size > 0 and $size < 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>

That should do the trick.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文