Skip to content

Commit 6ad40ee

Browse files
authored
Add transformTagName option to transform tag names when parsing (#469)
1 parent 2203711 commit 6ad40ee

File tree

5 files changed

+53
-3
lines changed

5 files changed

+53
-3
lines changed

spec/transform_tagname_spec.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
"use strict";
2+
3+
const {XMLParser} = require("../src/fxp");
4+
5+
describe("XMLParser", function() {
6+
it("should parse lowercase tagnames", function() {
7+
const xmlData = `<?xml version='1.0'?>
8+
<root>
9+
<person>Person 1</person>
10+
<Person>Person 2</Person>
11+
<PERSON>Person 3</PERSON>
12+
<person>Person 4</person>
13+
</root>
14+
`;
15+
16+
const parser = new XMLParser({
17+
// transformTagName: (tagName) => tagName
18+
ignoreDeclaration: true,
19+
transformTagName: (tagName) => tagName.toLowerCase()
20+
});
21+
let result = parser.parse(xmlData);
22+
23+
const expected = {
24+
"root": {
25+
"person": [
26+
"Person 1",
27+
"Person 2",
28+
"Person 3",
29+
"Person 4"
30+
]
31+
}
32+
};
33+
34+
expect(result).toEqual(expected);
35+
});
36+
});

src/fxp.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ type X2jOptions = {
2222
htmlEntities: boolean;
2323
ignoreDeclaration: boolean;
2424
ignorePiTags: boolean;
25+
transformTagName: ((tagName: string) => string) | false;
2526
};
2627
type strnumOptions = {
2728
hex: boolean;

src/xmlbuilder/json2xml.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@ const defaultOptions = {
3030
{ regex: new RegExp("\"", "g"), val: "&quot;" }
3131
],
3232
processEntities: true,
33-
stopNodes: []
33+
stopNodes: [],
34+
transformTagName: false,
3435
};
3536

3637
function Builder(options) {

src/xmlparser/OptionsBuilder.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@ const defaultOptions = {
3030
processEntities: true,
3131
htmlEntities: false,
3232
ignoreDeclaration: false,
33-
ignorePiTags: false
33+
ignorePiTags: false,
34+
transformTagName: false,
3435
};
3536

3637
const buildOptions = function(options) {

src/xmlparser/OrderedObjParser.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,10 @@ const parseXml = function(xmlData) {
193193
}
194194
}
195195

196+
if(this.options.transformTagName) {
197+
tagName = this.options.transformTagName(tagName);
198+
}
199+
196200
if(currentNode){
197201
textData = this.saveTextToParentTag(textData, currentNode, jPath);
198202
}
@@ -257,12 +261,15 @@ const parseXml = function(xmlData) {
257261

258262
i = closeIndex + 2;
259263
}else {//Opening tag
260-
261264
let result = readTagExp(xmlData,i, this. options.removeNSPrefix);
262265
let tagName= result.tagName;
263266
let tagExp = result.tagExp;
264267
let attrExpPresent = result.attrExpPresent;
265268
let closeIndex = result.closeIndex;
269+
270+
if (this.options.transformTagName) {
271+
tagName = this.options.transformTagName(tagName);
272+
}
266273

267274
//save text as child node
268275
if (currentNode && textData) {
@@ -322,6 +329,10 @@ const parseXml = function(xmlData) {
322329
}else{
323330
tagExp = tagExp.substr(0, tagExp.length - 1);
324331
}
332+
333+
if(this.options.transformTagName) {
334+
tagName = this.options.transformTagName(tagName);
335+
}
325336

326337
const childNode = new xmlNode(tagName);
327338
if(tagName !== tagExp && attrExpPresent){

0 commit comments

Comments
 (0)