Deleting tags via XSL

Thomas Vackier

Usenet Archive
Joined
Dec 12, 2003
Messages
3
Reaction score
0
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
 

Martin Honnen

Usenet Archive
Joined
Apr 8, 2000
Messages
6,174
Reaction score
0
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>

?
 

Richard Light

Usenet Archive
Joined
Jun 19, 2000
Messages
43
Reaction score
0
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
 

Thomas Vackier

Usenet Archive
Joined
Dec 12, 2003
Messages
3
Reaction score
0
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
 

Martin Honnen

Usenet Archive
Joined
Apr 8, 2000
Messages
6,174
Reaction score
0
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

No members online now.

Forum statistics

Threads
548,208
Messages
3,104,798
Members
249,594
Latest member
henrywalsh

Latest Threads

Top