Using XSL to delete fixed text in arbitrary elements

Discussion in 'XML' started by Fred Bartlett, Feb 17, 2004.

  1. Given this:
    <a>..<b>..<b/><c>..</c>Stuff<d>..</d>..</a>
    and this:
    <a>..<b>..<b/>Stuff<d>..</d>..</a>
    How can I delete 'Stuff' from both? 'Stuff' always appears in the
    content of <a> and before the tag <d>, but there's no way to tell
    which of several (approx 6, not just two) alternatives appears before
    'Stuff'.

    Thanks,
    Fred
     
    Fred Bartlett, Feb 17, 2004
    #1
    1. Advertising

  2. "Fred Bartlett" <> wrote in message
    news:...
    > Given this:
    > <a>..<b>..<b/><c>..</c>Stuff<d>..</d>..</a>
    > and this:
    > <a>..<b>..<b/>Stuff<d>..</d>..</a>
    > How can I delete 'Stuff' from both? 'Stuff' always appears in the
    > content of <a> and before the tag <d>, but there's no way to tell
    > which of several (approx 6, not just two) alternatives appears before
    > 'Stuff'.
    >
    > Thanks,
    > Fred


    Both of the above are not well-formed xml documents.

    Probably you meant:

    <a>..<b>..</b><c>..</c>Stuff<d>..</d>..</a>

    instead of

    > <a>..<b>..<b/><c>..</c>Stuff<d>..</d>..</a>



    and


    > <a>..<b>..</b>Stuff<d>..</d>..</a>


    instead of

    > <a>..<b>..<b/>Stuff<d>..</d>..</a>


    The wanted transformation is a simple override of the identity rule:

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

    <xsl:eek:utput omit-xml-declaration="yes"/>

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

    <xsl:template match="text()[.='Stuff'][following-sibling::*[self::d]]"/>

    </xsl:stylesheet>

    In the transformation above an empty rule is added (,which effectively
    ignores or discards or "deletes" all matching nodes from the generated
    output) matching a text node, whose value is the string 'Stuff' and whose
    immediate following sibling is a "d" element.

    The results for both xml source documents (corrected to be wellformed) are
    respectively:

    <a>..<b>..</b><c>..</c><d>..</d>..</a>

    and

    <a>..<b>..</b><d>..</d>..</a>




    Hope this helped.

    Cheers,

    Dimitre Novatchev [XML MVP],
    FXSL developer, XML Insider,

    http://fxsl.sourceforge.net/ -- the home of FXSL
    Resume: http://fxsl.sf.net/DNovatchev/Resume/Res.html
     
    Dimitre Novatchev [MVP XML], Feb 17, 2004
    #2
    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. Replies:
    1
    Views:
    3,613
    A. Bolmarcich
    May 27, 2005
  2. Rob Smegma
    Replies:
    1
    Views:
    1,818
    shaun
    Sep 26, 2005
  3. Honestmath
    Replies:
    5
    Views:
    562
    Honestmath
    Dec 13, 2004
  4. Rob Clewley
    Replies:
    2
    Views:
    356
    Rob Clewley
    Aug 11, 2008
  5. John Nagle
    Replies:
    6
    Views:
    98
    Jeremy J Starcher
    Feb 12, 2008
Loading...

Share This Page