RDFLib / rdflib

RDFLib is a Python library for working with RDF, a simple yet powerful language for representing information.
https://rdflib.readthedocs.org
BSD 3-Clause "New" or "Revised" License
2.17k stars 556 forks source link

Bad syntax (bad escape) at... on a string #942

Closed moissinac closed 4 years ago

moissinac commented 5 years ago

Hello

I have a text file (test.n3) with the following content: http://gv.eu/myuri http://gv.eu/hasScript """

""".

When doing gexpo = rdflib.Graph() gexpo.parse(source="test.n3", format="n3") I get an error: ... File "C:\outils\Python\Python36\lib\site-packages\rdflib\plugins\parsers\notation3.py", line 1615, in BadSyntax raise BadSyntax(self._thisDoc, self.lines, argstr, i, msg) rdflib.plugins.parsers.notation3.BadSyntax: at line 3 of <>: Bad syntax (bad escape) at ^ in: "...b'riptTag = document.createElement("script");scriptTag.src = "'^b'\/themes\/custom\/pmp\/javascripts\/google-analytics.js";doc'..."

and I doesn't find how to manage the problem (solution or workaround) Thank's for any help

JervenBolleman commented 4 years ago

Your example does not follow the N3 syntax for escaping characters (\/ is not allowed I think according to https://www.w3.org/TeamSubmission/n3/#escaping)

nicholascar commented 4 years ago

@moissinac just reiterating your issue to be clear:

The string:

<http://gv.eu/myuri> <http://gv.eu/hasScript> """
<script>function euCookieComplianceLoadScripts() {var scriptTag = document.createElement("script");scriptTag.src = "\/themes\/custom\/pmp\/javascripts\/google-analytics.js";document.body.appendChild(scriptTag);}</script>
""".

Is indeed invalid N3 and the master branch version (5.0.0-dev) rdflib parser correctly fails to parse it with the message you quote above.

As @JervenBolleman says, you can't escape "/" using "\" in N3 so to make your N3 code valid, just replace "\/" with "/":

<http://gv.eu/myuri> <http://gv.eu/hasScript> """<script>function euCookieComplianceLoadScripts() {var scriptTag = document.createElement("script");scriptTag.src = "/themes/custom/pmp/javascripts/google-analytics.js";document.body.appendChild(scriptTag);}</script>""".

Then rdflib will parse it and you can print out the triple getting:

(
    rdflib.term.URIRef('http://gv.eu/myuri'), 
    rdflib.term.URIRef('http://gv.eu/hasScript'), 
    rdflib.term.Literal('<script>function euCookieComplianceLoadScripts() {var scriptTag = document.createElement("script");scriptTag.src = "/themes/custom/pmp/javascripts/google-analytics.js";document.body.appendChild(scriptTag);}</script>')
)

So I'm going to close this as it's not an rdflib problem to fix.

Also, for future Issues, please include quoted code or RDF in backtick "`" so it renders nicely, as I've done in this reply!