or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

Files

docs

dataset-named-graphs.mdgraph-operations.mdindex.mdnamespace-management.mdparsers-serializers.mdrdf-containers-collections.mdrdf-terms.mdsparql-queries-updates.mdutilities-helpers.md

namespace-management.mddocs/

0

# Namespace Management

1

2

Tools for managing URI namespaces and prefixes. Provides predefined vocabularies and utilities for creating custom namespaces, enabling clean and readable RDF serialization.

3

4

## Capabilities

5

6

### Namespace - Custom Namespace Creation

7

8

Create custom namespaces for URI construction with attribute or dictionary access.

9

10

```python { .api }

11

class Namespace:

12

def __init__(self, value: str):

13

"""

14

Create a namespace.

15

16

Parameters:

17

- value: Base URI for the namespace

18

"""

19

20

def __getattr__(self, name: str) -> URIRef:

21

"""

22

Get URI by attribute access.

23

24

Parameters:

25

- name: Local name to append

26

27

Returns:

28

URIRef: Combined URI

29

"""

30

31

def __getitem__(self, key: str) -> URIRef:

32

"""

33

Get URI by dictionary access.

34

35

Parameters:

36

- key: Local name to append

37

38

Returns:

39

URIRef: Combined URI

40

"""

41

42

def term(self, name: str) -> URIRef:

43

"""

44

Create a term in this namespace.

45

46

Parameters:

47

- name: Local name

48

49

Returns:

50

URIRef: Namespaced URI

51

"""

52

```

53

54

### NamespaceManager - Namespace Binding Management

55

56

Manages namespace prefix bindings for graphs.

57

58

```python { .api }

59

class NamespaceManager:

60

def __init__(self, graph: Graph):

61

"""

62

Create a namespace manager.

63

64

Parameters:

65

- graph: Graph to manage namespaces for

66

"""

67

68

def bind(self, prefix: str, namespace: Namespace, override: bool = True, replace: bool = False):

69

"""

70

Bind a prefix to a namespace.

71

72

Parameters:

73

- prefix: Prefix string (without colon)

74

- namespace: Namespace URI or Namespace object

75

- override: Whether to override existing binding

76

- replace: Whether to replace existing namespace

77

"""

78

79

def compute_qname(self, uri: URIRef, generate: bool = True) -> Tuple[str, URIRef, str]:

80

"""

81

Compute prefix:localname for a URI.

82

83

Parameters:

84

- uri: URI to compute qname for

85

- generate: Whether to generate prefix if none exists

86

87

Returns:

88

Tuple: (prefix, namespace, localname)

89

"""

90

91

def compute_qname_strict(self, uri: URIRef, generate: bool = True) -> Tuple[str, URIRef, str]:

92

"""

93

Compute qname with strict validation.

94

95

Parameters:

96

- uri: URI to compute qname for

97

- generate: Whether to generate prefix if none exists

98

99

Returns:

100

Tuple: (prefix, namespace, localname)

101

"""

102

103

def normalizeUri(self, rdfTerm: URIRef) -> str:

104

"""

105

Get normalized (prefixed) representation of URI.

106

107

Parameters:

108

- rdfTerm: URI to normalize

109

110

Returns:

111

str: Normalized URI string

112

"""

113

114

def expand_curie(self, curie: str) -> URIRef:

115

"""

116

Expand a CURIE to full URI.

117

118

Parameters:

119

- curie: Compact URI (prefix:localname)

120

121

Returns:

122

URIRef: Full URI

123

"""

124

125

def qname(self, uri: URIRef) -> str:

126

"""

127

Get qualified name for URI.

128

129

Parameters:

130

- uri: URI to get qname for

131

132

Returns:

133

str: Qualified name (prefix:localname)

134

"""

135

136

def namespaces(self) -> Iterator[Tuple[str, URIRef]]:

137

"""

138

Get all namespace bindings.

139

140

Returns:

141

Iterator: (prefix, namespace) pairs

142

"""

143

144

def reset(self):

145

"""Reset to default namespace bindings."""

146

147

def __getitem__(self, prefix: str) -> Namespace:

148

"""

149

Get namespace by prefix.

150

151

Parameters:

152

- prefix: Prefix string

153

154

Returns:

155

Namespace: Namespace object

156

"""

157

```

158

159

## Predefined Namespaces

160

161

RDFLib provides many predefined namespace objects for common vocabularies:

162

163

```python { .api }

164

# Core RDF/RDFS/OWL

165

RDF: Namespace # http://www.w3.org/1999/02/22-rdf-syntax-ns#

166

RDFS: Namespace # http://www.w3.org/2000/01/rdf-schema#

167

OWL: Namespace # http://www.w3.org/2002/07/owl#

168

169

# XML Schema Datatypes

170

XSD: Namespace # http://www.w3.org/2001/XMLSchema#

171

172

# Social Web

173

FOAF: Namespace # http://xmlns.com/foaf/0.1/

174

DOAP: Namespace # http://usefulinc.com/ns/doap#

175

176

# Dublin Core

177

DC: Namespace # http://purl.org/dc/elements/1.1/

178

DCTERMS: Namespace # http://purl.org/dc/terms/

179

DCMITYPE: Namespace # http://purl.org/dc/dcmitype/

180

181

# Knowledge Organization

182

SKOS: Namespace # http://www.w3.org/2004/02/skos/core#

183

184

# Provenance

185

PROV: Namespace # http://www.w3.org/ns/prov#

186

187

# Dataset/Catalog

188

DCAT: Namespace # http://www.w3.org/ns/dcat#

189

VOID: Namespace # http://rdfs.org/ns/void#

190

191

# Organizations

192

ORG: Namespace # http://www.w3.org/ns/org#

193

194

# Time

195

TIME: Namespace # http://www.w3.org/2006/time#

196

197

# Geography

198

GEO: Namespace # http://www.w3.org/2003/01/geo/wgs84_pos#

199

200

# Schema.org

201

SDO: Namespace # http://schema.org/

202

203

# SHACL

204

SH: Namespace # http://www.w3.org/ns/shacl#

205

206

# Sensors

207

SOSA: Namespace # http://www.w3.org/ns/sosa/

208

SSN: Namespace # http://www.w3.org/ns/ssn/

209

210

# Data Cube

211

QB: Namespace # http://purl.org/linked-data/cube#

212

213

# Profiles

214

PROF: Namespace # http://www.w3.org/ns/dx/prof/

215

216

# Digital Rights

217

ODRL2: Namespace # http://www.w3.org/ns/odrl/2/

218

219

# Building/IoT

220

BRICK: Namespace # https://brickschema.org/schema/Brick#

221

222

# CSV on Web

223

CSVW: Namespace # http://www.w3.org/ns/csvw#

224

225

# Vocabulary Annotation

226

VANN: Namespace # http://purl.org/vocab/vann/

227

228

# XML

229

XMLNS: Namespace # http://www.w3.org/2000/xmlns/

230

```

231

232

## Usage Examples

233

234

### Creating Custom Namespaces

235

236

```python

237

from rdflib import Namespace, URIRef

238

239

# Create custom namespace

240

EX = Namespace("http://example.org/")

241

COMPANY = Namespace("http://company.example.com/vocab#")

242

243

# Use attribute access

244

person_uri = EX.person1 # Creates URIRef("http://example.org/person1")

245

employee_class = COMPANY.Employee # Creates URIRef("http://company.example.com/vocab#Employee")

246

247

# Use dictionary access

248

product_uri = EX['product-123'] # Handles special characters

249

```

250

251

### Using Predefined Namespaces

252

253

```python

254

from rdflib import Graph, Literal

255

from rdflib.namespace import RDF, RDFS, FOAF, XSD

256

257

g = Graph()

258

259

# Use predefined namespaces

260

person = URIRef("http://example.org/person/1")

261

g.add((person, RDF.type, FOAF.Person))

262

g.add((person, FOAF.name, Literal("John Doe")))

263

g.add((person, FOAF.age, Literal(30, datatype=XSD.integer)))

264

g.add((person, RDFS.label, Literal("A person named John")))

265

```

266

267

### Binding Namespaces to Graphs

268

269

```python

270

from rdflib import Graph, Namespace

271

from rdflib.namespace import FOAF

272

273

g = Graph()

274

275

# Create and bind custom namespace

276

EX = Namespace("http://example.org/")

277

g.bind("ex", EX)

278

g.bind("foaf", FOAF)

279

280

# Add data using bound namespaces

281

g.add((EX.person1, RDF.type, FOAF.Person))

282

g.add((EX.person1, FOAF.name, Literal("Alice")))

283

284

# Serialization will use prefixes

285

turtle_output = g.serialize(format="turtle")

286

print(turtle_output)

287

# Output will include:

288

# @prefix ex: <http://example.org/> .

289

# @prefix foaf: <http://xmlns.com/foaf/0.1/> .

290

```

291

292

### Working with NamespaceManager

293

294

```python

295

from rdflib import Graph, URIRef, Namespace

296

297

g = Graph()

298

nm = g.namespace_manager

299

300

# Bind namespaces

301

EX = Namespace("http://example.org/")

302

nm.bind("ex", EX)

303

304

# Compute qualified names

305

uri = URIRef("http://example.org/person1")

306

prefix, namespace, localname = nm.compute_qname(uri)

307

print(f"Prefix: {prefix}, Namespace: {namespace}, Local: {localname}")

308

309

# Get normalized representation

310

normalized = nm.normalizeUri(uri)

311

print(f"Normalized: {normalized}") # "ex:person1"

312

313

# Expand CURIE

314

expanded = nm.expand_curie("ex:person1")

315

print(f"Expanded: {expanded}") # URIRef("http://example.org/person1")

316

```

317

318

### Managing Multiple Namespaces

319

320

```python

321

from rdflib import Graph, Namespace, Literal

322

from rdflib.namespace import RDF, FOAF, DCTERMS

323

324

g = Graph()

325

326

# Multiple custom namespaces

327

COMPANY = Namespace("http://company.example.com/")

328

PROJECT = Namespace("http://projects.example.com/")

329

SKILL = Namespace("http://skills.example.com/")

330

331

# Bind all namespaces

332

g.bind("company", COMPANY)

333

g.bind("project", PROJECT)

334

g.bind("skill", SKILL)

335

g.bind("dcterms", DCTERMS)

336

337

# Use in RDF data

338

employee = COMPANY.employee123

339

g.add((employee, RDF.type, FOAF.Person))

340

g.add((employee, FOAF.name, Literal("Jane Smith")))

341

g.add((employee, COMPANY.worksOn, PROJECT.webapp_redesign))

342

g.add((employee, COMPANY.hasSkill, SKILL.python))

343

g.add((employee, DCTERMS.created, Literal("2023-01-15", datatype=XSD.date)))

344

345

# All namespaces will be properly prefixed in serialization

346

```

347

348

### Namespace Utilities

349

350

```python

351

from rdflib import Graph, Namespace

352

from rdflib.namespace import NamespaceManager

353

354

g = Graph()

355

nm = g.namespace_manager

356

357

# List all bound namespaces

358

for prefix, namespace in nm.namespaces():

359

print(f"{prefix}: {namespace}")

360

361

# Reset to defaults

362

nm.reset()

363

364

# Check available namespaces after reset

365

default_namespaces = list(nm.namespaces())

366

print(f"Default namespaces: {len(default_namespaces)}")

367

```

368

369

### Advanced Namespace Operations

370

371

```python

372

from rdflib import Graph, URIRef, Namespace

373

374

# Create graph with custom namespace handling

375

g = Graph()

376

377

# Create hierarchical namespaces

378

BASE = Namespace("http://example.org/")

379

PEOPLE = Namespace("http://example.org/people/")

380

ORGS = Namespace("http://example.org/organizations/")

381

382

g.bind("base", BASE)

383

g.bind("people", PEOPLE)

384

g.bind("orgs", ORGS)

385

386

# Use namespaces for different types

387

person_uri = PEOPLE.john_doe

388

org_uri = ORGS.tech_corp

389

concept_uri = BASE.employee_of

390

391

g.add((person_uri, concept_uri, org_uri))

392

393

# Compute qnames for different URIs

394

person_qname = g.namespace_manager.qname(person_uri)

395

org_qname = g.namespace_manager.qname(org_uri)

396

concept_qname = g.namespace_manager.qname(concept_uri)

397

398

print(f"Person: {person_qname}") # people:john_doe

399

print(f"Organization: {org_qname}") # orgs:tech_corp

400

print(f"Concept: {concept_qname}") # base:employee_of

401

```