# sparqlxml-parse

> Parses SPARQL XML query results

Latest version **3.3.0** (published 2025-10-24) · MIT license · 0 weekly downloads

## Install

```sh
npm install sparqlxml-parse
pnpm add sparqlxml-parse
yarn add sparqlxml-parse
bun add sparqlxml-parse
```

## Health

**Score 60/100 (C)** — status: stable.

Positive: has types; no vulnerabilities; high maintenance score; high quality score.

Warnings: low downloads; no esm support.

## Facts

| | |
|---|---|
| Version | 3.3.0 |
| Published | 2025-10-24 |
| First published | 2018-08-21 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 5 |
| Unpacked size | 41.4 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 2 |
| Author | Ruben Taelman |
| Maintainers | rubensworks |
| Keywords | sparql, xml, rdfjs, rdf, linked data |

## Links

- npm: https://www.npmjs.com/package/sparqlxml-parse
- Repository: https://github.com/rubensworks/sparqlxml-parse.js
- Homepage: https://github.com/rubensworks/sparqlxml-parse.js#readme
- Issues: https://github.com/rubensworks/sparqlxml-parse.js/issues
- Funding: https://github.com/sponsors/rubensworks/
- npm.io page: https://npm.io/package/sparqlxml-parse

## Dependencies (5)

- [buffer](https://npm.io/package/buffer.md) ^6.0.3
- [readable-stream](https://npm.io/package/readable-stream.md) ^4.5.2
- [rdf-data-factory](https://npm.io/package/rdf-data-factory.md) ^2.0.0
- [@rubensworks/saxes](https://npm.io/package/@rubensworks/saxes.md) ^6.0.1
- [@types/readable-stream](https://npm.io/package/@types/readable-stream.md) ^4.0.0

## Alternatives

- [babylon](https://npm.io/package/babylon.md) — 5.1M weekly downloads
- [csscolorparser](https://npm.io/package/csscolorparser.md) — 3.7M weekly downloads
- [expr-eval-fork](https://npm.io/package/expr-eval-fork.md) — 1.5M weekly downloads
- [@leeoniya/ufuzzy](https://npm.io/package/@leeoniya/ufuzzy.md) — 247.7K weekly downloads
- [xml-parser](https://npm.io/package/xml-parser.md) — 78.4K weekly downloads

## Recent versions

- 3.3.0 (latest) — 2025-10-24
- 3.2.0 — 2025-10-23
- 3.1.0 — 2025-06-17
- 3.0.0 — 2025-01-08
- 2.1.1 — 2023-06-05
- 2.1.0 — 2023-01-27
- 2.0.2 — 2022-11-09
- 2.0.1 — 2022-07-15
- 2.0.0 — 2022-07-14
- 1.5.0 — 2021-08-11
- 1.4.0 — 2020-09-16
- 1.3.0 — 2020-08-24
- 1.2.2 — 2019-08-22
- 1.2.1 — 2019-02-13
- 1.2.0 — 2018-11-08
- … 5 more at https://npm.io/package/sparqlxml-parse/versions

## README

# SPARQL-Results+XML Parse

[![Build status](https://github.com/rubensworks/sparqlxml-parse.js/workflows/CI/badge.svg)](https://github.com/rubensworks/sparqlxml-parse.js/actions?query=workflow%3ACI)
[![Coverage Status](https://coveralls.io/repos/github/rubensworks/sparqlxml-parse.js/badge.svg?branch=master)](https://coveralls.io/github/rubensworks/sparqlxml-parse.js?branch=master)
[![npm version](https://badge.fury.io/js/sparqlxml-parse.svg)](https://www.npmjs.com/package/sparqlxml-parse)

A utility package that allows you to parse [SPARQL XML](https://www.w3.org/TR/rdf-sparql-XMLres/) results
in a convenient [RDF/JS](http://rdf.js.org/)-based datastructure.

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

In:
```json
<?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:
```javascript
[
  { '?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

```javascript
import {SparqlXmlParser} from "sparqlxml-parse";

const sparqlXmlParser = new SparqlXmlParser();
```

Optionally, you can provide a settings object to the constructor with optional parameters:
```javascript
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.

```javascript
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:
```javascript
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') ]
```

The error thrown for unsupported versions can be skipped
by setting `parseUnsupportedVersions` to `true` when constructing the parser.

### Convert a SPARQL XML boolean response stream

```javascript
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](http://rubensworks.net/).

This code is released under the [MIT license](http://opensource.org/licenses/MIT).

---
_Source: https://npm.io/package/sparqlxml-parse · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
