fast-xml-parser icon indicating copy to clipboard operation
fast-xml-parser copied to clipboard

Configuring XML output attributes - attributeValueProcessor?

Open timoconnor opened this issue 1 year ago • 5 comments

  • [x] Are you running the latest version?
  • [x] Have you included sample input, output, error, and expected output?
  • [ ] Have you checked if you are using correct configuration?
  • [x] Did you try online tool?

Description

Not sure if this should be a feature request, or I am just not understanding the docs - How to I ensure that when I am using the XMLBuilder that XML attributes are correct? I have very strict requirements on the XML format, since it will need to be human writable. I know I will need to provide some additional config into the builder, as there is no way it can know that I want 'id' to be an attribute - I was looking at 'attributeValueProcessor' but there is no documentation how to use it, or I couldnt see it.

Input

Here is an example of the JSON

Code

{
    "question": {
        "ask": "Which of the following statements are true?",
        "choice": [
            {
                "#text": "This one",
                "id": "A"
            },
            {
                "#text": "Probably this one",
                "id": "B"
            },
            {
                "#text": "Not this one",
                "id": "C"
            }
        ],
        "answer": "A",
        "response": [
            {
                "#text": "Sorry that is not right.",
                "type": "incorrect"
            },
            {
                "#text": "Well done!",
                "type": "correct"
            }
        ],
        "type": "MC"
    }
}

Output

<question>
	<ask>
		Which of the following statements are true?
	</ask>
	<choice>
		This one.
		<id>
			A
		</id>
	</choice>
	<choice>
		Probably this one
		<id>
			B
		</id>
	</choice>
	<choice>
		Not this one.
		<id>
			C
		</id>
	</choice>
	<answer>
		A
	</answer>
	<response>
		Sorry that is not right.
		<type>
			incorrect
		</type>
	</response>
	<response>
		Well done!
		<type>
			correct
		</type>
	</response>
	<type>
		MC
	</type>
</question>

expected data

<question type="MC">
      <ask>Which of the following statements are true?</ask>
      <choice id="A">This one.</choice>
      <choice id="B">Probably this one</choice>
      <choice id="C">Not this one.</choice>
      <answer>A</answer>
      <response type="incorrect">Sorry that is not right.</response>
      <response type="correct">Well done!</response>
</question>

Would you like to work on this issue?

  • [ ] Yes
  • [ ] No

Bookmark this repository for further updates.

timoconnor avatar Mar 02 '23 06:03 timoconnor

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.

github-actions[bot] avatar Mar 02 '23 06:03 github-actions[bot]

I guess I can use XSLT to transform the XML into the form that I want or just write some code to craft the XML in the exact form I want, but looking for a neat solution :)

timoconnor avatar Mar 02 '23 06:03 timoconnor

attributeValueProcessor is just to process value. Currently there is no feature to convert specific tags into attribute. But probably we can introduce isAttribute method to do that. So may be some other solution

amitguptagwl avatar Mar 02 '23 06:03 amitguptagwl

This package, with it's XML builder and an ability to output attributes as well as elements would be a winning solution for me. I really hope building attributes makes it onto the radar. Another thing of interest, which I have not really investigated and may already be possible, is: is it possible, perhaps with some tree-shaking, to import just the builder components?

ReinsBrain avatar Mar 18 '24 15:03 ReinsBrain

Yes you can import just the builder.

const {XMLBuilder} = require("fast-xml-parser");

I would also suggest to try v5

amitguptagwl avatar Mar 19 '24 07:03 amitguptagwl