• Cast a SPARQL variable to a specific XSD datatype

    This ensures type safety for comparisons and operations. The cast will fail at query execution time if the value cannot be converted to the target type (e.g., casting "abc" to xsd:integer).

    Parameters

    • variable: any

      The SPARQL variable to cast (e.g., ?age)

    • targetType: NamedNode

      The target XSD datatype (e.g., xsd.integer, xsd.decimal)

    Returns SparqlTemplateResult

    SPARQL expression that casts the variable to the target type

    // Cast to integer for numeric comparison
    const castVar = castVariable(propertyVar, xsd.integer);
    // Results in: xsd:integer(?age)
    // Use in filter
    filters: [sparql`FILTER(${castVariable(propertyVar, xsd.integer)} >= 18)`]