Skip to content

Commit 98d6ab5

Browse files
committed
Include parent namespaces in XML Literals
1 parent b828deb commit 98d6ab5

File tree

2 files changed

+40
-7
lines changed

2 files changed

+40
-7
lines changed

lib/RdfXmlParser.ts

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,9 @@ export class RdfXmlParser extends Transform implements RDF.Sink<EventEmitter, RD
193193
// Convert this tag to a string
194194
const tagName: string = tag.name;
195195
let attributes: string = '';
196+
for (const { key, value } of parentTag.namespaces || []) {
197+
attributes += ` ${key}="${value}"`;
198+
}
196199
for (const attributeKey in tag.attributes) {
197200
attributes += ` ${attributeKey}="${tag.attributes[attributeKey].value}"`;
198201
}
@@ -211,13 +214,11 @@ export class RdfXmlParser extends Transform implements RDF.Sink<EventEmitter, RD
211214

212215
const activeTag: IActiveTag = {};
213216
if (parentTag) {
214-
// Inherit language scope, direction scope and baseIRI from parent
217+
// Inherit properties from parent
215218
activeTag.language = parentTag.language;
216219
activeTag.direction = parentTag.direction;
217220
activeTag.baseIRI = parentTag.baseIRI;
218-
// Also inherit triple term collection array
219221
activeTag.childrenTripleTerms = parentTag.childrenTripleTerms;
220-
// Also RDF version
221222
activeTag.rdfVersion = parentTag.rdfVersion;
222223
} else {
223224
activeTag.baseIRI = this.baseIRI;
@@ -229,6 +230,19 @@ export class RdfXmlParser extends Transform implements RDF.Sink<EventEmitter, RD
229230
} else { // currentParseType === ParseType.PROPERTY
230231
this.onTagProperty(tag, activeTag, parentTag);
231232
}
233+
234+
for (const attributeKey in tag.attributes) {
235+
const attribute = tag.attributes[attributeKey];
236+
if (attribute.prefix === 'xmlns') {
237+
if (!activeTag.namespaces) {
238+
activeTag.namespaces = [];
239+
}
240+
activeTag.namespaces.push({ key: `${attribute.prefix}:${attribute.local}`, value: attribute.value });
241+
}
242+
}
243+
if (parentTag && parentTag.namespaces) {
244+
activeTag.namespaces = [ ...activeTag.namespaces || [], ...parentTag.namespaces ];
245+
}
232246
}
233247

234248
/**
@@ -809,6 +823,7 @@ export interface IActiveTag {
809823
childrenTripleTerms?: RDF.Quad[];
810824
reifier?: RDF.NamedNode | RDF.BlankNode;
811825
rdfVersion?: string;
826+
namespaces?: { key: string; value: string }[];
812827
}
813828

814829
export enum ParseType {

test/RdfXmlParser-test.ts

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2293,8 +2293,8 @@ abc`)).rejects.toBeTruthy();
22932293
<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
22942294
xmlns:ex="http://example.org/stuff/1.0/">
22952295
<rdf:Description rdf:about="http://example.org/item01">
2296-
<ex:prop rdf:parseType="Literal">
2297-
<a:Box required="true" xmlns:a="http://example.org/a#">
2296+
<ex:prop rdf:parseType="Literal" xmlns:a="http://example.org/a#">
2297+
<a:Box required="true">
22982298
<a:widget size="10" />
22992299
<a:grommit id="23">abc</a:grommit>
23002300
</a:Box>
@@ -2304,7 +2304,7 @@ abc`)).rejects.toBeTruthy();
23042304
return expect(array)
23052305
.toBeRdfIsomorphic([
23062306
quad('http://example.org/item01', 'http://example.org/stuff/1.0/prop',
2307-
'"\n <a:Box required="true" xmlns:a="http://example.org/a#">\n' +
2307+
'"\n <a:Box xmlns:a="http://example.org/a#" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:ex="http://example.org/stuff/1.0/" required="true">\n' +
23082308
' <a:widget size="10"></a:widget>\n' +
23092309
' <a:grommit id="23">abc</a:grommit>\n' +
23102310
' </a:Box>\n' +
@@ -2326,7 +2326,7 @@ abc`)).rejects.toBeTruthy();
23262326
return expect(array)
23272327
.toBeRdfIsomorphic([
23282328
quad('http://example.org/item01', 'http://example.org/stuff/1.0/prop',
2329-
'"\n <Box></Box>\n' +
2329+
'"\n <Box xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:ex="http://example.org/stuff/1.0/"></Box>\n' +
23302330
' "^^http://www.w3.org/1999/02/22-rdf-syntax-ns#XMLLiteral'),
23312331
]);
23322332
});
@@ -2900,6 +2900,24 @@ abc`)).rejects.toBeTruthy();
29002900
quad('_:an2', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#rest', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#nil'),
29012901
]);
29022902
});
2903+
2904+
it('on property elements with rdf:annotation with literal parse type', async () => {
2905+
const array = await parse(parser, `<?xml version="1.0"?>
2906+
<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
2907+
xmlns:eg="http://example.org/"
2908+
xml:base="http://example.com/">
2909+
2910+
<rdf:Description rdf:about="http://www.example.org/a">
2911+
<eg:prop rdf:annotation="http://example.com/triple1" rdf:parseType="Literal"><br /></eg:prop>
2912+
</rdf:Description>
2913+
2914+
</rdf:RDF>`);
2915+
return expect(array)
2916+
.toBeRdfIsomorphic([
2917+
quad('http://www.example.org/a', 'http://example.org/prop', '"<br xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:eg="http://example.org/"></br>"^^http://www.w3.org/1999/02/22-rdf-syntax-ns#XMLLiteral'),
2918+
quad('http://example.com/triple1', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#reifies', '<<http://www.example.org/a http://example.org/prop "<br xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:eg="http://example.org/"></br>"^^http://www.w3.org/1999/02/22-rdf-syntax-ns#XMLLiteral>>'),
2919+
]);
2920+
});
29032921
});
29042922

29052923
describe('streaming-wise', () => {

0 commit comments

Comments
 (0)