Skip to content

SPARQL query result XML serialization misses 0.0 decimal values #3138

@mamiba

Description

@mamiba

Hi,

I'm using rdflib version 7.1.4.

If I serialize a SPARQL query result that includes the decimal value 0.0 using results.serialize(format="xml"), the XML output is missing the literal value.

It is working as expected for the other formats, but not for XML.

See below minimal working example.

from rdflib import Graph, Namespace

turtle_string = """
@prefix ex: <http://example.org/> .
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .

ex:item1
    a ex:Item ;
    ex:name "Smartphone" ;
    ex:price 0.0 ;
.
"""

g = Graph()
g.parse(data=turtle_string, format="turtle")
results = g.query(
    """
    SELECT ?item ?name ?price
    WHERE {
        ?item
            a ex:Item ;
            ex:name ?name ;
            ex:price ?price ;
        .
    }
    """,
    initNs={"ex": Namespace("http://example.org/")},
)

formats = ["csv", "json", "txt", "xml"]
for format_type in formats:
    result = results.serialize(format=format_type).decode("utf-8")

    contains_zero = "0.0" in result
    print(f"{format_type}: Contains '0.0': {contains_zero}\n")
    print(f"````{format_type}\n{result}\n````\n")

Output:

csv: Contains '0.0': True

item,name,price
http://example.org/item1,Smartphone,0.0

json: Contains '0.0': True

{"results": {"bindings": [{"item": {"type": "uri", "value": "http://example.org/item1"}, "name": {"type": "literal", "value": "Smartphone"}, "price": {"type": "literal", "value": "0.0", "datatype": "http://www.w3.org/2001/XMLSchema#decimal"}}]}, "head": {"vars": ["item", "name", "price"]}}

txt: Contains '0.0': True

           item           |    name    |                      price
------------------------------------------------------------------------------------------
<http://example.org/item1>|"Smartphone"|"0.0"^^<http://www.w3.org/2001/XMLSchema#decimal>

xml: Contains '0.0': False

<?xml version="1.0" encoding="utf-8"?>
<sparql xmlns="http://www.w3.org/2005/sparql-results#" xmlns:xml="http://www.w3.org/XML/1998/namespace"><head><variable name="item"></variable><variable name="name"></variable><variable name="price"></variable></head><results><result><binding name="item"><uri>http://example.org/item1</uri></binding><binding name="name"><literal>Smartphone</literal></binding><binding name="price"><literal datatype="http://www.w3.org/2001/XMLSchema#decimal"></literal></binding></result></results></sparql>

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions