I am trying to run a SPARQL query in Python, however, when trying to use bind & replace, escaping the " is not working resulting in an error.
I tried to find some info on unicoding, but seem to find it hard to include it within this query.
Does anyone have a solution for the problem?
TEXT:
from rdflib import Graph
from rdflib.namespace import RDF, SKOS
g = Graph()
g.parse('\Python\Molenakker.orox.ttl')
len(g)
print(len(g))
gmls = g.query('''
PREFIX gwsw: <http://data.gwsw.nl/1.5/totaal/>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX sparql: <http://sparql.gwsw.nl/bim/juinen#>
SELECT ?XYZ
WHERE {
{
?uri rdf:type gwsw:GemengdRiool;
gwsw:hasAspect ?ori.
?ori gwsw:hasAspect ?lijn.
?lijn gwsw:hasValue ?GML.
BIND(REPLACE(STR(?GML),"<gml:LineString xmlns:gml=\"http://www.opengis.net/gml\"><gml:posList srsDimension=\"3\">","") AS ?gml)
BIND(REPLACE(STR(?gml),"</gml:posList></gml:LineString>","")AS ?XYZ)
}
UNION
{
?uri rdf:type gwsw:Hemelwaterriool;
gwsw:hasAspect ?ori.
?ori gwsw:hasAspect ?lijn.
?lijn gwsw:hasValue ?GML.
BIND(REPLACE(STR(?GML),"<gml:LineString xmlns:gml=\"http://www.opengis.net/gml\"><gml:posList srsDimension=\"3\">","") AS ?gml)
BIND(REPLACE(STR(?gml),"</gml:posList></gml:LineString>","")AS ?XYZ)
}
UNION
{
?uri rdf:type gwsw:Vuilwaterriool;
gwsw:hasAspect ?ori.
?ori gwsw:hasAspect ?lijn.
?lijn gwsw:hasValue ?GML.
BIND(REPLACE(STR(?GML),"<gml:LineString xmlns:gml=\"http://www.opengis.net/gml\"><gml:posList srsDimension=\"3\">","") AS ?gml)
BIND(REPLACE(STR(?gml),"</gml:posList></gml:LineString>","")AS ?XYZ)
}
}''')
for gml in gmls:
print(f"{gml.XYZ}")
Since you are using a triple quote for constructing a multi-line string, the backslash-doublequote becomes simply a doublequote.
'''
"<gml:LineString xmlns:gml=\"http://www.opengis.net/gml\"><gml:posList srsDimension=\"3\">"
'''
becomes
'''
"<gml:LineString xmlns:gml="http://www.opengis.net/gml"><gml:posList srsDimension="3">"
'''
which is probably not what you want.
Try to rewrite it as
'''
'<gml:LineString xmlns:gml="http://www.opengis.net/gml"><gml:posList srsDimension="3">'
'''