Deleting elements from xml doc.

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.
0
Gaurav (24)
10/19/2007 4:06:15 AM
dotnet.xml 7266 articles. 0 followers. Follow

5 Replies
491 Views

Similar Articles

[PageSpeed] 46

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/
0
mahotrash (1777)
10/19/2007 12:20:33 PM
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:

> 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/
> 
0
Gaurav (24)
10/19/2007 3:06:00 PM
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/
0
mahotrash (1777)
10/19/2007 3:29:04 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:

> 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/
> 
0
Gaurav (24)
10/19/2007 4:12:01 PM
Gaurav wrote:
> I am using .NET framework 1.1.

Then you need to use System.Xml.Xsl.XslTransform.

> 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/
0
mahotrash (1777)
10/19/2007 4:32:00 PM
Reply:

Similar Artilces: