2

在我的 XML 文档中,我正在提取<TextBlock>包含图像的内容。XML 显示:

<img src="/templates_soft/images/facebook.png" alt="twitter" />

当我查看该页面时,该图像未显示,因为它与原始页面的路径不同。

我需要添加要显示的图像的 URL 的其余部分。像http://www.mypage.com/这样的东西,图像显示从http://www.mypage.com/templates_soft/images/facebook.png

有没有办法做到这一点?

4

2 回答 2

2

使用

<img src="{$imageBase/}templates_soft/images/facebook.png" alt="twitter" />

其中 xsl:variable 命名$imageBase被定义为包含必要的前缀(在您的情况下"http://www.mypage.com")。

这是一个完整的 XSLT 解决方案

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>

 <xsl:param name="pimageBase" select="'http://www.mypage.com'"/>

    <xsl:template match="img">
   <img src="{concat($pimageBase, @src)}" alt="{@alt}"/>
  </xsl:template>
</xsl:stylesheet>

当此转换应用于以下 XML 文档时

<img src="/templates_soft/images/facebook.png" alt="twitter" />

产生了想要的正确结果

<img src="http://www.mypage.com/templates_soft/images/facebook.png" alt="twitter"/>
于 2011-01-18T17:04:10.230 回答
0

如果您使用 XSLT,您只需根据需要创建一个包含整个 URL 的 XML,然后标记 XSLT,使其包含指向 XML 文件中原始字段的“指针”。如果您要绑定到控件(如 Grid),则可以行绑定并在该点添加信息,如果这样做比 XSLT 更容易的话。

于 2011-01-18T17:03:16.337 回答