Skip to content

trix

Classes:

TRIXNS module-attribute

TRIXNS = Namespace('http://www.w3.org/2004/03/trix/trix-1/')

XMLNS module-attribute

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

__all__ module-attribute

__all__ = ['TriXSerializer']

TriXSerializer

TriXSerializer(store: Graph)

Bases: Serializer

TriX RDF graph serializer.

Methods:

Source code in rdflib/plugins/serializers/trix.py
def __init__(self, store: Graph):
    super(TriXSerializer, self).__init__(store)
    if not store.context_aware:
        raise Exception(
            "TriX serialization only makes sense for context-aware stores"
        )

serialize

serialize(stream: IO[bytes], base: Optional[str] = None, encoding: Optional[str] = None, **kwargs: Any) -> None
Source code in rdflib/plugins/serializers/trix.py
def serialize(
    self,
    stream: IO[bytes],
    base: Optional[str] = None,
    encoding: Optional[str] = None,
    **kwargs: Any,
) -> None:
    nm = self.store.namespace_manager

    self.writer = XMLWriter(stream, nm, encoding, extra_ns={"": TRIXNS})

    self.writer.push(TRIXNS["TriX"])
    # if base is given here, use that, if not and a base is set for the graph use that
    if base is None and self.store.base is not None:
        base = self.store.base
    if base is not None:
        self.writer.attribute("http://www.w3.org/XML/1998/namespacebase", base)
    self.writer.namespaces()

    if isinstance(self.store, ConjunctiveGraph):
        for subgraph in self.store.contexts():
            self._writeGraph(subgraph)
    elif isinstance(self.store, Graph):
        self._writeGraph(self.store)
    else:
        raise Exception(f"Unknown graph type: {type(self.store)}")

    self.writer.pop()
    stream.write("\n".encode("latin-1"))