Skip to main content
Question

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


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

jdh
Contributor
Forum|alt.badge.img+28
  • Contributor
  • July 25, 2018

You can use an xmlXQueryUpdater with a query like:

 

for $node in //OperatingTime
where $node =''
return (
delete node $node)

takashi
Contributor
  • July 25, 2018
jdh wrote:

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

jdh
Contributor
Forum|alt.badge.img+28
  • Contributor
  • July 25, 2018
takashi wrote:
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.

 


  • Author
  • July 25, 2018

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.


takashi
Contributor
  • July 25, 2018
takashi wrote:
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...

 

 


jdh
Contributor
Forum|alt.badge.img+28
  • Contributor
  • July 25, 2018
takashi wrote:
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.

 

 


jdh
Contributor
Forum|alt.badge.img+28
  • Contributor
  • July 25, 2018
balcpnar wrote:

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.

 


friesewoudloper
Forum|alt.badge.img

hoursoperationlist.xml

 

strip-hoursoperationlist.fmw

I think XSLTProcessor with a simple stylesheet does the job.


friesewoudloper
Forum|alt.badge.img
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


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