Skip to content
Permalink
Browse files

Handle emtpy literals correctly

  • Loading branch information...
wschella authored and rubensworks committed Feb 13, 2019
1 parent 9ca6534 commit 47b67208443899287564882bac6c8f9e193fd359
Showing with 46 additions and 12 deletions.
  1. +12 −12 lib/SparqlXmlParser.ts
  2. +34 −0 test/SparqlXmlParser-test.ts
@@ -62,23 +62,23 @@ export class SparqlXmlParser {
for (const binding of bindingsArray) {
if (binding.attribs && binding.children) {
const key = binding.attribs.name;
let value: RDF.Term = null;
let term: RDF.Term = null;
if (binding.children.bnode) {
value = this.dataFactory.blankNode(binding.children.bnode.value);
term = this.dataFactory.blankNode(binding.children.bnode.value);
} else if (binding.children.literal) {
if (binding.children.literal.attribs && binding.children.literal.attribs['xml:lang']) {
value = this.dataFactory.literal(binding.children.literal.value,
binding.children.literal.attribs['xml:lang']);
} else if (binding.children.literal.attribs && binding.children.literal.attribs.datatype) {
value = this.dataFactory.literal(binding.children.literal.value,
this.dataFactory.namedNode(binding.children.literal.attribs.datatype));
const value = binding.children.literal.value || "";
const attribs = binding.children.literal.attribs;
if (attribs && attribs['xml:lang']) {
term = this.dataFactory.literal(value, attribs['xml:lang']);
} else if (attribs && attribs.datatype) {
term = this.dataFactory.literal(value, this.dataFactory.namedNode(attribs.datatype));
} else {
value = this.dataFactory.literal(binding.children.literal.value);
term = this.dataFactory.literal(value);
}
} else {
value = this.dataFactory.namedNode(binding.children.uri.value);
term = this.dataFactory.namedNode(binding.children.uri.value);
}
bindings[this.prefixVariableQuestionMark ? ('?' + key) : key] = value;
bindings[this.prefixVariableQuestionMark ? ('?' + key) : key] = term;
}
}
}
@@ -87,7 +87,7 @@ export class SparqlXmlParser {

/**
* Convert a SPARQL XML boolean response stream to a promise resolving to a boolean.
* This will reject if the given reponse was not a valid boolean response.
* This will reject if the given response was not a valid boolean response.
* @param {NodeJS.ReadableStream} sparqlResponseStream A SPARQL XML response stream.
* @return {NodeJS.ReadableStream} A stream of bindings.
*/
@@ -415,6 +415,40 @@ describe('SparqlXmlParser', () => {
return expect(parser.parseXmlBindings(binding)).toEqual({ '?book': literal('abc') });
});

it('should convert bindings with empty literals', () => {
const binding = {
children: {
binding: [
{
attribs: { name: 'book' },
children: {
literal: { value: '' },
},
},
],
},
};
return expect(parser.parseXmlBindings(binding)).toEqual({ '?book': literal('') });
});

it('should convert bindings with empty literals', async () => {
return expect(await arrayifyStream(parser.parseXmlResultsStream(streamifyString(`<?xml version="1.0"?>
<sparql xmlns="http://www.w3.org/2005/sparql-results#">
<head>
<variable name="x"/>
</head>
<results>
<result>
<binding name="x">
<literal></literal>
</binding>
</result>
</results>
</sparql>
`))))
.toEqual([{ '?x': literal("") }]);
});

it('should convert bindings with languaged literals', () => {
const binding = {
children: {

0 comments on commit 47b6720

Please sign in to comment.
You can’t perform that action at this time.