Replace href value in anchor tags of html using XSLT -
i want replace value of href tags in html using xslt. example: if anchor tag <a href="/dir/file1.htm" />, want replace href value this: <a href="http://site/dir/file1.htm" />. point want replace relative urls absolute values.
i want anchor tags in html content. how can using xslt?
thanks.
edit: google appliance. display results in frame , links doesn't work in cached page. takes address bar url root. here html in form of string, , displays html based on condition. can suggest way replace href tags in string?
this xslt 1.0 transformation:
<xsl:stylesheet version="1.0"  xmlns:xsl="http://www.w3.org/1999/xsl/transform">  <xsl:output method="xml" omit-xml-declaration="yes" indent="yes"/>   <xsl:param name="pservername" select="'http://myserver'"/>   <xsl:template match="node()|@*">   <xsl:copy>    <xsl:apply-templates select="node()|@*"/>   </xsl:copy>  </xsl:template>   <xsl:template match="a/@href[not(starts-with(.,'http://'))]">   <xsl:attribute name="href">    <xsl:value-of select="concat($pservername, .)"/>   </xsl:attribute>  </xsl:template> </xsl:stylesheet>   when applied xml document:
<html>  <a href="/dir/file1.htm">link 1</a>  <a href="/dir/file2.htm">link 2</a>  <a href="/dir/file3.htm">link 3</a> </html>   produces wanted, correct result:
<html>     <a href="http://myserver/dir/file1.htm">link 1</a>     <a href="http://myserver/dir/file2.htm">link 2</a>     <a href="http://myserver/dir/file3.htm">link 3</a> </html>   ii. xslt 2.0 solution:
in xpath 2.0 1 can use standard function resolve-uri()
this transformation:
<xsl:stylesheet version="2.0"  xmlns:xsl="http://www.w3.org/1999/xsl/transform"  xmlns:xs="http://www.w3.org/2001/xmlschema">   <xsl:variable name="vbaseuri" select="'http://myserver/ttt/x.xsl'"/>   <xsl:template match="/">   <xsl:value-of select="resolve-uri('/mysite.aspx', $vbaseuri)"/>  </xsl:template> </xsl:stylesheet>   when applied on xml document (not used), produces wanted, correct result:
http://myserver/mysite.aspx   if stylesheet module comes same server relative urls resolved, there no need pass base uri in parameter -- doing following produces wanted result:
<xsl:stylesheet version="2.0"  xmlns:xsl="http://www.w3.org/1999/xsl/transform"  xmlns:xs="http://www.w3.org/2001/xmlschema">   <xsl:variable name="vbaseuri">   <xsl:for-each select="document('')">    <xsl:sequence select="resolve-uri('')"/>   </xsl:for-each>  </xsl:variable>    <xsl:template match="/">   <xsl:value-of select="resolve-uri('/mysite.aspx', $vbaseuri)"/>  </xsl:template> </xsl:stylesheet>      
Comments
Post a Comment