Connecting Tech Pros Worldwide Forums | Help | Site Map

To delete a unwanted sub element using xsl

Newbie
 
Join Date: Nov 2008
Posts: 5
#1: Nov 6 '08
I have xml like

<root>
<parent>
<child1>Text</child1>
</parent>
</root>

I want to delete the unwanted sub element <child1> and output would be like <parent>Text</parent>.

Thanks in advance.

Dormilich's Avatar
Moderator
 
Join Date: Aug 2008
Location: Leipzig, Germany
Posts: 3,662
#2: Nov 6 '08

re: To delete a unwanted sub element using xsl


if the xml is that simple you can simply rewrite it.
Expand|Select|Wrap|Line Numbers
  1. <xsl:template match="parent"> // or any convenient xpath/name depending on the situation
  2.   <parent>
  3.     <xsl:value-of select="child1/text()"/>
  4.   </parent>
  5. </xsl:template>
regards
Newbie
 
Join Date: Nov 2008
Posts: 5
#3: Nov 6 '08

re: To delete a unwanted sub element using xsl


Quote:

Originally Posted by Dormilich

if the xml is that simple you can simply rewrite it.

Expand|Select|Wrap|Line Numbers
  1. <xsl:template match="parent"> // or any convenient xpath/name depending on the situation
  2.   <parent>
  3.     <xsl:value-of select="child1/text()"/>
  4.   </parent>
  5. </xsl:template>
regards


Thanks for info but it is not a simple xml file. It is a big xml file with lot of elements. I have tried the above sample, but it is not resulting the desired output. The xml file is actually looks like:

<site.info number="009" snumber="0122" wnumber="AtoZ" type="text"><ital>Rose,</ital> running text</site.info>

here it want to remove the element <ital>, I am using this xsl file as pre cleanup process.

Thanks in advance
Dormilich's Avatar
Moderator
 
Join Date: Aug 2008
Location: Leipzig, Germany
Posts: 3,662
#4: Nov 6 '08

re: To delete a unwanted sub element using xsl


Quote:

Originally Posted by radhakrishnanrs

Thanks for info but it is not a simple xml file. It is a big xml file with lot of elements. I have tried the above sample, but it is not resulting the desired output.

the problem lies in the <template>'s match attribute. if you want to filter it from a more complex xml you'll probably need to copy the xml but those elements and insert the corrected elements.
some general code for xsl copy you'll find here in some of the threads (I'll look up some of them later). the principle is something like
Expand|Select|Wrap|Line Numbers
  1. <xsl:template match="node() | @*">
  2.   <xsl:copy select="."/>
  3. </xsl:template>
of cause this is without filter.
Expand|Select|Wrap|Line Numbers
  1. <xsl:template match="node()[not(ital)]">
  2. // ...
  3. // add attribute copy
this will exclude any elements with a <ital> child element from copying, now insert the just excluded elements
Expand|Select|Wrap|Line Numbers
  1. <xsl:template match="node()[ital]">
  2. // code similar to the already posted
note: this is a rough sketch of what I would do, still need to look up the exact code.

regards
Dormilich's Avatar
Moderator
 
Join Date: Aug 2008
Location: Leipzig, Germany
Posts: 3,662
#5: Nov 6 '08

re: To delete a unwanted sub element using xsl


Quote:

Originally Posted by Dormilich

some general code for xsl copy you'll find here in some of the threads (I'll look up some of them later).

http://bytes.com/showthread.php?t=842976
http://bytes.com/showthread.php?t=841347
**********
Moderator
 
Join Date: Mar 2006
Posts: 1,103
#6: Nov 6 '08

re: To delete a unwanted sub element using xsl


Assuming you're just filtering out the ital nodes:
Expand|Select|Wrap|Line Numbers
  1. <xsl:template match="ital">
  2.   <xsl:apply-templates/> <!-- apply-templates but do not copy the node -->
  3. </xsl:template>
  4.  
  5. <xsl:template match="*"> <!-- copy template -->
  6.   <xsl:copy>
  7.     <xsl:copy-of select="@*"/>
  8.     <xsl:apply-templates/>
  9.   </xsl:copy>
  10. </xsl:template>
  11.  
Newbie
 
Join Date: Nov 2008
Posts: 5
#7: Nov 17 '08

re: To delete a unwanted sub element using xsl


Thanks all, it Worked correctly:)
Reply