Re: [xsl] replacing selective p tags with tags with attributes

Subject: Re: [xsl] replacing selective p tags with tags with attributes
From: "G. Ken Holman" <gkholman@xxxxxxxxxxxxxxxxxxxx>
Date: Thu, 18 Apr 2013 23:02:45 -0400
At 2013-04-18 22:38 -0400, strip513 . wrote:
Hey guys,

I have an xml file with many p tags --

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

I advise you to look at XSLT differently, so I'm glad you posted. You are treating XSLT imperatively, in a manner like Java or C or Python, and not taking advantage of what XSLT can do for you. And you are trying to create angle brackets in your strings, which is entirely inappropriate for XSLT.


Consider the working solution below that does what you ask. It is written declaratively and it looks at the input as a tree of nodes, not as a string of angle brackets. I declare in my stylesheet what I want the output to look like for a given input element. I've characterized what is special about <p> element nodes that need to be written out differently, otherwise, all element nodes are simply copied to the result tree as they are found in the input tree.

You need to approach solutions in a manner appropriate to the processing model (declarative) and data model (nodes) of XSLT. Using purely imperative approaches and putting angle strings will not help you and will hinder your use of the language.

XSLT is built for XML and with XML, it is not a language that simply supports XML from function calls.

I hope you find this helpful.

. . . . . . . Ken


T:\ftemp>type arkle.xml <?xml version="1.0" encoding="UTF-8"?> <test> <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> </test>

T:\ftemp>call xslt arkle.xml arkle.xsl
<?xml version="1.0" encoding="utf-8"?><test>
  <p channel="y">
              <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">
              <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>
</test>
T:\ftemp>type arkle.xsl
<?xml version="1.0" encoding="US-ASCII"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
                version="1.0">

<xsl:template match="p[inline-img]">
  <p channel="y">
    <xsl:apply-templates/>
  </p>
</xsl:template>

<xsl:template match="@*|node()"><!--identity for all other nodes-->
  <xsl:copy>
    <xsl:apply-templates select="@*|node()"/>
  </xsl:copy>
</xsl:template>

</xsl:stylesheet>
T:\ftemp>rem Done!


-- Contact us for world-wide XML consulting and instructor-led training | Free 5-hour lecture: http://www.CraneSoftwrights.com/links/udemy.htm | Crane Softwrights Ltd. http://www.CraneSoftwrights.com/s/ | G. Ken Holman mailto:gkholman@xxxxxxxxxxxxxxxxxxxx | Google+ profile: https://plus.google.com/116832879756988317389/about | Legal business disclaimers: http://www.CraneSoftwrights.com/legal |

Current Thread