Skip to content

# `` trix

Classes:

- `TriXSerializer` –

  TriX RDF graph serializer.

## `` 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:

- `serialize` –

Source code in `rdflib/plugins/serializers/trix.py`

    21
    22
    23
    24
    25
    26

    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`

    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56

    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"))