【问题标题】:Extract image from CDATA-xml rss file into xslt从 CDATA-xml rss 文件中提取图像到 xslt
【发布时间】:2013-03-22 08:58:31
【问题描述】:

我需要显示有关 xml RSS 提要的 xslt 信息。 xml来源为:

<description><![CDATA[<p>
<img style="margin: 10px; 
     float: left;" alt="Nuevo modelo general de negocio" 
     src="http://mysite.es/images/figure1.jpg" width="196" height="147" />
     La compañía apuesta por un marcado giro en el modelo]]>
</description>

我正在使用:

<xsl:value-of select="description" disable-output-escaping="yes"/>

但是渲染效果不好,因为我需要显示一个调整大小的图像,大小例如 70x70。

我试过了,但它错了:

<xsl:value-of select="replace("description","images/","images/resized/images/")" 
   disable-output-escaping="yes"/>

对我来说完美的解决方案是从标签中提取分离的 src 属性和文本。

问候, 玛丽亚

【问题讨论】:

  • 请在此处查看答案stackoverflow.com/questions/8273065/…
  • 您可以使用 XSLT 3.0 (XPath 3.0) 轻松完成此操作。您对 XSLT 3.0 解决方案感兴趣吗?或者,对于 .NET XslCompiledTransform,使用我在这里描述的技术:stackoverflow.com/a/8273277/36305
  • 抱歉,我是 xslt 的新手,无法回答您的问题...我正在 Sharepoint Foundation 2010 中创建一个 webpart。

标签: image xslt cdata


【解决方案1】:

如果你的 xml 总是像你的例子那么你应该可以使用这样的东西:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
exclude-result-prefixes="xsl">

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

  <xsl:template match="/">
    <div>
      <xsl:apply-templates select="rss/channel"/>
    </div>
  </xsl:template>
  <xsl:template match="rss/channel">
    <ul>
      <xsl:apply-templates select="item"/>
    </ul>
  </xsl:template>
  <xsl:template match="item">
    <xsl:variable name="item_link" select="link"/>
    <xsl:variable name="item_title" select="substring-after(description, '/&gt;')"/>
    <xsl:variable name="item_image" select="substring-before(substring-after(description, 'src=&quot;'), '&quot;')" />
    <li>
      <a href="{$item_link}">
        <img alt="" src="{$item_image}" width="70" height="70" />
        <xsl:value-of select="$item_title"/>
      </a>
    </li>
  </xsl:template>
</xsl:stylesheet>

【讨论】:

  • Perfect Per,使用您的代码,我可以毫无问题地读取 src 属性...谢谢
猜你喜欢
  • 2011-07-10
  • 2015-10-13
  • 1970-01-01
  • 2016-06-05
  • 1970-01-01
  • 2019-02-15
  • 2011-04-09
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多