Skip to main content
Solved

Writing XML with optional attributes

  • January 27, 2017
  • 4 replies
  • 61 views

jpvo
Contributor
Forum|alt.badge.img+2

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!

This post is closed to further activity.
It may be an old question, an answered question, an implemented idea, or a notification-only post.
Please check post dates before relying on any information in a question or answer.
For follow-up or related questions, please post a new question or idea.
If there is a genuine update to be made, please contact us and request that the post is reopened.

4 replies

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

Hi @jpvo,

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


jpvo
Contributor
Forum|alt.badge.img+2
  • Author
  • Contributor
  • January 27, 2017

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
Contributor
Forum|alt.badge.img+2
  • Author
  • Contributor
  • 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!

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=""