【问题标题】:XSLT to display news item images with consistent sizeXSLT 以一致的大小显示新闻项目图像
【发布时间】:2013-02-27 23:50:22
【问题描述】:

我有一个 RSS XML 新闻文件,其中包含一个项目列表,其中包括一个图像的 URL。我还有一个关联的 XSLT。

问题是图像大小不一致,我想限制图像大小,调整它们的大小,使其成为漂亮的缩略图。

我将如何修改 XSLT 来实现这一点?

XML 示例:

<?xml version="1.0" encoding="UTF-8" ?>
<rss version ="2.0" xmlns:g="http://base.google.com/ns/1.0">
    <channel>
        <title>Company Name</title>
        <description>Company description</description>
        <link>http://www.mycompanyurl.com</link>

        <item>
            <title>News Item Title</title>
            <link>http://www.whateverurl.com/</link>
            <category>Space</category>
            <pubDate>12 April 1961</pubDate>
            <description>Software to reduce your job search to a half hour per day. all major job sites, job boards, classifieds. unemployment paperwork, CRM, interviews, more</description>
            <image>
                <url>~/App_Data/NewsControl/whatever.png</url>
                <title>Whatever1</title>
                <link>javascript:void(0)</link>
            </image>
            <g:id>1</g:id>
            <g:brand>Whatever2</g:brand>
            <g:condition>whatever3</g:condition>
            <g:price>$whatever4</g:price>
            <g:product_type>Whatever5</g:product_type>
        </item>
    </channel>
</rss>

这里是相关的 XSLT:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:template match="/">
    <items>
      <xsl:for-each select="//item">
        <item Name="{position()}" HeaderText="{title}" Text="{description}" NavigateUrl="{position()}" Date="{pubDate}" ImageUrl="{image/url}"/>
      </xsl:for-each>
    </items>
  </xsl:template>
</xsl:stylesheet>

第一次回答的结果

<items>
    <xsl:for-each select="//item">
       <item Name="{position()}" HeaderText="{title}" Text="{description}" NavigateUrl="{position()}" Date="{pubDate}" ImageUrl="/Tools/thumber.php?img={image/url}"/>
    </xsl:for-each>
</items>

我进行了这些更改,在服务器上启用了 PHP(从服务器和本地测试),并发现了 2 个问题: 1. 我没有得到图像,只是一个没有图像的框。

  1. 如果我尝试编辑 ImageUrl 并添加“&W=xxx&H=xxx”,Visual Studio 验证器会抱怨并在 & 上抛出错误。

更新 2 这是 XSLT 中的最新行: http://myserver.com/Tools/thumber.php?img=',image/url)}"/>

XML 中对应的图片部分

<image>
    <url>/Products/Jobfish/Images/Boxshots/Jobfish_DVDCaseCD_ShadowOut.jpg</url>
    <title>Jobfish</title>
    <link>javascript:void(0)</link>

【问题讨论】:

  • 这个/Tools/thumber.php?img={image/url} 是错误的,您不能仅将属性的一部分作为表达式进行评估 - 它应该是{concat('/Tools/thumber.php?img=',image/url)}

标签: image xslt image-resizing image-size


【解决方案1】:

XSLT 没有用于调整大小或缩略图的内置函数。您将不得不为此使用外部处理器,例如。通过使用 PHP 缩略图生成器。

然后将原始图像路径替换为指向缩略图生成器的 URL,源为原始图像。

假设 ImageUrl = mediaserver.xyz/ourlogo.jpg 新的 ImageUrl 将变为 myserver.com/thumbnailgenerator.php?src=http://mediaserver.xyz/ourlogo.jpg

请确保您选择缓存缩略图库(例如 https://code.google.com/p/phpthumbmaker/wiki/ThumberWiki ),因为如果您跳过它,这将是一个严重的资源消耗。在重新提供这些缩略图时还要考虑版权问题。

【讨论】:

  • 感谢您的建议。我下载了Thumber并在我的帖子更新中添加了代码,但是我得到VS抱怨&符号,更重要的是没有缩略图,只是一个没有图像的框。
  • 更新:抱歉,我没有正确阅读您的问题:图像不在您的服务器上,并且建议的 phpthumbmaker 不是获取外部内容的正确 php 库。所以请不要使用那个来获取外部图像。另请参阅“我如何获得外部站点的调整大小的图像?”关于stackoverflow的问题
  • 目前所有图片都是本地的。在 XML 文件中,我使用构造“/images/xxxxx.png”。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-05-11
  • 1970-01-01
  • 2017-06-06
  • 2010-11-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多