Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add transformTagName option to transform tag names when parsing #469

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
36 changes: 36 additions & 0 deletions spec/transform_tagname_spec.js
@@ -0,0 +1,36 @@
"use strict";

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

describe("XMLParser", function() {
it("should parse lowercase tagnames", function() {
const xmlData = `<?xml version='1.0'?>
<root>
<person>Person 1</person>
<Person>Person 2</Person>
<PERSON>Person 3</PERSON>
<person>Person 4</person>
</root>
`;

const parser = new XMLParser({
// transformTagName: (tagName) => tagName
ignoreDeclaration: true,
transformTagName: (tagName) => tagName.toLowerCase()
});
let result = parser.parse(xmlData);

const expected = {
"root": {
"person": [
"Person 1",
"Person 2",
"Person 3",
"Person 4"
]
}
};

expect(result).toEqual(expected);
});
});
1 change: 1 addition & 0 deletions src/fxp.d.ts
Expand Up @@ -22,6 +22,7 @@ type X2jOptions = {
htmlEntities: boolean;
ignoreDeclaration: boolean;
ignorePiTags: boolean;
transformTagName: ((tagName: string) => string) | false;
};
type strnumOptions = {
hex: boolean;
Expand Down
3 changes: 2 additions & 1 deletion src/xmlbuilder/json2xml.js
Expand Up @@ -30,7 +30,8 @@ const defaultOptions = {
{ regex: new RegExp("\"", "g"), val: "&quot;" }
],
processEntities: true,
stopNodes: []
stopNodes: [],
transformTagName: false,
};

function Builder(options) {
Expand Down
3 changes: 2 additions & 1 deletion src/xmlparser/OptionsBuilder.js
Expand Up @@ -30,7 +30,8 @@ const defaultOptions = {
processEntities: true,
htmlEntities: false,
ignoreDeclaration: false,
ignorePiTags: false
ignorePiTags: false,
transformTagName: false,
};

const buildOptions = function(options) {
Expand Down
13 changes: 12 additions & 1 deletion src/xmlparser/OrderedObjParser.js
Expand Up @@ -193,6 +193,10 @@ const parseXml = function(xmlData) {
}
}

if(this.options.transformTagName) {
tagName = this.options.transformTagName(tagName);
}

if(currentNode){
textData = this.saveTextToParentTag(textData, currentNode, jPath);
}
Expand Down Expand Up @@ -257,12 +261,15 @@ const parseXml = function(xmlData) {

i = closeIndex + 2;
}else {//Opening tag

let result = readTagExp(xmlData,i, this. options.removeNSPrefix);
let tagName= result.tagName;
let tagExp = result.tagExp;
let attrExpPresent = result.attrExpPresent;
let closeIndex = result.closeIndex;

if (this.options.transformTagName) {
tagName = this.options.transformTagName(tagName);
}

//save text as child node
if (currentNode && textData) {
Expand Down Expand Up @@ -322,6 +329,10 @@ const parseXml = function(xmlData) {
}else{
tagExp = tagExp.substr(0, tagExp.length - 1);
}

if(this.options.transformTagName) {
tagName = this.options.transformTagName(tagName);
}

const childNode = new xmlNode(tagName);
if(tagName !== tagExp && attrExpPresent){
Expand Down