Link to home
Start Free TrialLog in
Avatar of alicia1234
alicia1234Flag for United States of America

asked on

How to strip out only the <p> tags that I want?

See XML code below.
There could be any number of "Blank" <p> tags.
In the example, what I want to show on the page is ONLY this:


<p>This is a test program using the new template that gets the correct left-hand menu (a list of categories).</p>
<p>This would be a second paragraph.</p>

How can I do this in the xslt? (I'm fairly new with xslt). I thought I should be able to use an "apply-templates" on the "Teaser/p" tags and then in the template, only display the <p> tag if it is not blank and does not contain an image tag. But I can't quite figure out the right xpaths and syntax for the if tests.

For example, I had <xsl:apply-templates select="Teaser/p" />

Then I had a template that looks like this but it doesn't work ... actually, it only displays the image! (I'm really out of my league here)...

<xsl:template match="Teaser/p">
<xsl:if test=". != ''">
<p>
<xsl:copy-of select="." />
<p>
</xsl:template>
</xsl:template>

Thanks.




<Teaser>
    	<p>
<img src="/uploadedImages/CCA/Content/Forms/ADP_logo.gif" alt="ADP logo" title="ADP logo" />
      </p>
      <p> </p>
      <p>This is a test program using the new template that gets the correct left-hand menu (a list of categories).</p>
<p>This would be a second paragraph.</p>
<p>&#160;</p>
</Teaser>

Open in new window

Avatar of Michael
Michael

You should be able to open this in notepad (by rightclicking on it and go to "Open with" and if that option is not available, click "Chose program" and chose notepad) and use the find and replace function by going to "Edit" then "Replace".
SOLUTION
Avatar of zc2
zc2
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Try this

      <xsl:template match="Teaser/p[not(normalize-space(translate(., '&#160;', '')))][not(*)]"/>
      <xsl:template match="Teaser/p[normalize-space(translate(., '&#160;', '')) or *]">
            <xsl:copy-of select="." />
      </xsl:template>

The surprise comes from the fact that != does a set compare
to evaluate the differences of the solutions
- I don't understand what TenKantrus means, I assume he posted to the wrong question :-)
- my solution keeps the apply-templates approach you were using, you can simply replace your one template with my two templates. I also keep
- zc2's solution count on the fact that a Teaser can always only have p children and will drop the whitespace nodes between the p. zc2 will also drop the img only p.

Seems that you want to drop the image only p
I have included a new version taht also drops the image

I am by the way in favour of maintaining the original xsl:apply-templates approach, that is a better XSLT approach
<xsl:template match="Teaser/p[not(normalize-space(translate(., '&#160;', '')))]"/>
      <xsl:template match="Teaser/p[normalize-space(translate(., '&#160;', ''))]">
            <xsl:copy-of select="." />
      </xsl:template>

Open in new window

Is your <xsl:apply-templates select="Teaser/p" />
in the right context?

you should get more than just the image
Gertone,

Why you use the empty template for?
Isn't the result be the same without one?
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of alicia1234

ASKER

Gertrone:
Could you explain to me what this is actually doing?

<xsl:apply-templates select="Teaser/p[normalize-space(translate(., '&#160;', ''))]" />

zc2 & Gertrone:

Each of your solutions produce the same result. They do display the text that I want. However - the two lines (see my original post) of text are to be two separate paragraphs, and they are not coming out that way. They are lumped together into a single <p> tag.

See: http://screencast.com/t/5Tw9B7MNCUNN
Instead of selecting all Teaser/p elements,
you select all Teaser/p elements that have text in it

translate(., '&#160;', '') will replace the character 160 with nothing
normalize-space will drop the trailing and leading whitespace (and makes each sequence of white-space equal to one space)

normalize-space(translate(., '&#160;', '')) will turn every node that has only white-space in it into an empty string
[] puts a condition on the p
a string as a boolean expression will be true() unless it is empty

Teaser/p[normalize-space(translate(., '&#160;', ''))] selects all the Teaser/p elements that are not empty

Well, something transforms your end-result into a png image, so it is hard for us to debug.
To my surprise I saw that you were duplicating the p tags in your earliest template.
Apparently you have a process in place that removes one level of <p> further down the road

Either find out what that process is, or duplicate the <p> again
<xsl:template match="Teaser/p">
        <p>
            <xsl:copy-of select="." />
        <p>
      </xsl:template>
Never mind my comment about it not separating out the <p> tags. During my testing I had inserted another xml tag into the html to try out something different. When I got that out of the way, both of your solutions worked.

Points to both of you because zc2 was first with a solution that would work, but Gertrone gave what seems to be "better" solution and lots of explanation.

Sorry about that ... I do have other xsl that displays the image separately, that's why you see it.
I needed to format the image separately instead of displaying it in context with the original html.