xsl-list
[Top] [All Lists]

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

2013-04-18 22:30:50
strip513,

Did you know that cross-posting is evil?

See this answer to your identical question asked at SO:

http://stackoverflow.com/a/16096705/36305



On Thu, Apr 18, 2013 at 8:02 PM, G. Ken Holman
<gkholman(_at_)cranesoftwrights(_dot_)com> wrote:
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(_at_)CraneSoftwrights(_dot_)com |
Google+ profile: https://plus.google.com/116832879756988317389/about |
Legal business disclaimers:    http://www.CraneSoftwrights.com/legal |



--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: 
<mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--




-- 
Cheers,
Dimitre Novatchev
---------------------------------------
Truly great madness cannot be achieved without significant intelligence.
---------------------------------------
To invent, you need a good imagination and a pile of junk
-------------------------------------
Never fight an inanimate object
-------------------------------------
To avoid situations in which you might make mistakes may be the
biggest mistake of all
------------------------------------
Quality means doing it right when no one is looking.
-------------------------------------
You've achieved success in your field when you don't know whether what
you're doing is work or play
-------------------------------------
Facts do not cease to exist because they are ignored.
-------------------------------------
Typing monkeys will write all Shakespeare's works in 200yrs.Will they
write all patents, too? :)
-------------------------------------
I finally figured out the only reason to be alive is to enjoy it.

--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--

<Prev in Thread] Current Thread [Next in Thread>