Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
|
XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old March 11th, 2013, 03:50 AM
Authorized User
 
Join Date: Mar 2013
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Default Deletion of element from XML by specifying a condition

Hi,

I want to delete a node from XML by specifying a condition as below...
input XML:
<Transmission>
<ItemMaster>
<Item>
<ItemId>111</ItemId>
<ItemName>I1</ItemName>
</Item>
<Item>
<ItemId>112</ItemId>
<ItemName>I2</ItemName>
</Item>
<Item>
<ItemId>113</ItemId>
<ItemName>I3</ItemName>
</Item>
</ItemMaster>
</Transmission>

if ItemId = '112'

then i want the Item node to be deleted from xml where ItemId=112

Output xml:

<transmission>
<ItemMaster>
<Item>
<ItemId>111</ItemId>
<ItemName>I1</ItemName>
</Item>
<Item>
<ItemId>113</ItemId>
<ItemName>I3</ItemName>
</Item>
</ItemMaster>
</Transmission>

Any body help me how to esolve this
Thanks in advance!!!


Thanks
Sarala
 
Old March 11th, 2013, 04:31 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

Create a stylesheet with two template rules.

(1) an "identity template which copies everything by default

Code:
<xsl:template match="*"><xsl:copy><xsl:apply-templates/></xsl:copy></xsl:template>
(2) a template rule to "not copy" (i.e. delete) the selected element

Code:
<xsl:template match="ItemId[.='112']"/>
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference
 
Old March 11th, 2013, 04:53 AM
Authorized User
 
Join Date: Mar 2013
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thankyou for your reply Mhkay,

But the code which u have provided is not deleting the whole item node where itemId='112'.

The output when used the above code is as below...
<Transmission>
<ItemMaster>
<Item>
<ItemId>111</ItemId>
<ItemName>I1</ItemName>
</Item>
<Item>
<ItemName>I2</ItemName>
</Item>
<Item>
<ItemId>113</ItemId>
<ItemName>I3</ItemName>
</Item>
</ItemMaster>
</Transmission>

Thanks,
Sarala
 
Old March 11th, 2013, 05:06 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

Sorry, I made the mistake of giving you code rather than an explanation. I usually try to give an explanation, and leave you to write the code, because that way it forces you to try and understand the solution rather than just applying it blindly.

The design principle is exactly the same, you just need to change the pattern for the node being deleted from ItemId[.='112'] to Item[ItemId='112']
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference
 
Old March 11th, 2013, 05:11 AM
Authorized User
 
Join Date: Mar 2013
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks Michael,

If I have a many mutiple ItemId's which I need to delete from XML, what logic should I use?


Thanks in advance!!!



Sarala
 
Old March 11th, 2013, 05:46 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

Just use a match pattern that matches all the nodes you want to delete - or multiple template rules if you prefer.
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference
 
Old March 11th, 2013, 06:12 AM
Authorized User
 
Join Date: Mar 2013
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi Michael
thanks for your reply,

If I have xml like below...,
<Transmission>
<Location>
<LocId>L1</LocId>
<LocName>LOCa</LocName>
</Location>
<Location>
<LocId>L2</LocId>
<LocName>LOCb</LocName>
</Location>
<ItemMaster>
<Item>
<ItemId>111</ItemId>
<ItemName>I1</ItemName>
</Item>
<Item>
<ItemId>112</ItemId>
<ItemName>I2</ItemName>
</Item>
<Item>
<ItemId>113</ItemId>
<ItemName>I3</ItemName>
</Item>
</ItemMaster>
<Items>
<ItemId>111</ItemId>
<ItemId>112</ItemId>
</Items>
</Transmission>


I have to traverse through Transmission/Items where it should go for all the ItemId in Items and delete the Item node from ItemMaster where ItemMaster/Item/ItemId=Items/ItemId

output xml will be as follows......
<Transmission>
<Location>
<LocId>L1</LocId>
<LocName>LOCa</LocName>
</Location>
<Location>
<LocId>L2</LocId>
<LocName>LOCb</LocName>
</Location>
<ItemMaster>
<Item>
<ItemId>113</ItemId>
<ItemName>I3</ItemName>
</Item>
</ItemMaster>
<Items>
<ItemId>111</ItemId>
<ItemId>112</ItemId>
</Items>
</Transmission>


Hope I am clear with my requirement...
please help me to resolve this
Thanks in advance!!!!!!

Sarala
 
Old March 11th, 2013, 07:17 AM
Friend of Wrox
 
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

Well your own post shows the paths you need so use the identity transformation template as already suggested, then add a template
Code:
<xsl:template match="ItemMaster/Item[ItemId = /Transmission/Items/ItemId]"/>
With XSLT 2.0 and for efficiency I would use a key however:
Code:
<xsl:stylesheet
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  version="2.0">
  
<xsl:strip-space elements="*"/>
<xsl:output indent="yes"/>
  
<xsl:variable name="ids-to-be-deleted" select="/Transmission/Items/ItemId"/>

<xsl:key name="to-be-deleted" match="ItemMaster/Item" use="ItemId"/>

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

<xsl:template match="key('to-be-deleted', $ids-to-be-deleted)"/>

</xsl:stylesheet>
__________________
Martin Honnen
Microsoft MVP (XML, Data Platform Development) 2005/04 - 2013/03
My blog
 
Old March 11th, 2013, 07:27 AM
Authorized User
 
Join Date: Mar 2013
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi Martin,

Thanks for your reply....


It is giving the whole source file as it is with out deleting the Item nodes.


Thanks
Sarala
 
Old March 11th, 2013, 07:42 AM
Friend of Wrox
 
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

When I apply the code
Code:
xsl:stylesheet
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  version="2.0">
  
<xsl:strip-space elements="*"/>
<xsl:output indent="yes"/>
  
<xsl:variable name="ids-to-be-deleted" select="/Transmission/Items/ItemId"/>

<xsl:key name="to-be-deleted" match="ItemMaster/Item" use="ItemId"/>

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

<xsl:template match="key('to-be-deleted', $ids-to-be-deleted)"/>

</xsl:stylesheet>
to the input
Code:
<Transmission>
  <Location>
    <LocId>L1</LocId>
    <LocName>LOCa</LocName>
  </Location>
  <Location>
    <LocId>L2</LocId>
    <LocName>LOCb</LocName>
  </Location>
  <ItemMaster>
    <Item>
      <ItemId>111</ItemId>
      <ItemName>I1</ItemName>
    </Item>
    <Item>
      <ItemId>112</ItemId>
      <ItemName>I2</ItemName>
    </Item>
    <Item>
      <ItemId>113</ItemId>
      <ItemName>I3</ItemName>
    </Item>
  </ItemMaster>
  <Items>
    <ItemId>111</ItemId>
    <ItemId>112</ItemId>
  </Items>
</Transmission>
with Saxon 9.4 I get the output
Code:
<Transmission>
   <Location>
      <LocId>L1</LocId>
      <LocName>LOCa</LocName>
   </Location>
   <Location>
      <LocId>L2</LocId>
      <LocName>LOCb</LocName>
   </Location>
   <ItemMaster>
      <Item>
         <ItemId>113</ItemId>
         <ItemName>I3</ItemName>
      </Item>
   </ItemMaster>
   <Items>
      <ItemId>111</ItemId>
      <ItemId>112</ItemId>
   </Items>
</Transmission>
So inside ItemMaster some Item elements have been removed.
__________________
Martin Honnen
Microsoft MVP (XML, Data Platform Development) 2005/04 - 2013/03
My blog





Similar Threads
Thread Thread Starter Forum Replies Last Post
XSLT output to HTML - xml element text should look exactly as laid out in xml element DjGogga XSLT 4 July 21st, 2011 05:39 PM
Folder deletion with XML Config Peroni Beginning VB 6 5 January 16th, 2008 08:11 PM
Urgent:XSLT element value selection and condition amhicraig XSLT 1 December 4th, 2007 07:57 PM
hi please help me on xml deletion karthikc85 XML 1 October 10th, 2006 06:09 AM
Converting XML to XML (making element mandatory) boondocksaint20 XSLT 8 April 28th, 2006 10:54 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.