Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xslt/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Xslt xsl将一个链接替换为另一个链接_Xslt - Fatal编程技术网

Xslt xsl将一个链接替换为另一个链接

Xslt xsl将一个链接替换为另一个链接,xslt,Xslt,我管理一个网站,该网站由一个组织(即www.old.com)拥有,现在已被一个新组织(即www.new.com)取代。我的问题是,一些链接仍然指向我们在网站上使用的一些图像上的旧组织。我想知道是否有一种方法可以替换为使用xsl 我的想法是这样做的,但不确定它是否会起作用 <xsl:template match="image"> <xsl:param name ="old" value='http://www.old.com'/> <xsl:param name

我管理一个网站,该网站由一个组织(即www.old.com)拥有,现在已被一个新组织(即www.new.com)取代。我的问题是,一些链接仍然指向我们在网站上使用的一些图像上的旧组织。我想知道是否有一种方法可以替换为使用xsl

我的想法是这样做的,但不确定它是否会起作用

 <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>

xml如下所示

<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>

作为他在霍格沃茨魔法学校的第五年
魔法降临,15岁的哈利·波特。。。。。。。

感谢您的帮助

假设一个
应用模板
遍历所有节点(请参见下面的第一个模板),则以下操作应该有效:

<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>

http://www.old.com
http://www.new.com

您的源XML是什么?看起来您正在与
进行匹配,但这不是HTML;您没有创建
img
元素,即使这样,它也应该是
src
属性,而不是
href
属性。请更清楚地指定输入和输出格式。您好。我应该用xlink而不是link。该死!!我的xml看起来像这样(我目前没有源代码,但这是一个很好的例子)。。随着他在霍格沃茨魔法学校五年级的临近,15岁的哈利·波特。。。。。。。在您在注释中给出的示例中,XSL模板甚至不匹配……修复了XSL并将xml添加到示例中。希望它是正确的。但是,我是个新手,所以我不能100%确定您使用的是XSLT1.0还是XSLT2.0@卢塞罗的解决方案假设后者。谢谢我将不得不添加一个。。因为有些xml包含,有些包含,所以我需要代码来测试哪个是哪个,并应用适当的“@xlink:href”值否,replace仅在找到匹配项时才进行替换,这使得调用
contains
是不必要的。语义是一样的。我不知道。当我明天在工作中测试它时,我会让你知道它是如何工作的。谢谢你今天的帮助。