HighTechTalks DotNet Forums  

Deleting elements from xml doc.

Dotnet XML microsoft.public.dotnet.xml


Discuss Deleting elements from xml doc. in the Dotnet XML forum.



Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old   
Gaurav
 
Posts: n/a

Default Deleting elements from xml doc. - 10-19-2007 , 12:06 AM






Hello,

I am stuck trying to apply the following logic to the below xml:
<?xml version='1.0' encoding='ISO-8859-1'?>
<Collection>
<Book Id='1' Locator='Yes'>
<Title>Principle of Relativity</Title>
<Author>Albert Einstein</Author>
<Genre>Physics</Genre>
</Book>
<Book Id='2'>
<Title>Cosmos</Title>
<Author>Carl Sagan</Author>
<Genre>Cosmology</Genre>
</Book>
</Collection>

Read the "Locator" attribute from the Book element, in case it is "Yes"
.....delete the entire book element, so that the o/p now looks like:

<?xml version='1.0' encoding='ISO-8859-1'?>
<Collection>
<Book Id='2'>
<Title>Cosmos</Title>
<Author>Carl Sagan</Author>
<Genre>Cosmology</Genre>
</Book>
</Collection>

It would be very helpful if somebody can provide a little code on how to
achieve this?

Thanks.

Reply With Quote
  #2  
Old   
Martin Honnen
 
Posts: n/a

Default Re: Deleting elements from xml doc. - 10-19-2007 , 08:20 AM






Gaurav wrote:

Quote:
Read the "Locator" attribute from the Book element, in case it is "Yes"
....delete the entire book element,

With XSLT you can use the identity transformation plus a template
preventing that Book[@Locator = 'Yes'] is being processed e.g.

<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">

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

<xsl:template match="Book[@Locator = 'Yes']"/>

</xsl:stylesheet>

--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/


Reply With Quote
  #3  
Old   
Gaurav
 
Posts: n/a

Default Re: Deleting elements from xml doc. - 10-19-2007 , 11:06 AM



Thanks Martin. I am fairly new to xml transformation etc.... so transforming
the input xml file against the code you mentioned below would do the trick?


"Martin Honnen" wrote:

Quote:
Gaurav wrote:

Read the "Locator" attribute from the Book element, in case it is "Yes"
....delete the entire book element,


With XSLT you can use the identity transformation plus a template
preventing that Book[@Locator = 'Yes'] is being processed e.g.

xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0"

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

xsl:template match="Book[@Locator = 'Yes']"/

/xsl:stylesheet

--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/


Reply With Quote
  #4  
Old   
Martin Honnen
 
Posts: n/a

Default Re: Deleting elements from xml doc. - 10-19-2007 , 11:29 AM



Gaurav wrote:
Quote:
Thanks Martin. I am fairly new to xml transformation etc.... so transforming
the input xml file against the code you mentioned below would do the trick?
Yes. With the .NET framework 2.0 or later use
System.Xml.Xsl.XslCompiledTransform to perform the transformation.

--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/


Reply With Quote
  #5  
Old   
Gaurav
 
Posts: n/a

Default Re: Deleting elements from xml doc. - 10-19-2007 , 12:12 PM



I am using .NET framework 1.1.

I transformed the file against the .xslt and the result is:
Carl SaganCosmology

What is required to get the following result:
<Collection>
<Book Id='2'>
<Title>Cosmos</Title>
<Author>Carl Sagan</Author>
<Genre>Cosmology</Genre>
</Book>
</Collection>

Thanks a lot for all your help.

"Martin Honnen" wrote:

Quote:
Gaurav wrote:
Thanks Martin. I am fairly new to xml transformation etc.... so transforming
the input xml file against the code you mentioned below would do the trick?

Yes. With the .NET framework 2.0 or later use
System.Xml.Xsl.XslCompiledTransform to perform the transformation.

--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/


Reply With Quote
  #6  
Old   
Martin Honnen
 
Posts: n/a

Default Re: Deleting elements from xml doc. - 10-19-2007 , 12:32 PM



Gaurav wrote:
Quote:
I am using .NET framework 1.1.
Then you need to use System.Xml.Xsl.XslTransform.

Quote:
I transformed the file against the .xslt and the result is:
Carl SaganCosmology
I don't understand why you would get that result, unless you run the
transformation inside of a browser which then renders the contents of
elements in the result document.
Please tell us how you run the transformation and how you look at the
result.


--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/


Reply With Quote
Reply




Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.