XSLT将URL从相对添加到绝对
在我的XML文档中,我正在提取包含图像的XSLT将URL从相对添加到绝对,url,xslt,absolute-path,relative-url,Url,Xslt,Absolute Path,Relative Url,在我的XML文档中,我正在提取包含图像的的内容。XML显示: <img src="/templates_soft/images/facebook.png" alt="twitter" /> 当我查看页面时,图像不会显示,因为它与原始页面的路径不同 我需要添加图像显示的URL的其余部分。类似于http://www.mypage.com/以便从http://www.mypage.com/templates_soft/images/facebook.png 有办法做到这一点吗?如果使
的内容。XML显示:
<img src="/templates_soft/images/facebook.png" alt="twitter" />
当我查看页面时,图像不会显示,因为它与原始页面的路径不同
我需要添加图像显示的URL的其余部分。类似于http://www.mypage.com/
以便从http://www.mypage.com/templates_soft/images/facebook.png
有办法做到这一点吗?如果使用XSLT,只需创建一个包含所需整个URL的XML,然后标记XSLT,使其包含指向XML文件中原始字段的“指针”。如果要绑定到控件(如网格),则可以在该点进行行绑定并添加信息,前提是这样做比XSLT更容易。使用:
<img src="{$imageBase/}templates_soft/images/facebook.png" alt="twitter" />
<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>
<img src="/templates_soft/images/facebook.png" alt="twitter" />
<img src="http://www.mypage.com/templates_soft/images/facebook.png" alt="twitter"/>
当此转换应用于以下XML文档时:
<img src="{$imageBase/}templates_soft/images/facebook.png" alt="twitter" />
<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>
<img src="/templates_soft/images/facebook.png" alt="twitter" />
<img src="http://www.mypage.com/templates_soft/images/facebook.png" alt="twitter"/>
生成所需的正确结果:
<img src="{$imageBase/}templates_soft/images/facebook.png" alt="twitter" />
<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>
<img src="/templates_soft/images/facebook.png" alt="twitter" />
<img src="http://www.mypage.com/templates_soft/images/facebook.png" alt="twitter"/>
问得好,+1。有关完整而简短的XSLT解决方案,请参见我的答案。:)除了@Dimitre的好答案外,还有一些案例表明wich是正确的选择。