我有一个带有许多p标签的xml文件 -
INPUT -
<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.
答案 0 :(得分:1)
此转化:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="p[*[1][self::inline-img]]">
<p channel="y.com">
<xsl:apply-templates/>
</p>
</xsl:template>
</xsl:stylesheet>
应用于以下XML文档(将提供的XML片段包装到单个顶部元素中 - 将其制作为格式良好的XML文档):
<t>
<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>
</t>
会产生想要的正确结果:
<t>
<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>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 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>"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>
</t>