Skip to content

Commit

Permalink
Lookup namespace from original element. Fixes #51
Browse files Browse the repository at this point in the history
  • Loading branch information
oozcitak committed Aug 31, 2020
1 parent 11da6a8 commit 21263b3
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
8 changes: 6 additions & 2 deletions src/builder/XMLBuilderImpl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -702,10 +702,14 @@ export class XMLBuilderImpl implements XMLBuilder {
for (const attr of ele.attributes) {
const attrQName = attr.prefix ? attr.prefix + ':' + attr.localName : attr.localName
const [attrPrefix] = namespace_extractQName(attrQName)
if (attrPrefix === null) {
let newAttrNS = attr.namespaceURI
if (newAttrNS === null && attrPrefix !== null) {
newAttrNS = ele.lookupNamespaceURI(attrPrefix)
}

if (newAttrNS === null) {
newEle.setAttribute(attrQName, attr.value)
} else {
const newAttrNS = newEle.lookupNamespaceURI(attrPrefix)
newEle.setAttributeNS(newAttrNS, attrQName, attr.value)
}
}
Expand Down
20 changes: 20 additions & 0 deletions test/issues/issue-051.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import $$ from "../TestHelpers";

describe("Replicate issue", () => {
// https://github.com/oozcitak/xmlbuilder2/issues/51
test("#51 - NamespaceError: The operation is not allowed by Namespaces in XML. [XMLNS] Qualified name includes a prefix but the namespace is null.", () => {
const root = $$.create({ version: '1.0', encoding: 'utf-8' })
.ele('archive', {
version: '4.0',
'xmlns:xsi': 'http://www.w3.org/2001/XMLSchema-instance',
'xsi:schemaLocation': 'http://xml.datev.de/bedi/tps/document/v04.0 document_v040.xsd',
xmlns: 'http://xml.datev.de/bedi/tps/document/v04.0'
})
const xml = root.end({ headless: true, prettyPrint: true });

expect(xml).toBe($$.t`
<archive version="4.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xml.datev.de/bedi/tps/document/v04.0 document_v040.xsd" xmlns="http://xml.datev.de/bedi/tps/document/v04.0"/>
`)
})

})

0 comments on commit 21263b3

Please sign in to comment.