Skip to content

Commit

Permalink
Fix trix parser to allow lowercase trix, add tests (#1966)
Browse files Browse the repository at this point in the history
Changed TriX parser to allow `trix` and `TriX`

The RDFLib TriX parser currently only accepts TriX documents conforming to the the [Nokia-published XSD](https://web.archive.org/web/20040821093939/http://swdev.nokia.com/trix/trix-1.0.xsd) which specifies (the mixed-case) `TriX`, contradicting the [W3C-published XSD spec for TriX](https://www.w3.org/2004/03/trix/trix-1/trix-1.0.xsd) which specifies (the lower-case) `trix`. We should accept both.

We were also a bit light on a TriX test suite for exercising the parser, so I recruited some TriX test fixtures from NG4J and Jena and fleshed out the TriX test suite, using the W3C "Manifest"-style approach.
  • Loading branch information
Graham Higgins committed May 25, 2022
1 parent 685dec5 commit 8e24878
Show file tree
Hide file tree
Showing 135 changed files with 12,464 additions and 7 deletions.
4 changes: 2 additions & 2 deletions rdflib/plugins/parsers/trix.py
Expand Up @@ -56,7 +56,7 @@ def startElementNS(self, name, qname, attrs):
% (name[0], TRIXNS)
)

if name[1] == "TriX":
if name[1].lower() == "trix":
if self.state == 0:
self.state = 1
else:
Expand Down Expand Up @@ -205,7 +205,7 @@ def endElementNS(self, name, qname):
self.graph = None
self.state = 1

elif name[1] == "TriX":
elif name[1].lower() == "trix":
self.state = 0

else:
Expand Down
42 changes: 42 additions & 0 deletions test/data/suites/trix/README
@@ -0,0 +1,42 @@
This README is adapted from the README of the W3C RDF Working Group's
TriG test suite and modified for use with RDFLib's informally-assembled
TrIX test suite. This test suite contains three kinds of tests:

Evaluation (rdft:TestTrixEval) - a pair of an input trig file
and reference ntriples file.

Positive syntax (rdft:TestTrixPositiveSyntax) - an input trig
file with no syntax errors.

Negative syntax (rdft:TestTrixNegativeSyntax) - an input trig
file with at least one syntax error.

The manifest.ttl file in this directory lists all of the tests in the
RDFLib TriX test suite. Each test is one of the above tests. All
tests have a name (mf:name) and an input (mf:action). The Evaluation
tests have an expected result (mf:result).

• An implementation passes an Evaluation test if it parses the input
into a graph, parses the expecte result into another graph, and
those two graphs are isomorphic (see
<http://www.w3.org/TR/rdf11-concepts/#graph-isomorphism>).

• An implementation passes a positive syntax test if it parses the
input.

• An implementation passes a negative syntax test if it fails to parse
the input.

The IRI for the test suite is <https://rdflib.github.io/tests/trix/>.
Per RFC 3986 section 5.1.3, the base IRI for parsing each file is the
retrieval IRI for that file. For example, the tests trig-subm-01 and
trig-subm-27 require relative IRI resolution against a base of
<https://rdflib.github.io/tests/trix/trig-subm-01.trix> and
<https://rdflib.github.io/tests/trix/strig-subm-27.trix> respectively.


Adapted from https://github.com/w3c/rdf-tests/blob/main/trig/README,
authors:

Eric Prud'hommeaux <eric+turtle@w3.org> - 11 June 2013.
Gregg Kellogg <gregg@greggkellogg.net> - 12 June 2013.
20 changes: 20 additions & 0 deletions test/data/suites/trix/extension1.xsl
@@ -0,0 +1,20 @@
<?xml version="1.0"?>
<!-- Replaces any XML document by a hard-coded TriX document containing
one graph with one triple -->
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns="http://www.w3.org/2004/03/trix/trix-1/"
xmlns:ex="http://example.com/#"
version="2.0">
<xsl:template match="/">
<TriX>
<graph>
<triple>
<qname>ex:a</qname>
<qname>ex:b</qname>
<qname>ex:c</qname>
</triple>
</graph>
</TriX>
</xsl:template>
</xsl:stylesheet>
33 changes: 33 additions & 0 deletions test/data/suites/trix/extension2.xsl
@@ -0,0 +1,33 @@
<?xml version="1.0"?>
<!-- Expands <qname>ns:foo</qname> to <uri>.....foo</uri> using xmlns:ns -->
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns="http://www.w3.org/2004/03/trix/trix-1/"
xmlns:trix="http://www.w3.org/2004/03/trix/trix-1/"
xmlns:ex="http://example.com/#"
version="2.0">

<!-- Expand qnames -->
<xsl:template match="trix:qname">
<uri>
<xsl:variable name="ns">
<xsl:value-of select="substring-before(text(),':')"/>
</xsl:variable>
<xsl:value-of select="namespace::*[local-name()=$ns]"/>
<xsl:value-of select="substring-after(text(),':')"/>
</uri>
</xsl:template>

<!-- Not necessary, but avoids namespace declarations scattered through
the result document -->
<xsl:template match="trix:TriX">
<TriX><xsl:apply-templates/></TriX>
</xsl:template>

<!-- This is a simple identity function -->
<xsl:template match="@*|*|processing-instruction()|comment()" priority="-2">
<xsl:copy>
<xsl:apply-templates select="*|@*|text()|processing-instruction()|comment()"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>

0 comments on commit 8e24878

Please sign in to comment.