我有一个带有许多 p 标签的 xml 文件——
输入 -
<p>
<inline-img class="full">
<web-main-photo>
<photo src="http://yahoo.com/test.jpg"/>
</web-main-photo>
<data>
<br/>
<caption>
<p> (Nick Wass/Associated Press)</p>
</caption>
</data>
</inline-img>
</p>
<p>this is content </p>
<p>After missing the past three games with an injured left
leg, Martin Erat will return to the <a href="/blog/">Capitals</a>'
lineup Saturday night at Verizon Center against the Tampa Bay
Lightning.</p>
<p>Erat, 31, suffered the injury on April 6 at Florida
when Panthers defenseman Erik Gudbranson delivered a late hit from
behind that sent the veteran winger awkwardly into the boards. He
avoided significant harm, though, and after five consecutive days of
skating Erat said he's prepared to get back to game action.</p>
<p>
<inline-img class="full">
<web-main-photo>
<photo src="http://yahoo.com/test.jpg"/>
</web-main-photo>
<data>
<br/>
<caption>
<p> (Nick Wass/Associated Press)</p>
</caption>
</data>
</inline-img>
</p>
<p>"Feeling good. Feels better every day, I'm pretty much
ready to go," Erat said, adding that while he would have much rather
been playing games with his new team the time to take part in practice
and watch the system work from above should help him make a smooth
transition.</p>
输入到此结束
我希望替换内联 img 类上方的所有 p 标签,它看起来像
<p channel="y.com">
<inline-img class="full">
<web-main-photo>
<photo src="http://yahoo.com/test.jpg"/>
</web-main-photo>
<data>
<br/>
<caption>
<p> (Nick Wass/Associated Press)</p>
</caption>
</data>
</inline-img>
</p>
我不想替换任何其他正常的 p 标签。所以,在这个特定的示例中,查找和替换将发生在两个地方。我使用了这个代码,但它不起作用。请告知。谢谢。
使用 xslt 1.0 的 XSLT 代码
<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<component>
<p>
<xsl:call-template name="replace-text">
<xsl:with-param name="text"
select="/item/content" />
<xsl:with-param name="replace"
select="'<p><inline-img class='" />
<xsl:with-param name="by"
select="'<p channel="y.com"><inline-img class='" />
</xsl:call-template>
</p>
</component>
</xsl:template>
<xsl:template name="replace-text">
<xsl:param name="text"/>
<xsl:param name="replace" />
<xsl:param name="by" />
<xsl:choose>
<xsl:when test="contains($text, $replace)">
<xsl:value-of select="substring-before($text, $replace)"/>
<xsl:value-of select="$by" disable-output-escaping="yes"/>
<xsl:call-template name="replace-text">
<xsl:with-param name="text"
select="substring-after($text, $replace)"/>
<xsl:with-param name="replace" select="$replace" />
<xsl:with-param name="by" select="$by" />
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$text"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
Thanks so much for your help.I really appreciate it.