Deleting tags via XSL

Discussion in 'XML' started by Thomas Vackier, Dec 12, 2003.

  1. Hi all,

    I'm trying to delete a certain tag in an XML document using XSL, but I
    can't figure out how.

    Here's how my XML looks like:

    <root>
    <element>
    <name>This is text</name>
    </element>
    <element>
    <name>
    <TBT>This is text</TBT>
    </name>
    </element>
    ..
    ..
    ..
    </root>

    Sometimes the text is enclosed between <TBT> tags, sometimes it is
    not. These tags were added to be able to translate it more easily into
    other languages (where TBT stands for To Be Translated).

    Is there an easy way to write an XSL to delete all the TBT tags or
    produce an XML without these tags?

    Many thanks,

    Thomas Vackier
     
    Thomas Vackier, Dec 12, 2003
    #1
    1. Advertising

  2. Thomas Vackier wrote:

    > I'm trying to delete a certain tag in an XML document using XSL, but I
    > can't figure out how.
    >
    > Here's how my XML looks like:
    >
    > <root>
    > <element>
    > <name>This is text</name>
    > </element>
    > <element>
    > <name>
    > <TBT>This is text</TBT>
    > </name>
    > </element>
    > .
    > .
    > .
    > </root>
    >
    > Sometimes the text is enclosed between <TBT> tags, sometimes it is
    > not. These tags were added to be able to translate it more easily into
    > other languages (where TBT stands for To Be Translated).
    >
    > Is there an easy way to write an XSL to delete all the TBT tags or
    > produce an XML without these tags?


    What should the result be,

    <root>
    <element>
    <name>This is text</name>
    </element>
    <element>
    <name>
    This is text
    </name>
    </element>
    ..
    ..
    ..
    </root>

    or

    <root>
    <element>
    <name>This is text</name>
    </element>
    <element>
    <name>
    </name>
    </element>
    ..
    ..
    ..
    </root>

    ?
    --

    Martin Honnen
    http://JavaScript.FAQTs.com/
     
    Martin Honnen, Dec 12, 2003
    #2
    1. Advertising

  3. In message <>, Thomas
    Vackier <> writes
    >Hi all,
    >
    >I'm trying to delete a certain tag in an XML document using XSL, but I
    >can't figure out how.


    Essentially you have to create a complete output document which is the
    same as your source document, but without the elements you want to
    delete.

    So pick up your favourite "identity transform" template(s), e.g.:

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

    and add a template to suppress those unwanted TBTs:

    <xsl:template match="TBT"/>

    Job done!

    Richard Light

    >Here's how my XML looks like:
    >
    ><root>
    > <element>
    > <name>This is text</name>
    > </element>
    > <element>
    > <name>
    > <TBT>This is text</TBT>
    > </name>
    > </element>
    >.
    >.
    >.
    ></root>
    >
    >Sometimes the text is enclosed between <TBT> tags, sometimes it is
    >not. These tags were added to be able to translate it more easily into
    >other languages (where TBT stands for To Be Translated).
    >
    >Is there an easy way to write an XSL to delete all the TBT tags or
    >produce an XML without these tags?
    >
    >Many thanks,
    >
    >Thomas Vackier


    --
    Richard Light
    SGML/XML and Museum Information Consultancy
     
    Richard Light, Dec 12, 2003
    #3
  4. Thanks for the response. The XML should look like the first one you
    suggested, ie, without the tag <TBT>, but retaining the content.

    Cheers,

    Thomas


    *** Sent via Developersdex http://www.developersdex.com ***
    Don't just participate in USENET...get rewarded for it!
     
    Thomas Vackier, Dec 12, 2003
    #4
  5. Thomas Vackier wrote:

    > Thanks for the response. The XML should look like the first one you
    > suggested, ie, without the tag <TBT>, but retaining the content.


    Then you need

    <?xml version="1.0" encoding="UTF-8"?>
    <xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

    <xsl:eek:utput method="xml" />

    <xsl:template match="TBT">
    <xsl:apply-templates select="node()" />
    </xsl:template>

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

    </xsl:stylesheet>

    where all nodes are simply copied besides <TBT> elements where only the
    child nodes are copied.
    --

    Martin Honnen
    http://JavaScript.FAQTs.com/
     
    Martin Honnen, Dec 13, 2003
    #5
  6. Re: Deleting tags via XSL - Thanks!

    Thanks for the input. Works like a charm!

    Thomas

    > Then you need
    >
    > <?xml version="1.0" encoding="UTF-8"?>
    > <xsl:stylesheet version="1.0"
    > xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    >
    > <xsl:eek:utput method="xml" />
    >
    > <xsl:template match="TBT">
    > <xsl:apply-templates select="node()" />
    > </xsl:template>
    >
    > <xsl:template match="@* | node()">
    > <xsl:copy>
    > <xsl:apply-templates select="@* | node()" />
    > </xsl:copy>
    > </xsl:template>
    >
    > </xsl:stylesheet>
    >
    > where all nodes are simply copied besides <TBT> elements where only the
    > child nodes are copied.
     
    Thomas Vackier, Dec 15, 2003
    #6
    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. Greg Smith
    Replies:
    35
    Views:
    3,224
    Alex Kay
    Oct 24, 2004
  2. Replies:
    1
    Views:
    3,695
    A. Bolmarcich
    May 27, 2005
  3. Replies:
    0
    Views:
    584
  4. Harry Barker
    Replies:
    2
    Views:
    546
    Alf P. Steinbach
    Apr 19, 2006
  5. pablitoman
    Replies:
    1
    Views:
    188
    Mike Dalessio
    Dec 16, 2009
Loading...

Share This Page