Skip to main content
Solved

Writing XML with optional attributes


jpvo
Participant
Forum|alt.badge.img+1
  • Participant

Hi all,

I am trying to write an XML with optional attributes. If the attribute is null or missing I don't want to have it in the output xml. I xml attributes, not child elements. When using the regular xml writer, all fme attributes are redirected as child elements and missing attributes are properly removed.

However, when using xml attributes within a xml templater, missing attributes are still there and note removed.

Any idea on how to solve this?

Best answer by david_r

I've had the same issues, which for my specific task I was able to solve using two StringReplacers with the following regular expressions:

<[a-zA-Z0-9_]+>\s*</[a-zA-Z0-9_]+>

This one removes emtpy tags, e.g. "<tag></tag>"

And to remove emtpy self-closing tags, such as "<tag />":

<[a-zA-Z0-9_]+/>

I don't think it's ideal, but at least it works. I'd be glad if someone with a more elegant solution would like to share it here.

Edit: Oh, and I forgot to mention that I'm using the XMLTemplater and not the XML Writer!

View original
Did this help you find an answer to your question?

4 replies

itay
Supporter
Forum|alt.badge.img+17
  • Supporter
  • January 27, 2017

Hi @jpvo,

The XMLFormatter has some XML clean up options, did you try them?


jpvo
Participant
Forum|alt.badge.img+1
  • Author
  • Participant
  • January 27, 2017
itay wrote:

Hi @jpvo,

The XMLFormatter has some XML clean up options, did you try them?

Yes but it's not solving the probelm when dealing with xml attributes as opposed to xml child elements.

 

 


david_r
Celebrity
  • Best Answer
  • January 27, 2017

I've had the same issues, which for my specific task I was able to solve using two StringReplacers with the following regular expressions:

<[a-zA-Z0-9_]+>\s*</[a-zA-Z0-9_]+>

This one removes emtpy tags, e.g. "<tag></tag>"

And to remove emtpy self-closing tags, such as "<tag />":

<[a-zA-Z0-9_]+/>

I don't think it's ideal, but at least it works. I'd be glad if someone with a more elegant solution would like to share it here.

Edit: Oh, and I forgot to mention that I'm using the XMLTemplater and not the XML Writer!


jpvo
Participant
Forum|alt.badge.img+1
  • Author
  • Participant
  • January 27, 2017
david_r wrote:

I've had the same issues, which for my specific task I was able to solve using two StringReplacers with the following regular expressions:

<[a-zA-Z0-9_]+>\s*</[a-zA-Z0-9_]+>

This one removes emtpy tags, e.g. "<tag></tag>"

And to remove emtpy self-closing tags, such as "<tag />":

<[a-zA-Z0-9_]+/>

I don't think it's ideal, but at least it works. I'd be glad if someone with a more elegant solution would like to share it here.

Edit: Oh, and I forgot to mention that I'm using the XMLTemplater and not the XML Writer!

We solve it using python to create an xml fragment "manually" and an XMLAppender to insert it at the right place.

 

In the attached workspace I will use a regex to remove name=""

 

 


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