XSLT to Remove Elements with Specific Attributes?

A

Aaron Davies

I'm new to XSLT, and thought I'd start off with (what I hoped was) a
fairly simple transform, but I can't make heads or tails of the
example code I've found around the web. I'd like to a transform that
copies input to output, except that elements of a specific type with
attributes with specific values (and all their content) are omitted
completely. An example would be processing XHTML and dropping all P
paragraphs with class="foo". I assume this is fairly easily derivable
from an identity filter, but I'm not sure where to start. Any help
would be much appreciated.
 
M

Martin Honnen

Aaron said:
I'm new to XSLT, and thought I'd start off with (what I hoped was) a
fairly simple transform, but I can't make heads or tails of the
example code I've found around the web. I'd like to a transform that
copies input to output, except that elements of a specific type with
attributes with specific values (and all their content) are omitted
completely. An example would be processing XHTML and dropping all P
paragraphs with class="foo". I assume this is fairly easily derivable
from an identity filter, but I'm not sure where to start. Any help
would be much appreciated.


<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xhtml="http://www.w3.org/1999/xhtml"
version="1.0">

<xsl:template match="xhtml:p[@class = 'foo']"/>

<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>

</xsl:stylesheet>
 
A

Aaron Davies

Aaron said:
I'm new to XSLT, and thought I'd start off with (what I hoped was) a
fairly simple transform, but I can't make heads or tails of the
example code I've found around the web. I'd like to a transform that
copies input to output, except that elements of a specific type with
attributes with specific values (and all their content) are omitted
completely. An example would be processing XHTML and dropping all P
paragraphs with class="foo". I assume this is fairly easily derivable
from an identity filter, but I'm not sure where to start. Any help
would be much appreciated.

<xsl:stylesheet
   xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
   xmlns:xhtml="http://www.w3.org/1999/xhtml"
   version="1.0">

   <xsl:template match="xhtml:p[@class = 'foo']"/>

   <xsl:template match="@* | node()">
     <xsl:copy>
       <xsl:apply-templates select="@* | node()"/>
     </xsl:copy>
   </xsl:template>

</xsl:stylesheet>

hmm. looks reasonable, but doesn't seem to work on the actual document
i'm trying to change.

i'm trying to hack up an rss feed, and the paragraphs i want to remove
are in the description section. actually, i'm not sure this is doable
with XSLT at all--on closer inspection, the description is a blob of
presumably opaque text, and what ends up as tags in the browser is
ampersand encoded in the actual feed.

should i give up and use sed? :)
 
M

Martin Honnen

Aaron said:
hmm. looks reasonable, but doesn't seem to work on the actual document
i'm trying to change.

i'm trying to hack up an rss feed, and the paragraphs i want to remove
are in the description section. actually, i'm not sure this is doable
with XSLT at all--on closer inspection, the description is a blob of
presumably opaque text, and what ends up as tags in the browser is
ampersand encoded in the actual feed.

RSS usually does not contain well-formed XHTML markup but rather escaped
HTML tag soup. So processing that escaped HTML tag soup with XML tools
like XSLT is difficult.
You could try to parse the escaped markup with an extension function
like
http://www.saxonica.com/documentation/extensions/functions/parse.html
but even that will only work if the escaped markup is well-formed XML
and not HTML tag soup.
 
T

The Magpie

Martin said:
RSS usually does not contain well-formed XHTML markup

RSS *ought* to contain well-formed XML or, to be more accurate,
properly constructed RDF (which is an XML content pattern). As to
whether or not it actually *does*....
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,225
Members
46,815
Latest member
treekmostly22

Latest Threads

Top