Skip to content

Commit

Permalink
Add transformAttributeName to transform the attribute name upon parsi…
Browse files Browse the repository at this point in the history
…ng. Same logic as transformTagName (#519)
  • Loading branch information
erkie authored Dec 12, 2022
1 parent f900ef8 commit a619c48
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 1 deletion.
8 changes: 8 additions & 0 deletions docs/v4/2.XMLparseOptions.md
Original file line number Diff line number Diff line change
Expand Up @@ -817,6 +817,14 @@ Output
transformTagName: (tagName) => tagName.toLowerCase()
```

## transformAttributeName

`transformAttributeName` property function let you modify the name of attribute

```js
transformAttributeName: (attributeName) => attributeName.toLowerCase()
```

## trimValues
Remove surrounding whitespace from tag or attribute value.

Expand Down
39 changes: 39 additions & 0 deletions spec/transform_attributes_spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
"use strict";

const {XMLParser} = require("../src/fxp");

describe("XMLParsers", function() {
it("should transform attribute names", function() {
const xmlData = `<?xml version='1.0'?>
<root>
<person title="1">Person 1</person>
<person tITle="2">Person 2</Person>
<person hello:TItle="3">Person 3</PERSON>
<person hElLO:TITLE="4">Person 4</person>
</root>
`;

const parser = new XMLParser({
ignoreAttributes: false,
ignoreDeclaration: true,
attributeNamePrefix: "",
attributesGroupName: "attributes",
transformTagName: (tag) => tag.toLowerCase(),
transformAttributeName: (attr) => attr.toLowerCase()
});
let result = parser.parse(xmlData);

const expected = {
"root": {
"person": [
{ attributes: { title: "1" }, "#text": "Person 1" },
{ attributes: { title: "2" }, "#text": "Person 2" },
{ attributes: { "hello:title": "3" }, "#text": "Person 3" },
{ attributes: { "hello:title": "4" }, "#text": "Person 4" }
]
}
};

expect(result).toEqual(expected);
});
});
1 change: 1 addition & 0 deletions src/fxp.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ Control how tag value should be parsed. Called only if tag value is not empty
ignoreDeclaration: boolean;
ignorePiTags: boolean;
transformTagName: ((tagName: string) => string) | false;
transformAttributeName: ((attributeName: string) => string) | false;
};
type strnumOptions = {
hex: boolean;
Expand Down
1 change: 1 addition & 0 deletions src/xmlbuilder/json2xml.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ const defaultOptions = {
processEntities: true,
stopNodes: [],
transformTagName: false,
transformAttributeName: false,
};

function Builder(options) {
Expand Down
1 change: 1 addition & 0 deletions src/xmlparser/OptionsBuilder.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ const defaultOptions = {
ignoreDeclaration: false,
ignorePiTags: false,
transformTagName: false,
transformAttributeName: false,
};

const buildOptions = function(options) {
Expand Down
5 changes: 4 additions & 1 deletion src/xmlparser/OrderedObjParser.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,11 @@ function buildAttributesMap(attrStr, jPath) {
for (let i = 0; i < len; i++) {
const attrName = this.resolveNameSpace(matches[i][1]);
let oldVal = matches[i][4];
const aName = this.options.attributeNamePrefix + attrName;
let aName = this.options.attributeNamePrefix + attrName;
if (attrName.length) {
if (this.options.transformAttributeName) {
aName = this.options.transformAttributeName(aName);
}
if (oldVal !== undefined) {
if (this.options.trimValues) {
oldVal = oldVal.trim();
Expand Down

0 comments on commit a619c48

Please sign in to comment.