包详细信息

sparqlxml-parse

rubensworks99.1kMIT3.1.0

Parses SPARQL XML query results

sparql, xml, rdfjs, rdf

自述文件

SPARQL-Results+XML Parse

Build status Coverage Status npm version

A utility package that allows you to parse SPARQL XML results in a convenient RDF/JS-based datastructure.

For example, the following SPARQL XML result can be converted as follows:

In:

<?xml version="1.0"?>
<sparql xmlns="http://www.w3.org/2005/sparql-results#">
  <head>
    <variable name="book"/>
  </head>
  <results>
    <result>
      <binding name="book">
          <uri>http://example.org/book/book1</uri>
      </binding>
    </result>
    <result>
      <binding name="book">
          <uri>http://example.org/book/book2</uri>
      </binding>
    </result>
    <result>
      <binding name="book">
          <uri>http://example.org/book/book3</uri>
      </binding>
    </result>
    <result>
      <binding name="book">
          <uri>http://example.org/book/book4</uri>
      </binding>
    </result>
    <result>
      <binding name="book">
          <uri>http://example.org/book/book5</uri>
      </binding>
    </result>
    <result>
      <binding name="book">
        <triple>
          <subject>
            <uri>http://example.org/bob</uri>
          </subject>
          <predicate>
            <uri>http://example.org/name</uri>
          </predicate>
          <object>
            <literal datatype='http://example.org/Type'>Bob</literal>
          </object>
        </triple>
      </binding>
    </result>
  </results>
</sparql>

Out:

[
  { '?book': namedNode('http://example.org/book/book1') },
  { '?book': namedNode('http://example.org/book/book2') },
  { '?book': namedNode('http://example.org/book/book3') },
  { '?book': namedNode('http://example.org/book/book4') },
  { '?book': namedNode('http://example.org/book/book5') }, 
  { '?book': quad(namedNode('http://example.org/bob'), namedNode('http://example.org/name'), literal('Bob', namedNode('http://example.org/Type'))) },
]

Where namedNode is an RDF/JS named node, quad is an RDF/JS quad/triple, and literal is an RDF/JS literal.

This library automatically converts all SPARQL XML result values to their respective RDFJS type.

Usage

Create a new parser

import {SparqlXmlParser} from "sparqlxml-parse";

const sparqlXmlParser = new SparqlXmlParser();

Optionally, you can provide a settings object to the constructor with optional parameters:

const sparqlXmlParser = new SparqlXmlParser({
  dataFactory: dataFactory, // A custom RDFJS datafactory
  prefixVariableQuestionMark: true, // If variable names in the output should be prefixed with '?', default is false.
});

Convert a SPARQL XML response stream

If you have many query results, then a streaming-based approach as provided by sparqlXmlParser.parseXmlResultsStream is ideal.

const sparqlJsonResponseStream = streamifyString(`<?xml version="1.0"?>
<sparql xmlns="http://www.w3.org/2005/sparql-results#">
  <head>
    <variable name="book"/>
  </head>
  <results>
    <result>
      <binding name="book">
        <uri>http://example.org/book/book1</uri>
      </binding>
    </result>
  </results>
</sparql>`);
sparqlXmlParser.parseXmlResultsStream(sparqlJsonResponseStream)
    .on('data', (bindings: IBindings) => console.log(bindings));
// This will output [ { '?book': namedNode('http://example.org/book/book1') } ]

Optionally, you can also retrieve the variables inside the head and the version as follows by listening to the 'variables' and 'version' events:

sparqlXmlParser.parseXmlResultsStream(sparqlJsonResponseStream)
    .on('variables', (variables: RDF.Variable[]) => console.log(variables))
    .on('version', (version: string) => console.log(version))
    .on('data', (bindings: IBindings) => { return; });
// This will output [ variable('book') ]

Convert a SPARQL XML boolean response stream

const sparqlJsonResponseStream = streamifyString(`<?xml version="1.0"?>
<sparql xmlns="http://www.w3.org/2005/sparql-results#">
  <boolean>true</boolean>
</sparql>`);
sparqlXmlParser.parseXmlBooleanStream(sparqlJsonResponseStream)
    .then((result: boolean) => console.log(result));
// This will output true

License

This software is written by Ruben Taelman.

This code is released under the MIT license.

更新日志

Changelog

All notable changes to this project will be documented in this file.

v3.1.0 - 2025-06-17

Added

v3.0.0 - 2025-01-08

BREAKING CHANGES

Added

Fixed

v2.1.1 - 2023-06-05

Fixed

v2.1.0 - 2023-01-27

Added

v2.0.2 - 2022-11-09

Fixed

v2.0.1 - 2022-07-15

Fixed

v2.0.0 - 2022-07-14

This release has been marked as a major change due to the transition from Node's internal stream API to readable-stream. Most users should experience not breakages with this change.

Changed

v1.5.0 - 2021-08-11

Changed

v1.4.0 - 2020-09-16

Changed

v1.3.0 - 2020-08-24

Changed

v1.2.2 - 2019-08-22

Fixed

v1.2.1 - 2019-02-13

Fixed

v1.2.0 - 2018-11-08

Changed

1.1.3 - 2018-09-21

1.1.2 - 2018-09-05

1.1.1 - 2018-08-24

1.1.0 - 2018-08-23

[1.0.0] - 2018-08-21