npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

sparqlxml-parse

v2.1.1

Published

Parses SPARQL XML query results

Downloads

45,685

Readme

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 as follows by listening to the variables event:

sparqlXmlParser.parseXmlResultsStream(sparqlJsonResponseStream)
    .on('variables', (variables: RDF.Variable[]) => console.log(variables))
    .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.