Solved

Deleting elements in xml file

  • 3 September 2018
  • 3 replies
  • 34 views

Badge

Hi,

sorry for this, I have a strong feeling this is for the beginners level but I can't figure it out after 2 hours of trying so maybe someone can show me the right way (I'm also a FME beginner who did only a few workbenches till now).

I need to update an xml file in a way to delete elements based on some condition. For example in this xml file:

<?xml version="1.0" encoding="UTF-8"?>
<bookstore>   
<book category="cooking">         
  <title lang="en">Everyday Italian</title>         
  <author>Giada De Laurentiis</author>         
  <year>2005</year>         
  <price>30.00</price>   
</book>   
<book category="children">         
  <title lang="en">Harry Potter</title>         
  <author>J K. Rowling</author>         
  <year>2005</year>         
  <price>29.99</price>   
</book>   
<book category="web">         
  <title lang="en">Learning XML</title>         
  <author>Erik T. Ray</author>         
  <year>2003</year>         
  <price>39.95</price>   
</book>
</bookstore>
 

I want to delete all book elements with the year 2005. So the resulting xml should look like this:

<?xml version="1.0" encoding="UTF-8"?>
<bookstore>   
<book category="web">         
  <title lang="en">Learning XML</title>         
  <author>Erik T. Ray</author>         
  <year>2003</year>        
  <price>39.95</price>   
</book>
</bookstore>

Thank you very much in advanced for any help!

icon

Best answer by takashi 3 September 2018, 15:42

View original

3 replies

Userlevel 3
Badge +17

Hi @mario_bla, the XMLUpdater might help you. Note the XMLUpdater always requires a feature input from the Update port, although it's not necessary that the feature contains any relevant attribute in this case.

XML Path setting:

//year[text()='2005']/parent::book

0684Q00000ArJvMQAV.png

Alternatively, the XMLXQueryUpdater can be used.

XQuery Expression

delete node //year[text()='2005']/parent::book
Badge

Hi @takashi,

thank you very much for your help! I managed to do it following your first suggestion with XMLUpdater. It took me a while because I was always using xml reader and couldn't make the XMLUpdater to do its task. Than I used the txt reader instead and read the xml file as a plain text (with parameter 'read whole file at once' set to 'yes'). When I did that, workbench ran successfully.

Userlevel 3
Badge +17

Hi @takashi,

thank you very much for your help! I managed to do it following your 
first suggestion with XMLUpdater. It took me a while because I was 
always using xml reader and couldn't make the XMLUpdater to do its task.
 Than I used the txt reader instead and read the xml file as a plain 
text (with parameter 'read whole file at once' set to 'yes'). When I did
 that, workbench ran successfully.

Good to hear.

 

Additions.

 

1. You can also read the source XML file with the XMLUpdater directly.

 

0684Q00000ArMFwQAN.png

2. This XPath is also available in your case.

 

//book[child::year/text()='2005']

Reply