[XSL-LIST Mailing List Archive Home] [By Thread] [By Date]

[xsl] replacing selective p tags with tags with attributes


Subject: [xsl] replacing selective p tags with tags with attributes
From: "strip513 ." <arkle.ash@xxxxxxxxx>
Date: Thu, 18 Apr 2013 22:38:40 -0400

        Hey guys,

        I have an xml file with many p tags --

        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>


        INPUT ENDS HERE

        I want all the p tags which are above inline img class to be
replaced by <p channel="y.com"> and it will look like

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

        I dont want to replace any other normal p tags.So, in this
particular example find and replace will happen at two places.

        I used this code,but it is not working.Please advise.Thanks.

        XSLT CODE using xslt 1.0




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


Current Thread
Keywords