XSLT to Remove Elements with Specific Attributes?

Discussion in 'XML' started by Aaron Davies, Feb 6, 2009.

  1. Aaron Davies

    Aaron Davies Guest

    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.
     
    Aaron Davies, Feb 6, 2009
    #1
    1. Advertising

  2. Aaron Davies wrote:
    > 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>

    --

    Martin Honnen
    http://JavaScript.FAQTs.com/
     
    Martin Honnen, Feb 6, 2009
    #2
    1. Advertising

  3. Aaron Davies

    Aaron Davies Guest

    On Feb 6, 7:31 pm, Martin Honnen <> wrote:
    > Aaron Davies wrote:
    > > 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? :)
     
    Aaron Davies, Feb 9, 2009
    #3
  4. Aaron Davies wrote:

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




    --

    Martin Honnen
    http://JavaScript.FAQTs.com/
     
    Martin Honnen, Feb 9, 2009
    #4
  5. Aaron Davies

    The Magpie Guest

    Martin Honnen wrote:
    > Aaron Davies wrote:
    >
    >> 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
    >


    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*....
     
    The Magpie, Feb 11, 2009
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Colin Mackenzie
    Replies:
    0
    Views:
    1,426
    Colin Mackenzie
    Jul 10, 2003
  2. Johannes Koch
    Replies:
    1
    Views:
    829
    Martin Honnen
    Mar 5, 2004
  3. Gerald Aichholzer
    Replies:
    2
    Views:
    2,623
    Gerald Aichholzer
    Jun 27, 2006
  4. hisachu
    Replies:
    0
    Views:
    1,569
    hisachu
    Sep 1, 2010
  5. Kabindra
    Replies:
    3
    Views:
    203
    David Mark
    Jan 21, 2010
Loading...

Share This Page