NaturalIntelligence / fast-xml-parser

Validate XML, Parse XML and Build XML rapidly without C/C++ based libraries and no callback.
https://naturalintelligence.github.io/fast-xml-parser/
MIT License
2.49k stars 302 forks source link

Questions re: TagValueProcessor behavior #544

Open dopry opened 1 year ago

dopry commented 1 year ago

I was wondering about the if tag value is empty then tagValueProcessor will not be called. specification for the TagValueProcessor

I have the following scenarios

empty result

 <?xml version="1.0" encoding="UTF-8" standalone="yes"?><response status="Ok"><links/></response>
{ response: { links: '', '@_status': 'Ok' }}

populated result

<response status="Ok"><links>
   <link id="1234567" hint="1 Street Street" linkDescription="..." link="/property/1234567" />
   <link id="1234568" hint="2 Street Street" linkDescription="..." link="/property/1234568" />
</links></response>
{ response: { links: { link: [Array] }, '@_status': 'Ok' } }

In this scenario I'd like to be able to overwrite the value of an empty links property to { link: [] } so that I have a consistent interface for my client code that consumes this API. Unfortunately since empty values are never pass to the tagValueProcessor I cannot do this.

Would you be open to an update that allows passing the value of empty nodes through the tagValueProcessor to support my feature request?

github-actions[bot] commented 1 year ago

I'm glad you find this repository helpful. I'll try to address your issue ASAP. You can watch the repo for new changes or star it.

amitguptagwl commented 1 year ago

The way currently it is implemented, it has many open architecture issues which need to be decided before supporting empty node. There is a separate branch where we attempted to implement this feature but since the decision was pending on many issues like how should library behave in that case, the branch was left for future changes. I can't exactly recall the issues but this feature may take time until I get some free time or some PR is raised.

dopry commented 1 year ago

So in general you're open to a PR, and there isn't a specific reason tagValueProcessor is not called on empty tag values?

amitguptagwl commented 1 year ago

With tagValueProcessor you can control how and which tag value should be parsed.

People need option to skip tagValueProcessor.

dopry commented 1 year ago

I understand that list. I referenced it in my initial comment. Are you saying you do not want to change those specifications to support the use case I presented? People can always skip processing a tag value by return the input or undefined... Right now I cannot override an empty value and need to. Before I start the work I want to know if you'd accept the change, or not. This way I can decide to submit a PR or start a fork.

amitguptagwl commented 1 year ago

Yes, you're right. People can skip parsing by just returning undefined. In my understanding, you want to call "tagValueProcessor" for empty values and set the value as per your need. Eg [], or "{}" etc. That is fine to me.

dopry commented 1 year ago

Thanks. Then I will work on a PR when the opportunity presents itself. I made a start, but quickly realized it had unanticipated impacts. I'll need to revisit it when I have a larger window to work on it.

amitguptagwl commented 1 year ago

You can refer the branch that I created to understand the impact better