Skip to content

Commit 8e24878

Browse files
authored
Fix trix parser to allow lowercase trix, add tests (#1966)
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.
1 parent 685dec5 commit 8e24878

File tree

135 files changed

+12464
-7
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

135 files changed

+12464
-7
lines changed

rdflib/plugins/parsers/trix.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ def startElementNS(self, name, qname, attrs):
5656
% (name[0], TRIXNS)
5757
)
5858

59-
if name[1] == "TriX":
59+
if name[1].lower() == "trix":
6060
if self.state == 0:
6161
self.state = 1
6262
else:
@@ -205,7 +205,7 @@ def endElementNS(self, name, qname):
205205
self.graph = None
206206
self.state = 1
207207

208-
elif name[1] == "TriX":
208+
elif name[1].lower() == "trix":
209209
self.state = 0
210210

211211
else:

test/data/suites/trix/README

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
This README is adapted from the README of the W3C RDF Working Group's
2+
TriG test suite and modified for use with RDFLib's informally-assembled
3+
TrIX test suite. This test suite contains three kinds of tests:
4+
5+
Evaluation (rdft:TestTrixEval) - a pair of an input trig file
6+
and reference ntriples file.
7+
8+
Positive syntax (rdft:TestTrixPositiveSyntax) - an input trig
9+
file with no syntax errors.
10+
11+
Negative syntax (rdft:TestTrixNegativeSyntax) - an input trig
12+
file with at least one syntax error.
13+
14+
The manifest.ttl file in this directory lists all of the tests in the
15+
RDFLib TriX test suite. Each test is one of the above tests. All
16+
tests have a name (mf:name) and an input (mf:action). The Evaluation
17+
tests have an expected result (mf:result).
18+
19+
• An implementation passes an Evaluation test if it parses the input
20+
into a graph, parses the expecte result into another graph, and
21+
those two graphs are isomorphic (see
22+
<http://www.w3.org/TR/rdf11-concepts/#graph-isomorphism>).
23+
24+
• An implementation passes a positive syntax test if it parses the
25+
input.
26+
27+
• An implementation passes a negative syntax test if it fails to parse
28+
the input.
29+
30+
The IRI for the test suite is <https://rdflib.github.io/tests/trix/>.
31+
Per RFC 3986 section 5.1.3, the base IRI for parsing each file is the
32+
retrieval IRI for that file. For example, the tests trig-subm-01 and
33+
trig-subm-27 require relative IRI resolution against a base of
34+
<https://rdflib.github.io/tests/trix/trig-subm-01.trix> and
35+
<https://rdflib.github.io/tests/trix/strig-subm-27.trix> respectively.
36+
37+
38+
Adapted from https://github.com/w3c/rdf-tests/blob/main/trig/README,
39+
authors:
40+
41+
Eric Prud'hommeaux <[email protected]> - 11 June 2013.
42+
Gregg Kellogg <[email protected]> - 12 June 2013.
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?xml version="1.0"?>
2+
<!-- Replaces any XML document by a hard-coded TriX document containing
3+
one graph with one triple -->
4+
<xsl:stylesheet
5+
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
6+
xmlns="http://www.w3.org/2004/03/trix/trix-1/"
7+
xmlns:ex="http://example.com/#"
8+
version="2.0">
9+
<xsl:template match="/">
10+
<TriX>
11+
<graph>
12+
<triple>
13+
<qname>ex:a</qname>
14+
<qname>ex:b</qname>
15+
<qname>ex:c</qname>
16+
</triple>
17+
</graph>
18+
</TriX>
19+
</xsl:template>
20+
</xsl:stylesheet>
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?xml version="1.0"?>
2+
<!-- Expands <qname>ns:foo</qname> to <uri>.....foo</uri> using xmlns:ns -->
3+
<xsl:stylesheet
4+
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
5+
xmlns="http://www.w3.org/2004/03/trix/trix-1/"
6+
xmlns:trix="http://www.w3.org/2004/03/trix/trix-1/"
7+
xmlns:ex="http://example.com/#"
8+
version="2.0">
9+
10+
<!-- Expand qnames -->
11+
<xsl:template match="trix:qname">
12+
<uri>
13+
<xsl:variable name="ns">
14+
<xsl:value-of select="substring-before(text(),':')"/>
15+
</xsl:variable>
16+
<xsl:value-of select="namespace::*[local-name()=$ns]"/>
17+
<xsl:value-of select="substring-after(text(),':')"/>
18+
</uri>
19+
</xsl:template>
20+
21+
<!-- Not necessary, but avoids namespace declarations scattered through
22+
the result document -->
23+
<xsl:template match="trix:TriX">
24+
<TriX><xsl:apply-templates/></TriX>
25+
</xsl:template>
26+
27+
<!-- This is a simple identity function -->
28+
<xsl:template match="@*|*|processing-instruction()|comment()" priority="-2">
29+
<xsl:copy>
30+
<xsl:apply-templates select="*|@*|text()|processing-instruction()|comment()"/>
31+
</xsl:copy>
32+
</xsl:template>
33+
</xsl:stylesheet>

0 commit comments

Comments
 (0)