Deleting tags via XSL

T

Thomas Vackier

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
 
M

Martin Honnen

Thomas said:
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>

?
 
R

Richard Light

Thomas said:
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
 
T

Thomas Vackier

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
 
M

Martin Honnen

Thomas said:
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.
 

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,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top