Skip to content

# `` rdfxml

An RDF/XML parser for RDFLib

Classes:

- `BagID` –
- `ElementHandler` –
- `RDFXMLHandler` –
- `RDFXMLParser` –

  An RDF/XML parser.

Functions:

- `create_parser` –

## `` BASE `module-attribute`

    BASE = (XMLNS, 'base')

## `` CORE_SYNTAX_TERMS `module-attribute`

    CORE_SYNTAX_TERMS = [RDF, ID, about, parseType, resource, nodeID, datatype]

## `` LANG `module-attribute`

    LANG = (XMLNS, 'lang')

## `` NODE_ELEMENT_ATTRIBUTES `module-attribute`

    NODE_ELEMENT_ATTRIBUTES = [ID, nodeID, about]

## `` NODE_ELEMENT_EXCEPTIONS `module-attribute`

    NODE_ELEMENT_EXCEPTIONS = CORE_SYNTAX_TERMS + [li] + OLD_TERMS

## `` OLD_TERMS `module-attribute`

    OLD_TERMS = [URIRef('http://www.w3.org/1999/02/22-rdf-syntax-ns#aboutEach'), URIRef('http://www.w3.org/1999/02/22-rdf-syntax-ns#aboutEachPrefix'), URIRef('http://www.w3.org/1999/02/22-rdf-syntax-ns#bagID')]

## `` PROPERTY_ATTRIBUTE_EXCEPTIONS `module-attribute`

    PROPERTY_ATTRIBUTE_EXCEPTIONS = CORE_SYNTAX_TERMS + [Description, li] + OLD_TERMS

## `` PROPERTY_ELEMENT_ATTRIBUTES `module-attribute`

    PROPERTY_ELEMENT_ATTRIBUTES = [ID, resource, nodeID]

## `` PROPERTY_ELEMENT_EXCEPTIONS `module-attribute`

    PROPERTY_ELEMENT_EXCEPTIONS = CORE_SYNTAX_TERMS + [Description] + OLD_TERMS

## `` RDFNS `module-attribute`

    RDFNS = RDFVOC

## `` SYNTAX_TERMS `module-attribute`

    SYNTAX_TERMS = CORE_SYNTAX_TERMS + [Description, li]

## `` UNQUALIFIED `module-attribute`

    UNQUALIFIED = {'about': about, 'ID': ID, 'type': type, 'resource': resource, 'parseType': parseType}

## `` XMLNS `module-attribute`

    XMLNS = 'http://www.w3.org/XML/1998/namespace'

## `` __all__ `module-attribute`

    __all__ = ['create_parser', 'BagID', 'ElementHandler', 'RDFXMLHandler', 'RDFXMLParser']

## `` BagID

    BagID(val)

Bases: `URIRef`

Methods:

- `next_li` –

Attributes:

- `__slots__` –
- `li` –

Source code in `rdflib/plugins/parsers/rdfxml.py`

    90
    91
    92
    93

    def __init__(self, val):
        # type error: Too many arguments for "__init__" of "object"
        super(URIRef, self).__init__(val)  # type: ignore[call-arg]
        self.li = 0

### `` __slots__ `class-attribute` `instance-attribute`

    __slots__ = ['li']

### `` li `instance-attribute`

    li = 0

### `` next_li

    next_li()

Source code in `rdflib/plugins/parsers/rdfxml.py`

    95
    96
    97
    98

    def next_li(self):
        self.li += 1
        # type error: Type expected within [...]
        return RDFNS["_%s" % self.li]  # type: ignore[misc]

## `` ElementHandler

    ElementHandler()

Methods:

- `next_li` –

Attributes:

- `__slots__` –
- `base` –
- `char` –
- `data` –
- `datatype` –
- `declared` –
- `end` –
- `id` –
- `language` –
- `li` –
- `list` –
- `object` –
- `start` –
- `subject` –

Source code in `rdflib/plugins/parsers/rdfxml.py`

    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132

    def __init__(self):
        self.start = None
        self.char = None
        self.end = None
        self.li = 0
        self.id = None
        self.base = None
        self.subject = None
        self.object = None
        self.list = None
        self.language = None
        self.datatype = None
        self.declared = None
        self.data = None

### `` __slots__ `class-attribute` `instance-attribute`

    __slots__ = ['start', 'char', 'end', 'li', 'id', 'base', 'subject', 'predicate', 'object', 'list', 'language', 'datatype', 'declared', 'data']

### `` base `instance-attribute`

    base = None

### `` char `instance-attribute`

    char = None

### `` data `instance-attribute`

    data = None

### `` datatype `instance-attribute`

    datatype = None

### `` declared `instance-attribute`

    declared = None

### `` end `instance-attribute`

    end = None

### `` id `instance-attribute`

    id = None

### `` language `instance-attribute`

    language = None

### `` li `instance-attribute`

    li = 0

### `` list `instance-attribute`

    list = None

### `` object `instance-attribute`

    object = None

### `` start `instance-attribute`

    start = None

### `` subject `instance-attribute`

    subject = None

### `` next_li

    next_li()

Source code in `rdflib/plugins/parsers/rdfxml.py`

    134
    135
    136

    def next_li(self):
        self.li += 1
        return RDFVOC["_%s" % self.li]

## `` RDFXMLHandler

    RDFXMLHandler(store: Graph)

Bases: `ContentHandler`

Methods:

- `absolutize` –
- `add_reified` –
- `characters` –
- `convert` –
- `document_element_start` –
- `endElementNS` –
- `endPrefixMapping` –
- `error` –
- `get_current` –
- `get_next` –
- `get_parent` –
- `ignorableWhitespace` –
- `list_node_element_end` –
- `literal_element_char` –
- `literal_element_end` –
- `literal_element_start` –
- `node_element_end` –
- `node_element_start` –
- `processingInstruction` –
- `property_element_char` –
- `property_element_end` –
- `property_element_start` –
- `reset` –
- `setDocumentLocator` –
- `startDocument` –
- `startElementNS` –
- `startPrefixMapping` –

Attributes:

- `current` –
- `next` –
- `parent` –
- `preserve_bnode_ids` –
- `store` –

Source code in `rdflib/plugins/parsers/rdfxml.py`

    140
    141
    142
    143

    def __init__(self, store: Graph):
        self.store = store
        self.preserve_bnode_ids = False
        self.reset()

### `` current `class-attribute` `instance-attribute`

    current = property(get_current)

### `` next `class-attribute` `instance-attribute`

    next = property(get_next)

### `` parent `class-attribute` `instance-attribute`

    parent = property(get_parent)

### `` preserve_bnode_ids `instance-attribute`

    preserve_bnode_ids = False

### `` store `instance-attribute`

    store = store

### `` absolutize

    absolutize(uri: str) -> URIRef

Source code in `rdflib/plugins/parsers/rdfxml.py`

    262
    263
    264
    265
    266
    267

    def absolutize(self, uri: str) -> URIRef:
        # type error: Argument "allow_fragments" to "urljoin" has incompatible type "int"; expected "bool"
        result = urljoin(self.current.base, uri, allow_fragments=1)  # type: ignore[arg-type]
        if uri and uri[-1] == "#" and result[-1] != "#":
            result = "%s#" % result
        return URIRef(result)

### `` add_reified

    add_reified(sid: Identifier, spo: _TripleType)

Source code in `rdflib/plugins/parsers/rdfxml.py`

    225
    226
    227
    228
    229
    230

    def add_reified(self, sid: Identifier, spo: _TripleType):
        s, p, o = spo
        self.store.add((sid, RDF.type, RDF.Statement))
        self.store.add((sid, RDF.subject, s))
        self.store.add((sid, RDF.predicate, p))
        self.store.add((sid, RDF.object, o))

### `` characters

    characters(content: str) -> None

Source code in `rdflib/plugins/parsers/rdfxml.py`

    214
    215
    216
    217

    def characters(self, content: str) -> None:
        char = self.current.char
        if char:
            char(content)

### `` convert

    convert(name: Tuple[Optional[str], str], qname, attrs: AttributesImpl) -> Tuple[URIRef, Dict[URIRef, str]]

Source code in `rdflib/plugins/parsers/rdfxml.py`

    269
    270
    271
    272
    273
    274
    275
    276
    277
    278
    279
    280
    281
    282
    283
    284
    285
    286
    287
    288
    289
    290
    291
    292
    293
    294
    295

    def convert(
        self, name: Tuple[Optional[str], str], qname, attrs: AttributesImpl
    ) -> Tuple[URIRef, Dict[URIRef, str]]:
        if name[0] is None:
            # type error: Incompatible types in assignment (expression has type "URIRef", variable has type "Tuple[Optional[str], str]")
            name = URIRef(name[1])  # type: ignore[assignment]
        else:
            # type error: Incompatible types in assignment (expression has type "URIRef", variable has type "Tuple[Optional[str], str]")
            # type error: Argument 1 to "join" of "str" has incompatible type "Tuple[Optional[str], str]"; expected "Iterable[str]"
            name = URIRef("".join(name))  # type: ignore[assignment, arg-type]
        atts = {}
        for n, v in attrs.items():
            # mypy error: mypy thinks n[0]==None is unreachable
            if n[0] is None:
                att = n[1]  # type: ignore[unreachable, unused-ignore]
            else:
                att = "".join(n)
            if att.startswith(XMLNS) or att[0:3].lower() == "xml":
                pass
            elif att in UNQUALIFIED:
                # if not RDFNS[att] in atts:
                # type error: Variable "att" is not valid as a type
                atts[RDFNS[att]] = v  # type: ignore[misc, valid-type]
            else:
                atts[URIRef(att)] = v
        # type error: Incompatible return value type (got "Tuple[Tuple[Optional[str], str], Dict[Any, Any]]", expected "Tuple[URIRef, Dict[URIRef, str]]")
        return name, atts  # type: ignore[return-value]

### `` document_element_start

    document_element_start(name: Tuple[str, str], qname, attrs: AttributesImpl) -> None

Source code in `rdflib/plugins/parsers/rdfxml.py`

    297
    298
    299
    300
    301
    302
    303
    304
    305
    306

    def document_element_start(
        self, name: Tuple[str, str], qname, attrs: AttributesImpl
    ) -> None:
        if name[0] and URIRef("".join(name)) == RDFVOC.RDF:
            # Cheap hack so 2to3 doesn't turn it into __next__
            next = getattr(self, "next")
            next.start = self.node_element_start
            next.end = self.node_element_end
        else:
            self.node_element_start(name, qname, attrs)

### `` endElementNS

    endElementNS(name: Tuple[Optional[str], str], qname) -> None

Source code in `rdflib/plugins/parsers/rdfxml.py`

    210
    211
    212

    def endElementNS(self, name: Tuple[Optional[str], str], qname) -> None:
        self.current.end(name, qname)
        self.stack.pop()

### `` endPrefixMapping

    endPrefixMapping(prefix: Optional[str]) -> None

Source code in `rdflib/plugins/parsers/rdfxml.py`

    173
    174
    175

    def endPrefixMapping(self, prefix: Optional[str]) -> None:
        self._current_context = self._ns_contexts[-1]
        del self._ns_contexts[-1]

### `` error

    error(message: str) -> NoReturn

Source code in `rdflib/plugins/parsers/rdfxml.py`

    232
    233
    234
    235
    236
    237
    238
    239

    def error(self, message: str) -> NoReturn:
        locator = self.locator
        info = "%s:%s:%s: " % (
            locator.getSystemId(),
            locator.getLineNumber(),
            locator.getColumnNumber(),
        )
        raise ParserError(info + message)

### `` get_current

    get_current() -> Optional[ElementHandler]

Source code in `rdflib/plugins/parsers/rdfxml.py`

    241
    242

    def get_current(self) -> Optional[ElementHandler]:
        return self.stack[-2]

### `` get_next

    get_next() -> Optional[ElementHandler]

Source code in `rdflib/plugins/parsers/rdfxml.py`

    248
    249

    def get_next(self) -> Optional[ElementHandler]:
        return self.stack[-1]

### `` get_parent

    get_parent() -> Optional[ElementHandler]

Source code in `rdflib/plugins/parsers/rdfxml.py`

    255
    256

    def get_parent(self) -> Optional[ElementHandler]:
        return self.stack[-3]

### `` ignorableWhitespace

    ignorableWhitespace(content) -> None

Source code in `rdflib/plugins/parsers/rdfxml.py`

    219
    220

    def ignorableWhitespace(self, content) -> None:
        pass

### `` list_node_element_end

    list_node_element_end(name: Tuple[str, str], qname) -> None

Source code in `rdflib/plugins/parsers/rdfxml.py`

    555
    556
    557
    558
    559
    560
    561
    562
    563
    564
    565
    566
    567
    568
    569
    570
    571

    def list_node_element_end(self, name: Tuple[str, str], qname) -> None:
        current = self.current
        if self.parent.list == RDF.nil:
            list = BNode()
            # Removed between 20030123 and 20030905
            # self.store.add((list, RDF.type, LIST))
            self.parent.list = list
            self.store.add((self.parent.list, RDF.first, current.subject))
            self.parent.object = list
            self.parent.char = None
        else:
            list = BNode()
            # Removed between 20030123 and 20030905
            # self.store.add((list, RDF.type, LIST))
            self.store.add((self.parent.list, RDF.rest, list))
            self.store.add((list, RDF.first, current.subject))
            self.parent.list = list

### `` literal_element_char

    literal_element_char(data: str) -> None

Source code in `rdflib/plugins/parsers/rdfxml.py`

    607
    608

    def literal_element_char(self, data: str) -> None:
        self.current.object += escape(data)

### `` literal_element_end

    literal_element_end(name: Tuple[str, str], qname) -> None

Source code in `rdflib/plugins/parsers/rdfxml.py`

    610
    611
    612
    613
    614
    615
    616
    617
    618
    619

    def literal_element_end(self, name: Tuple[str, str], qname) -> None:
        if name[0]:
            prefix = self._current_context[name[0]]
            if prefix:
                end = "</%s:%s>" % (prefix, name[1])
            else:
                end = "</%s>" % name[1]
        else:
            end = "</%s>" % name[1]
        self.parent.object += self.current.object + end

### `` literal_element_start

    literal_element_start(name: Tuple[str, str], qname, attrs: AttributesImpl) -> None

Source code in `rdflib/plugins/parsers/rdfxml.py`

    573
    574
    575
    576
    577
    578
    579
    580
    581
    582
    583
    584
    585
    586
    587
    588
    589
    590
    591
    592
    593
    594
    595
    596
    597
    598
    599
    600
    601
    602
    603
    604
    605

    def literal_element_start(
        self, name: Tuple[str, str], qname, attrs: AttributesImpl
    ) -> None:
        current = self.current
        self.next.start = self.literal_element_start
        self.next.char = self.literal_element_char
        self.next.end = self.literal_element_end
        current.declared = self.parent.declared.copy()
        if name[0]:
            prefix = self._current_context[name[0]]
            if prefix:
                current.object = "<%s:%s" % (prefix, name[1])
            else:
                current.object = "<%s" % name[1]
            if not name[0] in current.declared:  # noqa: E713
                current.declared[name[0]] = prefix
                if prefix:
                    current.object += ' xmlns:%s="%s"' % (prefix, name[0])
                else:
                    current.object += ' xmlns="%s"' % name[0]
        else:
            current.object = "<%s" % name[1]
        # type error: Incompatible types in assignment (expression has type "str", variable has type "Tuple[str, str]")
        for name, value in attrs.items():  # type: ignore[assignment, unused-ignore]
            if name[0]:
                if not name[0] in current.declared:  # noqa: E713
                    current.declared[name[0]] = self._current_context[name[0]]
                name = current.declared[name[0]] + ":" + name[1]
            else:
                # type error: Incompatible types in assignment (expression has type "str", variable has type "Tuple[str, str]")
                name = name[1]  # type: ignore[assignment]
            current.object += " %s=%s" % (name, quoteattr(value))
        current.object += ">"

### `` node_element_end

    node_element_end(name: Tuple[str, str], qname) -> None

Source code in `rdflib/plugins/parsers/rdfxml.py`

    394
    395
    396
    397
    398
    399
    400
    401
    402
    403

    def node_element_end(self, name: Tuple[str, str], qname) -> None:
        # repeat node-elements are only allowed
        # at at top-level

        if self.parent.object and self.current != self.stack[2]:
            self.error(
                "Repeat node-elements inside property elements: %s" % "".join(name)
            )

        self.parent.object = self.current.subject

### `` node_element_start

    node_element_start(name: Tuple[str, str], qname, attrs: AttributesImpl) -> None

Source code in `rdflib/plugins/parsers/rdfxml.py`

    311
    312
    313
    314
    315
    316
    317
    318
    319
    320
    321
    322
    323
    324
    325
    326
    327
    328
    329
    330
    331
    332
    333
    334
    335
    336
    337
    338
    339
    340
    341
    342
    343
    344
    345
    346
    347
    348
    349
    350
    351
    352
    353
    354
    355
    356
    357
    358
    359
    360
    361
    362
    363
    364
    365
    366
    367
    368
    369
    370
    371
    372
    373
    374
    375
    376
    377
    378
    379
    380
    381
    382
    383
    384
    385
    386
    387
    388
    389
    390
    391
    392

    def node_element_start(
        self, name: Tuple[str, str], qname, attrs: AttributesImpl
    ) -> None:
        # type error: Incompatible types in assignment (expression has type "URIRef", variable has type "Tuple[str, str]")
        name, atts = self.convert(name, qname, attrs)  # type: ignore[assignment]
        current = self.current
        absolutize = self.absolutize

        # Cheap hack so 2to3 doesn't turn it into __next__
        next = getattr(self, "next")
        next.start = self.property_element_start
        next.end = self.property_element_end

        if name in NODE_ELEMENT_EXCEPTIONS:
            # type error: Not all arguments converted during string formatting
            self.error("Invalid node element URI: %s" % name)  # type: ignore[str-format]
        subject: _SubjectType
        if RDFVOC.ID in atts:
            if RDFVOC.about in atts or RDFVOC.nodeID in atts:
                self.error("Can have at most one of rdf:ID, rdf:about, and rdf:nodeID")

            id = atts[RDFVOC.ID]
            if not is_ncname(id):
                self.error("rdf:ID value is not a valid NCName: %s" % id)
            subject = absolutize("#%s" % id)
            if subject in self.ids:
                self.error("two elements cannot use the same ID: '%s'" % subject)
            self.ids[subject] = 1  # IDs can only appear once within a document
        elif RDFVOC.nodeID in atts:
            if RDFVOC.ID in atts or RDFVOC.about in atts:
                self.error("Can have at most one of rdf:ID, rdf:about, and rdf:nodeID")
            nodeID = atts[RDFVOC.nodeID]
            if not is_ncname(nodeID):
                self.error("rdf:nodeID value is not a valid NCName: %s" % nodeID)
            if self.preserve_bnode_ids is False:
                if nodeID in self.bnode:
                    subject = self.bnode[nodeID]
                else:
                    subject = BNode()
                    self.bnode[nodeID] = subject
            else:
                subject = BNode(nodeID)
        elif RDFVOC.about in atts:
            if RDFVOC.ID in atts or RDFVOC.nodeID in atts:
                self.error("Can have at most one of rdf:ID, rdf:about, and rdf:nodeID")
            subject = absolutize(atts[RDFVOC.about])
        else:
            subject = BNode()

        if name != RDFVOC.Description:  # S1
            # error: Argument 1 has incompatible type "Tuple[str, str]"; expected "str"
            self.store.add((subject, RDF.type, absolutize(name)))  # type: ignore[arg-type]

        object: _ObjectType
        language = current.language
        for att in atts:
            if not att.startswith(str(RDFNS)):
                predicate = absolutize(att)
                try:
                    object = Literal(atts[att], language)
                except Error as e:
                    # type error: Argument 1 to "error" of "RDFXMLHandler" has incompatible type "Optional[str]"; expected "str"
                    self.error(e.msg)  # type: ignore[arg-type]
            elif att == RDF.type:  # S2
                predicate = RDF.type
                object = absolutize(atts[RDF.type])
            elif att in NODE_ELEMENT_ATTRIBUTES:
                continue
            elif att in PROPERTY_ATTRIBUTE_EXCEPTIONS:  # S3
                self.error("Invalid property attribute URI: %s" % att)
                # type error: Statement is unreachable
                continue  # type: ignore[unreachable] # for when error does not throw an exception
            else:
                predicate = absolutize(att)
                try:
                    object = Literal(atts[att], language)
                except Error as e:
                    # type error: Argument 1 to "error" of "RDFXMLHandler" has incompatible type "Optional[str]"; expected "str"
                    self.error(e.msg)  # type: ignore[arg-type]
            self.store.add((subject, predicate, object))

        current.subject = subject

### `` processingInstruction

    processingInstruction(target, data) -> None

Source code in `rdflib/plugins/parsers/rdfxml.py`

    222
    223

    def processingInstruction(self, target, data) -> None:
        pass

### `` property_element_char

    property_element_char(data: str) -> None

Source code in `rdflib/plugins/parsers/rdfxml.py`

    531
    532
    533
    534

    def property_element_char(self, data: str) -> None:
        current = self.current
        if current.data is not None:
            current.data += data

### `` property_element_end

    property_element_end(name: Tuple[str, str], qname) -> None

Source code in `rdflib/plugins/parsers/rdfxml.py`

    536
    537
    538
    539
    540
    541
    542
    543
    544
    545
    546
    547
    548
    549
    550
    551
    552
    553

    def property_element_end(self, name: Tuple[str, str], qname) -> None:
        current = self.current
        if current.data is not None and current.object is None:
            literalLang = current.language
            if current.datatype is not None:
                literalLang = None
            current.object = Literal(current.data, literalLang, current.datatype)
            current.data = None
        if self.next.end == self.list_node_element_end:
            if current.object != RDF.nil:
                self.store.add((current.list, RDF.rest, RDF.nil))
        if current.object is not None:
            self.store.add((self.parent.subject, current.predicate, current.object))
            if current.id is not None:
                self.add_reified(
                    current.id, (self.parent.subject, current.predicate, current.object)
                )
        current.subject = None

### `` property_element_start

    property_element_start(name: Tuple[str, str], qname, attrs: AttributesImpl) -> None

Source code in `rdflib/plugins/parsers/rdfxml.py`

    405
    406
    407
    408
    409
    410
    411
    412
    413
    414
    415
    416
    417
    418
    419
    420
    421
    422
    423
    424
    425
    426
    427
    428
    429
    430
    431
    432
    433
    434
    435
    436
    437
    438
    439
    440
    441
    442
    443
    444
    445
    446
    447
    448
    449
    450
    451
    452
    453
    454
    455
    456
    457
    458
    459
    460
    461
    462
    463
    464
    465
    466
    467
    468
    469
    470
    471
    472
    473
    474
    475
    476
    477
    478
    479
    480
    481
    482
    483
    484
    485
    486
    487
    488
    489
    490
    491
    492
    493
    494
    495
    496
    497
    498
    499
    500
    501
    502
    503
    504
    505
    506
    507
    508
    509
    510
    511
    512
    513
    514
    515
    516
    517
    518
    519
    520
    521
    522
    523
    524
    525
    526
    527
    528
    529

    def property_element_start(
        self, name: Tuple[str, str], qname, attrs: AttributesImpl
    ) -> None:
        # type error: Incompatible types in assignment (expression has type "URIRef", variable has type "Tuple[str, str]")
        name, atts = self.convert(name, qname, attrs)  # type: ignore[assignment]
        current = self.current
        absolutize = self.absolutize

        # Cheap hack so 2to3 doesn't turn it into __next__
        next = getattr(self, "next")
        object: Optional[_ObjectType] = None
        current.data = None
        current.list = None

        # type error: "Tuple[str, str]" has no attribute "startswith"
        if not name.startswith(str(RDFNS)):  # type: ignore[attr-defined]
            # type error: Argument 1 has incompatible type "Tuple[str, str]"; expected "str"
            current.predicate = absolutize(name)  # type: ignore[arg-type]
        elif name == RDFVOC.li:
            current.predicate = current.next_li()
        elif name in PROPERTY_ELEMENT_EXCEPTIONS:
            # type error: Not all arguments converted during string formatting
            self.error("Invalid property element URI: %s" % name)  # type: ignore[str-format]
        else:
            # type error: Argument 1 has incompatible type "Tuple[str, str]"; expected "str"
            current.predicate = absolutize(name)  # type: ignore[arg-type]

        id = atts.get(RDFVOC.ID, None)
        if id is not None:
            if not is_ncname(id):
                self.error("rdf:ID value is not a value NCName: %s" % id)
            current.id = absolutize("#%s" % id)
        else:
            current.id = None

        resource = atts.get(RDFVOC.resource, None)
        nodeID = atts.get(RDFVOC.nodeID, None)
        parse_type = atts.get(RDFVOC.parseType, None)
        if resource is not None and nodeID is not None:
            self.error("Property element cannot have both rdf:nodeID and rdf:resource")
        if resource is not None:
            object = absolutize(resource)
            next.start = self.node_element_start
            next.end = self.node_element_end
        elif nodeID is not None:
            if not is_ncname(nodeID):
                self.error("rdf:nodeID value is not a valid NCName: %s" % nodeID)
            if self.preserve_bnode_ids is False:
                if nodeID in self.bnode:
                    object = self.bnode[nodeID]
                else:
                    subject = BNode()
                    self.bnode[nodeID] = subject
                    object = subject
            else:
                object = subject = BNode(nodeID)
            next.start = self.node_element_start
            next.end = self.node_element_end
        else:
            if parse_type is not None:
                for att in atts:
                    if att != RDFVOC.parseType and att != RDFVOC.ID:
                        self.error("Property attr '%s' now allowed here" % att)
                if parse_type == "Resource":
                    current.subject = object = BNode()
                    current.char = self.property_element_char
                    next.start = self.property_element_start
                    next.end = self.property_element_end
                elif parse_type == "Collection":
                    current.char = None
                    object = current.list = RDF.nil  # BNode()
                    # self.parent.subject
                    next.start = self.node_element_start
                    next.end = self.list_node_element_end
                else:  # if parse_type=="Literal":
                    # All other values are treated as Literal
                    # See: http://www.w3.org/TR/rdf-syntax-grammar/
                    # parseTypeOtherPropertyElt
                    object = Literal("", datatype=RDFVOC.XMLLiteral)
                    current.char = self.literal_element_char
                    current.declared = {XMLNS: "xml"}
                    next.start = self.literal_element_start
                    next.char = self.literal_element_char
                    next.end = self.literal_element_end
                current.object = object
                return
            else:
                object = None
                current.char = self.property_element_char
                next.start = self.node_element_start
                next.end = self.node_element_end

        datatype = current.datatype = atts.get(RDFVOC.datatype, None)
        language = current.language
        if datatype is not None:
            # TODO: check that there are no atts other than datatype and id
            datatype = absolutize(datatype)
        else:
            for att in atts:
                if not att.startswith(str(RDFNS)):
                    predicate = absolutize(att)
                elif att in PROPERTY_ELEMENT_ATTRIBUTES:
                    continue
                elif att in PROPERTY_ATTRIBUTE_EXCEPTIONS:
                    self.error("""Invalid property attribute URI: %s""" % att)
                else:
                    predicate = absolutize(att)
                o: _ObjectType
                if att == RDF.type:
                    o = URIRef(atts[att])
                else:
                    if datatype is not None:
                        # type error: Statement is unreachable
                        language = None  # type: ignore[unreachable]
                    o = Literal(atts[att], language, datatype)

                if object is None:
                    object = BNode()
                self.store.add((object, predicate, o))
        if object is None:
            current.data = ""
            current.object = None
        else:
            current.data = None
            current.object = object

### `` reset

    reset() -> None

Source code in `rdflib/plugins/parsers/rdfxml.py`

    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158

    def reset(self) -> None:
        document_element = ElementHandler()
        document_element.start = self.document_element_start
        document_element.end = lambda name, qname: None
        self.stack: List[Optional[ElementHandler]] = [
            None,
            document_element,
        ]
        self.ids: Dict[str, int] = {}  # remember IDs we have already seen
        self.bnode: Dict[str, Identifier] = {}
        self._ns_contexts: List[Dict[str, Optional[str]]] = [
            {}
        ]  # contains uri -> prefix dicts
        self._current_context: Dict[str, Optional[str]] = self._ns_contexts[-1]

### `` setDocumentLocator

    setDocumentLocator(locator: Locator)

Source code in `rdflib/plugins/parsers/rdfxml.py`

    162
    163

    def setDocumentLocator(self, locator: Locator):
        self.locator = locator

### `` startDocument

    startDocument() -> None

Source code in `rdflib/plugins/parsers/rdfxml.py`

    165
    166

    def startDocument(self) -> None:
        pass

### `` startElementNS

    startElementNS(name: Tuple[Optional[str], str], qname, attrs: AttributesImpl) -> None

Source code in `rdflib/plugins/parsers/rdfxml.py`

    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208

    def startElementNS(
        self, name: Tuple[Optional[str], str], qname, attrs: AttributesImpl
    ) -> None:
        stack = self.stack
        stack.append(ElementHandler())
        current = self.current
        parent = self.parent
        # type error: No overlaod for "get" of "AttributesImpl" mactches tuple (str, str)
        base = attrs.get(BASE, None)  # type: ignore[call-overload, unused-ignore]
        if base is not None:
            base, frag = urldefrag(base)
            if parent and parent.base:
                base = urljoin(parent.base, base)
            else:
                systemId = self.locator.getPublicId() or self.locator.getSystemId()
                if systemId:
                    base = urljoin(systemId, base)
        else:
            if parent:
                base = parent.base
            if base is None:
                systemId = self.locator.getPublicId() or self.locator.getSystemId()
                if systemId:
                    base, frag = urldefrag(systemId)
        current.base = base
        # type error: No overlaod for "get" of "AttributesImpl" mactches tuple (str, str)
        language = attrs.get(LANG, None)  # type: ignore[call-overload, unused-ignore]
        if language is None:
            if parent:
                language = parent.language
        current.language = language
        current.start(name, qname, attrs)

### `` startPrefixMapping

    startPrefixMapping(prefix: Optional[str], namespace: str) -> None

Source code in `rdflib/plugins/parsers/rdfxml.py`

    168
    169
    170
    171

    def startPrefixMapping(self, prefix: Optional[str], namespace: str) -> None:
        self._ns_contexts.append(self._current_context.copy())
        self._current_context[namespace] = prefix
        self.store.bind(prefix, namespace or "", override=False)

## `` RDFXMLParser

    RDFXMLParser()

Bases: `Parser`

An RDF/XML parser.

Methods:

- `parse` –

Source code in `rdflib/plugins/parsers/rdfxml.py`

    643
    644

    def __init__(self):
        pass

### `` parse

    parse(source: InputSource, sink: Graph, **args: Any) -> None

Source code in `rdflib/plugins/parsers/rdfxml.py`

    646
    647
    648
    649
    650
    651
    652
    653
    654
    655
    656

    def parse(self, source: InputSource, sink: Graph, **args: Any) -> None:
        self._parser = create_parser(source, sink)
        content_handler = self._parser.getContentHandler()
        preserve_bnode_ids = args.get("preserve_bnode_ids", None)
        if preserve_bnode_ids is not None:
            # type error: ContentHandler has no attribute "preserve_bnode_ids"
            content_handler.preserve_bnode_ids = preserve_bnode_ids  # type: ignore[attr-defined, unused-ignore]
        # # We're only using it once now
        # content_handler.reset()
        # self._parser.reset()
        self._parser.parse(source)

## `` create_parser

    create_parser(target: InputSource, store: Graph) -> XMLReader

Source code in `rdflib/plugins/parsers/rdfxml.py`

    622
    623
    624
    625
    626
    627
    628
    629
    630
    631
    632
    633
    634
    635
    636
    637

    def create_parser(target: InputSource, store: Graph) -> xmlreader.XMLReader:
        parser = make_parser()
        try:
            # Workaround for bug in expatreader.py. Needed when
            # expatreader is trying to guess a prefix.
            parser.start_namespace_decl("xml", "http://www.w3.org/XML/1998/namespace")  # type: ignore[attr-defined]
        except AttributeError:
            pass  # Not present in Jython (at least)
        parser.setFeature(handler.feature_namespaces, 1)
        rdfxml = RDFXMLHandler(store)
        # type error: Argument 1 to "setDocumentLocator" of "RDFXMLHandler" has incompatible type "InputSource"; expected "Locator"
        rdfxml.setDocumentLocator(target)  # type: ignore[arg-type]
        # rdfxml.setDocumentLocator(_Locator(self.url, self.parser))
        parser.setContentHandler(rdfxml)
        parser.setErrorHandler(ErrorHandler())
        return parser