 |
| 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
|
|
|
|

March 11th, 2013, 03:50 AM
|
|
Authorized User
|
|
Join Date: Mar 2013
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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
|
|

March 11th, 2013, 04:31 AM
|
 |
Wrox Author
|
|
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
|
|
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
|
|

March 11th, 2013, 04:53 AM
|
|
Authorized User
|
|
Join Date: Mar 2013
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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
|
|

March 11th, 2013, 05:06 AM
|
 |
Wrox Author
|
|
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
|
|
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
|
|

March 11th, 2013, 05:11 AM
|
|
Authorized User
|
|
Join Date: Mar 2013
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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
|
|

March 11th, 2013, 05:46 AM
|
 |
Wrox Author
|
|
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
|
|
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
|
|

March 11th, 2013, 06:12 AM
|
|
Authorized User
|
|
Join Date: Mar 2013
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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
|
|

March 11th, 2013, 07:17 AM
|
|
Friend of Wrox
|
|
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
|
|
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
|
|

March 11th, 2013, 07:27 AM
|
|
Authorized User
|
|
Join Date: Mar 2013
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Hi Martin,
Thanks for your reply....
It is giving the whole source file as it is with out deleting the Item nodes.
Thanks
Sarala
|
|

March 11th, 2013, 07:42 AM
|
|
Friend of Wrox
|
|
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
|
|
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
|
|
 |