relativizeIri function

String relativizeIri(
  1. String iri,
  2. String? baseIri
)

Converts an absolute IRI to a relative form when possible.

Takes an iri and attempts to express it relative to the given baseIri. This is useful for creating shorter, more readable RDF serializations.

Returns the original iri unchanged if:

  • baseIri is null or empty
  • The IRI cannot be safely relativized
  • The IRIs have different schemes or authorities

Examples:

relativizeIri('http://example.org/path/file.txt', 'http://example.org/path/')
// Returns: 'file.txt'

relativizeIri('http://example.org/path#section', 'http://example.org/path#')
// Returns: '#section'

relativizeIri('https://other.org/file', 'http://example.org/')
// Returns: 'https://other.org/file' (unchanged - different domains)

The function guarantees that resolveIri(relativizeIri(iri, base), base) will return the original iri.

Implementation

String relativizeIri(String iri, String? baseIri) {
  if (baseIri == null || baseIri.isEmpty) {
    return iri;
  }
  return _relativizeUri(iri, baseIri);
}