Skip to content

Commit

Permalink
Support namespace declarations on node elements and typed node elemen…
Browse files Browse the repository at this point in the history
…ts (#72)

* Add support for namespace declaration using xmlns.

* Handle namespace declarations on resource nodes

* Support namespace declaration on typed elements
  • Loading branch information
matthiaspalmer authored Nov 24, 2023
1 parent ba3994b commit 7e35733
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 6 deletions.
4 changes: 3 additions & 1 deletion lib/RdfXmlParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,9 @@ while ${attribute.value} and ${activeSubjectValue} where found.`);

// Interpret attributes at this point as properties on this node,
// but we ignore attributes that have no prefix or known expanded URI
if (attribute.prefix !== 'xml' && attribute.uri) {
if (attribute.prefix !== 'xml' && attribute.prefix !== 'xmlns'
&& (attribute.prefix !== '' || attribute.local !== 'xmlns')
&& attribute.uri) {
predicates.push(this.uriToNamedNode(attribute.uri + attribute.local));
objects.push(attribute.value);
}
Expand Down
60 changes: 55 additions & 5 deletions test/RdfXmlParser-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -930,20 +930,71 @@ abc`)).rejects.toBeTruthy();
]);
});


it('declaration of the namespace on the element', async () => {
it('declaration of the default namespace on the property element', async () => {
return expect(await parse(parser, `<?xml version="1.0"?>
<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">
<rdf:Description rdf:about="http://www.w3.org/TR/rdf-syntax-grammar">
<rdf:Description rdf:about="http://example.com">
<title xmlns="http://purl.org/dc/terms/" xml:lang="en">RDF1.1 XML Syntax</title>
</rdf:Description>
</rdf:RDF>`))
.toBeRdfIsomorphic([
quad('http://www.w3.org/TR/rdf-syntax-grammar',
quad('http://example.com',
'http://purl.org/dc/terms/title', '"RDF1.1 XML Syntax"@en'),
]);
});

it('declaration of the namespace on the property element', async () => {
return expect(await parse(parser, `<?xml version="1.0"?>
<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">
<rdf:Description rdf:about="http://example.com">
<dct:title xmlns:dct="http://purl.org/dc/terms/" xml:lang="en">RDF1.1 XML Syntax</dct:title>
</rdf:Description>
</rdf:RDF>`))
.toBeRdfIsomorphic([
quad('http://example.com',
'http://purl.org/dc/terms/title', '"RDF1.1 XML Syntax"@en'),
]);
});

it('declaration of the namespace on the resource element', async () => {
return expect(await parse(parser, `<?xml version="1.0"?>
<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">
<rdf:Description rdf:about="http://example.com" xmlns:dct="http://purl.org/dc/terms/">
<dct:title xml:lang="en">RDF1.1 XML Syntax</dct:title>
</rdf:Description>
</rdf:RDF>`))
.toBeRdfIsomorphic([
quad('http://example.com',
'http://purl.org/dc/terms/title', '"RDF1.1 XML Syntax"@en'),
]);
});

it('declaration of the default namespace on the resource element', async () => {
return expect(await parse(parser, `<?xml version="1.0"?>
<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">
<rdf:Description rdf:about="http://example.com" xmlns="http://purl.org/dc/terms/">
<title xml:lang="en">RDF1.1 XML Syntax</title>
</rdf:Description>
</rdf:RDF>`))
.toBeRdfIsomorphic([
quad('http://example.com',
'http://purl.org/dc/terms/title', '"RDF1.1 XML Syntax"@en'),
]);
});


it('declaration of the namespace on a typed resource element', async () => {
return expect(await parse(parser, `<?xml version="1.0"?>
<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">
<dct:Standard rdf:about="http://example.com" xmlns:dct="http://purl.org/dc/terms/">
</dct:Standard>
</rdf:RDF>`))
.toBeRdfIsomorphic([
quad('http://example.com',
'http://www.w3.org/1999/02/22-rdf-syntax-ns#type', 'http://purl.org/dc/terms/Standard'),
]);
});

it('cdata support', async () => {
return expect(await parse(parser, `<?xml version="1.0"?>
<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:dct="http://purl.org/dc/terms/" >
Expand All @@ -957,7 +1008,6 @@ abc`)).rejects.toBeTruthy();
]);
});


it('DOCTYPE and ENTITY\'s', async () => {
return expect(await parse(parser, `<!DOCTYPE rdf:RDF
[<!ENTITY rdf "http://www.w3.org/1999/02/22-rdf-syntax-ns#">
Expand Down

0 comments on commit 7e35733

Please sign in to comment.