Creating a Document
You can create documents in several ways using the Kodexa Document SDK:Saving a Document
You can save a Kodexa Document to a file or a store. Documents are saved in the KDDB format (a SQLite database):.kddb extension for Kodexa Document Database files.
Loading a Document
To load a previously saved Kodexa Document:Detached Documents
Sometimes you may want to make changes to a document without affecting the original file. In Python, you can load the document in detached mode:Anatomy of a Kodexa Document
The Kodexa Document Model provides a flexible and powerful way to represent structured and unstructured documents. At its core, it consists of a Document object that contains metadata and a hierarchical tree of ContentNodes, each of which can have features and tags attached to them. Let’s explore the key components of the model.Core Components
Document Structure
A Kodexa Document consists of:- Document Metadata: Flexible dictionary-based metadata about the document
- Content Node Tree: Hierarchical structure of content nodes
- Source Metadata: Information about the document’s origin (filename, MIME type, checksum)
- Native Documents: Embedded binary files (original PDFs, images, etc.)
- Data Objects & Attributes: Structured extracted data organized by taxonomy
- Audit Trail: Change history and revision tracking
Content Nodes
ContentNodes are the building blocks of the document structure. Each ContentNode represents a logical section of content and has the following properties: Key attributes:- node_type: Identifies the type of node (e.g., ‘page’, ‘line’, ‘word’, ‘cell’)
- content: The actual text content of the node
- features: List of attached features (metadata)
- tags: Annotations linking content to extracted data
- children: Child nodes in the hierarchy
- id: Unique numeric identifier
- index: Position among siblings
- virtual: Whether the node is a virtual/synthesized node
Features
Features are flexible metadata containers attached to ContentNodes: Each feature has:- feature_type: Category of the feature (e.g., ‘tag’, ‘spatial’)
- name: Identifier for the feature
- value: The feature’s data (always stored as an array)
Working with Documents
Creating Document Structure
Working with Features
Working with Tags
Tags are annotations on nodes that link content to extracted data:Node Navigation and Selection
The document model provides powerful ways to navigate and select nodes:- Direct Navigation:
get_children()/getChildren(): Get immediate child nodesget_parent()/getParent(): Get parent nodenext_node()/nextNode(): Get next siblingprevious_node()/previousNode(): Get previous siblingget_child(index)/getChild(index): Get child by index
- Selector-based Navigation:
Data Objects and Attributes
Documents can contain structured extracted data organized by taxonomy:Document Metadata
Working with Document Content
Kodexa uses a powerful selector syntax to find and manipulate content within documents. Selectors work similarly to CSS selectors or XPath, allowing you to build queries that can be executed on a document instance.Basic Selector Example
To find all content nodes matching a regex:Selector Syntax
The selector syntax is composed of several parts:- Axis & Node Type: Defines how to navigate the tree structure.
- Predicate: Further filters the selected nodes based on conditions.
Axis Examples
//: Current node and all children/: Root node.: Current Node (or root if from the document)./line/.: All nodes of type line under the current nodeparent::line: Any node in the parent structure of this node that is of node type line
Predicate Functions
Predicates can use various functions, such as:contentRegex: Matches content against a regular expressiontypeRegex: Matches node type name against a regular expressionhasTag: Checks if a node has a specific taghasFeature: Checks if a node has a specific featurecontent: Returns the content of the nodeuuid: Returns the UUID of the node
Operators
Operators can be used to combine functions:|: Union the results of two sides=: Test that two sides are equaland: Boolean AND operationor: Boolean OR operation
Pipeline Selectors
Kodexa also supports “pipeline” selectors, allowing you to chain multiple selectors:Best Practices
- Node Types: Use consistent node types throughout your document to make selection and processing easier
- Features:
- Use features to add metadata rather than modifying node content
- Keep feature names consistent across your application
- Use appropriate feature types for different kinds of metadata
- Content Structure:
- Maintain a logical hierarchy that reflects the document’s structure
- Use indexes appropriately to maintain node order
- Consider using virtual nodes for sparse content
- Performance:
- Use selectors efficiently
- Batch operations when possible
- Use KDDB format for large documents
- Resource Management:
- Always close documents when done (Python:
doc.close(), TypeScript:await doc.dispose()) - Use context managers in Python:
with Document() as doc:
- Always close documents when done (Python:
