XSLT将p标记替换为带属性的p标记



我有一个xml文件,里面有很多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>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>

输入到此结束

我想把所有在inline img class上面的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标签。因此,在这个特殊的例子中,查找和替换将在两个地方发生。我用这个代码,但它不工作。请advise.Thanks。

使用XSLT 1.0的XSLT CODE

 <?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="'&lt;p&gt;&lt;inline-img class='" />
                                <xsl:with-param name="by"
select="'&lt;p channel="y.com"&gt;&lt;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.

这一转换:

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

相关内容

  • 没有找到相关文章

最新更新