472,110 Members | 2,298 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,110 software developers and data experts.

To delete a unwanted sub element using xsl

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.
Nov 6 '08 #1
6 7219
Dormilich
8,658 Expert Mod 8TB
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
Nov 6 '08 #2
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
Nov 6 '08 #3
Dormilich
8,658 Expert Mod 8TB
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
Nov 6 '08 #4
Dormilich
8,658 Expert Mod 8TB
some general code for xsl copy you'll find here in some of the threads (I'll look up some of them later).
https://bytes.com/showthread.php?t=842976
https://bytes.com/showthread.php?t=841347
**********
Nov 6 '08 #5
jkmyoung
2,057 Expert 2GB
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.  
Nov 6 '08 #6
Thanks all, it Worked correctly:)
Nov 17 '08 #7

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

4 posts views Thread by frankabel | last post: by
3 posts views Thread by Brian Underhill via DotNetMonster.com | last post: by
10 posts views Thread by Mark A. Odell | last post: by
7 posts views Thread by JH Programmer | last post: by
5 posts views Thread by Edwin | last post: by
reply views Thread by Big Daddy | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.