user1937895
user1937895

Reputation: 39

xsl replace one link with another

I manage a site which was owned by one organization (ie www.old.com) and has now been replaced with a new organization (ie www.new.com). My problem is that some of the links still point to the old organization on some of the images we use on the site. I was wondering if there was a way to replace http://www.old.com with http://www.new.com using xsl.

My thought was doing this but not sure if it will work..

 <xsl:template match="image">
<xsl:param name ="old" value='http://www.old.com'/>
 <xsl:param name ="new" value='http://www.new.com'/>
  <xsl:choose>
          <xsl:when test="contains(@xlink:href,$old)">
            <xsl:attribute name="xlink:href">
              <xsl:value-of select="replace(@xlink:href, $old, $new)">
            </xsl:attribute>
          </xsl:when>
          <xsl:otherwise>
            <xsl:attribute name="xlink:href">
              <xsl:value-of select="@xlink:href"/>
            </xsl:attribute>
          </xsl:otherwise>
        </xsl:choose>
</xsl:template>

The xml looks like this..

<book title="Harry Potter">
 <description
 xlink:type="simple"
  xlink:href="http://www.old.com/images/HPotter.gif"
  xlink:show="new">
 As his fifth year at Hogwarts School of Witchcraft and
 Wizardry approaches, 15-year-old Harry Potter is.......
  </description>
</book>

Thanks for your help

Upvotes: 0

Views: 415

Answers (1)

Lucero
Lucero

Reputation: 60190

Assuming an apply-templates that traverses all nodes (see first template below), the following should work:

<xsl:template match="node()|@*">
  <xsl:copy>
    <xsl:apply-templates select="node()|@*"/>
  </xsl:copy>
</xsl:template>

<xsl:template match="@xlink:href">
  <xsl:param name ="old">http://www.old.com</xsl:param>
  <xsl:param name ="new">http://www.new.com</xsl:param>
  <xsl:attribute name="xlink:href">
    <xsl:value-of select="replace(., $old, $new)"/>
  </xsl:attribute>
</xsl:template>

Upvotes: 1

Related Questions