Question

How to remove xml element if it does not consist a value?

  • 25 July 2018
  • 9 replies
  • 58 views

Hi!

I have a XML file with opening hours of corresponding days of a week.

Sometimes I don`t have the opening hour of a certain day; but I still have the title of the day in the xml file. As shown here, I have empty FRIDAY and SATURDAY elements.

What I need is to remove the OperatingTime element totally, if dayOfweek does not have any value. As it is shown here, the elements for FRIDAY and SATURDAY removed.

As I have many records like this, I believe Pyhtoncaller or InlineQuery would fix it quickly but I couldnt manage to do it. Could you help me with that?

Thanks!


9 replies

Badge +22

You can use an xmlXQueryUpdater with a query like:

 

for $node in //OperatingTime
where $node =''
return (
delete node $node)
Userlevel 2
Badge +17

You can use an xmlXQueryUpdater with a query like:

 

for $node in //OperatingTime
where $node =''
return (
delete node $node)
Agree that the XMLXQueryUpdater can be used effectively here.

 

Just be aware that the "where" clause above may not work as expected depending on the format of source XML. For example, if the entire document is put on a single line and there is no excess spaces between elements, all OperatingTime elements will be removed.

 

I think this expression would be better If there could be such a case.

 

for $node in //OperatingTime
where count($node/child::*) = 0
return delete node $node 
Or, the "child::" can be abbreviated, as in:

 

for $node in //OperatingTime
where count($node/*) = 0
return delete node $node
Badge +22
Agree that the XMLXQueryUpdater can be used effectively here.

 

Just be aware that the "where" clause above may not work as expected depending on the format of source XML. For example, if the entire document is put on a single line and there is no excess spaces between elements, all OperatingTime elements will be removed.

 

I think this expression would be better If there could be such a case.

 

for $node in //OperatingTime
where count($node/child::*) = 0
return delete node $node 
Or, the "child::" can be abbreviated, as in:

 

for $node in //OperatingTime
where count($node/*) = 0
return delete node $node
I agree that`s it`s not the best clause in the general case.

 

I was thinking originally of using functx:has-empty-content http://www.xqueryfunctions.com/xq/functx_has-empty-content.html

 

but I couldn`t get the syntax exactly right.

 

Thanks @jdh and @takashi!

So , how should I make the XMLXQueryUpdater work with xml file? Because as i am using it directly after the reader now, everything went into failed. I am attaching an example.xml similar to my original file.

Userlevel 2
Badge +17
Agree that the XMLXQueryUpdater can be used effectively here.

 

Just be aware that the "where" clause above may not work as expected depending on the format of source XML. For example, if the entire document is put on a single line and there is no excess spaces between elements, all OperatingTime elements will be removed.

 

I think this expression would be better If there could be such a case.

 

for $node in //OperatingTime
where count($node/child::*) = 0
return delete node $node 
Or, the "child::" can be abbreviated, as in:

 

for $node in //OperatingTime
where count($node/*) = 0
return delete node $node
I'm afraid that FME doesn't bundle the Functx Function Library...

 

 

Badge +22
I'm afraid that FME doesn't bundle the Functx Function Library...

 

 

That would explain why I couldn't get it to work. I just figured it was my lack of skill in xquery.

 

 

Badge +22

Thanks @jdh and @takashi!

So , how should I make the XMLXQueryUpdater work with xml file? Because as i am using it directly after the reader now, everything went into failed. I am attaching an example.xml similar to my original file.

Don't read it in as a CSV.

 

You can use the XML reader, text file (whole file at once), or use a creator and in the XMLXQueryUpdater set the XML source to the file instead of an attribute containing the XML.

 

Badge

hoursoperationlist.xml

 

strip-hoursoperationlist.fmw

I think XSLTProcessor with a simple stylesheet does the job.

Badge
strip-hoursoperationlist.fmwhoursoperationlist.xml

 

 

The links in my answer are broken, so I'll give it another try. I also include screenshots, just in case ;-)

 

 

 

 

 

Reply