Skip to content

Commit f970161

Browse files
committed
Parse rdf:version
1 parent 265df9b commit f970161

File tree

3 files changed

+68
-2
lines changed

3 files changed

+68
-2
lines changed

README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,19 @@ fs.createReadStream('myfile.rdf')
4848
.on('end', () => console.log('All triples were parsed!'));
4949
```
5050

51+
### Read all version attribute values
52+
53+
```javascript
54+
const myParser = new RdfXmlParser();
55+
56+
fs.createReadStream('myfile.rdf')
57+
.pipe(myParser)
58+
.on('data', console.log)
59+
.on('version', console.log) // Log rdf:version attribute values
60+
.on('error', console.error)
61+
.on('end', () => console.log('All triples were parsed!'));
62+
```
63+
5164
### Manually write strings to the parser
5265

5366
```javascript

lib/RdfXmlParser.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,9 @@ export class RdfXmlParser extends Transform implements RDF.Sink<EventEmitter, RD
264264
let explicitType: string = null;
265265
for (const attributeKey in tag.attributes) {
266266
const attribute = tag.attributes[attributeKey];
267-
if (parentTag && attribute.uri === RdfXmlParser.RDF) {
267+
if (attribute.uri === RdfXmlParser.RDF && attribute.local === 'version') {
268+
this.emit('version', attribute.value);
269+
} else if (parentTag && attribute.uri === RdfXmlParser.RDF) {
268270
switch (attribute.local) {
269271
case 'about':
270272
if (activeSubjectValue) {
@@ -435,7 +437,9 @@ while ${attribute.value} and ${activeSubjectValue} where found.`);
435437
const objects: (RDF.NamedNode | RDF.BlankNode | RDF.Literal)[] = [];
436438
for (const propertyAttributeKey in tag.attributes) {
437439
const propertyAttribute = tag.attributes[propertyAttributeKey];
438-
if (propertyAttribute.uri === RdfXmlParser.RDF) {
440+
if (propertyAttribute.uri === RdfXmlParser.RDF && propertyAttribute.local === 'version') {
441+
this.emit('version', propertyAttribute.value);
442+
} else if (propertyAttribute.uri === RdfXmlParser.RDF) {
439443
switch (propertyAttribute.local) {
440444
case 'resource':
441445
if (activeSubSubjectValue) {

test/RdfXmlParser-test.ts

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2473,6 +2473,55 @@ abc`)).rejects.toBeTruthy();
24732473
quad('http://ex.org/i/subject', 'http://anon.org/o/hasBlankNode', '_:yellow"'),
24742474
]);
24752475
});
2476+
2477+
it('rdf:version attribute on the root tag', async () => {
2478+
const cb = jest.fn();
2479+
parser.on('version', cb);
2480+
await parse(parser, `<?xml version="1.0"?>
2481+
<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
2482+
xmlns:dc="http://purl.org/dc/elements/1.1/"
2483+
xmlns:ex="http://example.org/stuff/1.0/"
2484+
rdf:version="1.2">
2485+
<rdf:Description>
2486+
<ex:editor>
2487+
<rdf:Description></rdf:Description>
2488+
</ex:editor>
2489+
</rdf:Description>
2490+
</rdf:RDF>`);
2491+
return expect(cb).toHaveBeenCalledWith('1.2');
2492+
});
2493+
2494+
it('rdf:version attribute a property tag', async () => {
2495+
const cb = jest.fn();
2496+
parser.on('version', cb);
2497+
await parse(parser, `<?xml version="1.0"?>
2498+
<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
2499+
xmlns:dc="http://purl.org/dc/elements/1.1/"
2500+
xmlns:ex="http://example.org/stuff/1.0/">
2501+
<rdf:Description>
2502+
<ex:editor rdf:version="1.2">
2503+
<rdf:Description></rdf:Description>
2504+
</ex:editor>
2505+
</rdf:Description>
2506+
</rdf:RDF>`);
2507+
return expect(cb).toHaveBeenCalledWith('1.2');
2508+
});
2509+
2510+
it('rdf:version attribute an internal tag', async () => {
2511+
const cb = jest.fn();
2512+
parser.on('version', cb);
2513+
await parse(parser, `<?xml version="1.0"?>
2514+
<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
2515+
xmlns:dc="http://purl.org/dc/elements/1.1/"
2516+
xmlns:ex="http://example.org/stuff/1.0/">
2517+
<rdf:Description>
2518+
<ex:editor>
2519+
<rdf:Description rdf:version="1.2"></rdf:Description>
2520+
</ex:editor>
2521+
</rdf:Description>
2522+
</rdf:RDF>`);
2523+
return expect(cb).toHaveBeenCalledWith('1.2');
2524+
});
24762525
});
24772526

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

0 commit comments

Comments
 (0)