Combine CSV Columns into an RDF Literal with CSVW

Last updated: November 26, 2026.

To emit one RDF literal from two CSV columns, compute the combined value in a real CSV column before CSVW conversion. A valueUrl template can combine column values to identify an RDF resource. It cannot produce a literal.

<?php
// Run: php derive.php < locations.csv > enriched.csv
if (fgetcsv(STDIN, escape: '') !== ['city', 'region']) {
    throw new RuntimeException('Expected city,region header');
}

fputcsv(STDOUT, ['city', 'region', 'label'], escape: '');

while (($row = fgetcsv(STDIN, escape: '')) !== false) {
    if (count($row) !== 2 || trim($row[0]) === '' || trim($row[1]) === '') {
        throw new RuntimeException('Expected nonempty city and region');
    }
    [$city, $region] = $row;
    fputcsv(STDOUT, [$city, $region, "$city, $region"], escape: '');
}
if (!feof(STDIN)) {
    throw new RuntimeException('Input read failed');
}

Map the derived column as a string

With an input row such as Halifax,NS, the output row contains a third field whose value is Halifax, NS. PHP quotes that field because it contains a comma. The quotes are CSV syntax and disappear when the converter reads the cell.

In the metadata for enriched.csv, describe the three physical columns in file order. For the last column, set name to label, datatype to string and propertyUrl to the predicate you want. Leave valueUrl unset, including in any metadata from which the column inherits. Use suppressOutput on either source column if you only want the combined literal in the RDF.

Check the object in the RDF output

In Turtle, look for a quoted string in the object position. An object displayed as an IRI means the converter still has a value URL for that cell. A relative template still takes that branch, and adding a datatype does not override it.

For dates or times, validate each source field and produce the lexical form required by the target datatype before changing the metadata. If the source CSV must stay immutable, generate a separate enriched file and attach the mapping to that file. The example writes to standard output, so keep the source and output paths distinct and use the result only if the command exits successfully.

Documentation

Related guides

Sergey Kornilov

Sergey Kornilov