Skip to main content
Solved

Deleting elements in xml file

  • September 3, 2018
  • 3 replies
  • 138 views

mario_bla
Participant
Forum|alt.badge.img+6

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!

Best answer by takashi

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
View original
Did this help you find an answer to your question?

3 replies

takashi
Influencer
  • Best Answer
  • September 3, 2018

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

mario_bla
Participant
Forum|alt.badge.img+6
  • Author
  • Participant
  • September 5, 2018

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.


takashi
Influencer
  • September 6, 2018
mario_bla wrote:

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

Cookie policy

We use cookies to enhance and personalize your experience. If you accept you agree to our full cookie policy. Learn more about our cookies.

 
Cookie settings