DocLang Reference Toolkit

repository·main·Indexed 19 days ago

https://github.com/doclang-project/doclang

An AI-native markup format and toolkit for unstructured content like documents and images. DocLang maps content to LLM tokens while preserving structure, semantics, layout, and geometry. The toolkit provides a Python API and CLI for validating .dclg documents using XSD and Schematron rules, and packaging them into .dclx archives including pages and assets.

Tokens
27.1K
Snippets
68
Records
102
Agent score
65%

What's inside doclang

  1. What is DocLang?

    main

    DocLang is a universal, AI-native markup language designed to represent structured document content. Unlike traditional formats like HTML or LaTeX, DocLang is optimized for Large Language Model (LLM) and Vision-Language Model (VLM) workflows by focusing on:

    • Token Efficiency: Uses a controlled, minimalist vocabulary of tags and attributes to map naturally to LLM tokenizers.
    • Semantic & Geometric Preservation: Maintains document structure, semantics, and layout/geometric information (like bounding boxes) simultaneously.
    • Complex Component Support: Provides native primitives for tables (including spanned cells and headings), formulas, code, nested lists, and charts.
    • Lossless Round-tripping: Enables conversion between formats while preserving the original content's meaning and structure.

    It is specifically designed to bridge the gap between OCR-centric formats (like PageXML or ALTO XML) and generative AI requirements, prioritizing semantic context over mere geometric location.

  2. Handle multiple-choice and branched options in DocLang

    main

    When a form field presents multiple choices (e.g., 'Yes/No' with 'A/B' branches), use multiple <text> elements within a single <field_item>. Each <text> block can contain a <checkbox>, a <marker> (to identify the choice branch), and the choice's label text.

    This pattern is essential for capturing the relationship between a question and its specific possible answers.

    <field_item>
        <key>La cause du sinistre se situe-t-elle chez vous ?</key>
        <text><checkbox class="unselected"/><marker>A</marker>oui</text>
        <text><checkbox class="unselected"/><marker>A</marker>non</text>
        <text><checkbox class="unselected"/><marker>B</marker>oui</text>
        <text><checkbox class="unselected"/><marker>B</marker>non</text>
    </field_item>
  3. Use <head> and <page_break> for document structure

    main

    Used to include document-level metadata. It must be the first child of the <doclang> root element. It cannot contain raw text or primary semantic elements.

    <page_break/>

    An empty element used to indicate a page break in paginated documents. Any content split by a <page_break/> must be a valid DocLang document body. It must be a child of <doclang>.

    <doclang>
      <head>
        <!-- metadata here -->
      </head>
      <!-- first page content -->
      <page_break/>
      <!-- second page content -->
    </doclang>
  4. Understand DocLang validation (XSD vs Schematron)

    main

    DocLang uses two layers of validation:

    1. XSD Validation (doclang.xsd): Handles structural validation, including element hierarchy, data types, attributes, and element ordering.
    2. Schematron Validation (doclang.sch): Handles complex business rules that XSD cannot express. It uses XSLT 3.0 and XPath 3.1. The default backend transpiles these rules to XSLT 3.0 at validation time.
  5. Representing form elements and field regions

    main

    DocLang uses <field_region> and <field_item> to represent structured form data. A <field_region> can contain multiple <field_item> elements, each representing a specific data point with a <key>, a <value>, and an optional <hint>.

    In complex documents like forms, <field_region> elements can be mixed with other structural elements like <table>, <heading>, or <text> to maintain the document's logical flow.

    <field_region>
      <field_item>
        <key>Source</key>
        <value>www.pansi.org.uk and ... projections).</value>
      </field_item>
    </field_region>
  6. Distinguish between `<caption|description|summary>`

    main

    DocLang provides three ways to attach textual information to an element head, which serve different semantic purposes:

    ElementTypePurpose
    <caption|>Document ComponentAn actual part of the document (e.g., a visible caption under a chart) with its own location info.
    <description|>Meta-informationA derived textual account of what the component is or shows (e.g., an AI-inferred description of a picture).
    <summary|>Meta-informationA derived textual distillation of what the component conveys; not part of the original content.
  7. Adopt Governance Profiles (Minimal vs Full)

    main

    DocLang suggests two governance profiles to help producers implement consistent control surfaces:

    Use this for a standard baseline of compliance and access control:

    • Licensing & compliance: licenses, compliance_requirements
    • Classification & access: data_classification, access_policy (or access_control_level), retention_policy
    • PII: pii_status, pii_sensitivity_level
    • Extraction: extraction_permitted, pii_extraction_allowed
    • RAG: rag_permitted, rag_indexing_allowed, rag_pii_exposure_allowed
    • Training: training_permitted, training_pii_included

    Full Profile (Expanded Control Surface)

    Includes everything in the Minimal profile plus advanced controls for:

    • Purpose limitation and lawful basis
    • Component-level scope constraints
    • Transformation requirements (redaction/masking/pseudonymization)
    • Caching/index retention and auditability
    • Model scope restrictions and provenance requirements
  8. Use Component-level Metadata for non-content information

    main

    DocLang supports metadata elements to capture information that is not part of the primary document content. This includes:

    • Derivable information: Data derived from the document itself (e.g., a summary of a component) or from external knowledge sources.
    • Pipeline properties: Information reflecting the upstream processing pipeline (e.g., the VLM used to generate the document).

    To avoid naming collisions with the reserved metadata elements defined by the DocLang standard, all custom metadata elements SHOULD be properly namespaced.

  9. Configure Data Extraction, RAG, and Training controls in DocLang

    main

    DocLang allows for fine-grained control over how document data is handled by AI systems through specific XML-based control elements. These controls are grouped into three main categories:

    1. Data Extraction Controls: Define what can be extracted (e.g., extraction_scope), the purpose, granularity, and whether PII or sensitive data is allowed. It also manages audit requirements and human-in-the-loop constraints.
    2. RAG (Retrieval-Augmented Generation) and Retrieval Controls: Manage how documents are used in RAG pipelines, including indexing permissions, embedding scopes, chunking constraints, and caching policies.
    3. Document Training Controls: Govern whether a document can be used for model training, defining the scope, purpose, and model types, as well as provenance and audit retention requirements.

    Implementation Note: Implementations should define controlled vocabularies for enums such as extraction_scope, rag_embedding_scope, and training_model_type. When training_permitted is set to false, training-related fields should be omitted or set to none.

    <!-- Data extraction controls -->
    <extraction_permitted>true</extraction_permitted>
    <extraction_scope>tables_and_forms_only</extraction_scope>
    <extraction_purpose>case_metrics</extraction_purpose>
    <extraction_granularity>field_level</extraction_granularity>
    <pii_extraction_allowed>false</pii_extraction_allowed>
    <sensitive_data_extraction_allowed>false</sensitive_data_extraction_allowed>
    <extraction_transformation_required>redact</extraction_transformation_required>
    <extraction_output_constraints>internal_systems_only</extraction_output_constraints>
    <downstream_sharing_permitted>false</downstream_sharing_permitted>
    <extraction_audit_required>true</extraction_audit_required>
    <extraction_audit_retention unit="day">90</extraction_audit_retention>
    <human_in_the_loop_required>true</human_in_the_loop_required>
    
    <!-- RAG and retrieval controls -->
    <rag_permitted>true</rag_permitted>
    <rag_indexing_allowed>true</rag_indexing_allowed>
    <rag_embedding_scope>summaries_only</rag_embedding_scope>
    <rag_chunking_constraints>max_512_tokens</rag_chunking_constraints>
    <rag_query_restrictions>support_intent_only</rag_query_restrictions>
    <rag_output_attribution_required>true</rag_output_attribution_required>
    <rag_output_transformation_required>summarize_and_redact</rag_output_transformation_required>
    <rag_pii_exposure_allowed>false</rag_pii_exposure_allowed>
    <rag_sensitive_data_exposure_allowed>false</rag_sensitive_data_exposure_allowed>
    <rag_downstream_sharing_permitted>false</rag_downstream_sharing_permitted>
    <rag_caching_allowed>true</rag_caching_allowed>
    <rag_cache_retention unit="day">30</rag_cache_retention>
    <rag_audit_required>true</rag_audit_required>
    <rag_audit_retention unit="day">90</rag_audit_retention>
    <rag_model_scope>enterprise_internal_models</rag_model_scope>
    
    <!-- Document training controls -->
    <training_permitted>false</training_permitted>
    <training_scope>none</training_scope>
    <training_purpose>none</training_purpose>
    <training_model_type>none</training_model_type>
    <training_dataset_reuse_allowed>false</training_dataset_reuse_allowed>
    <training_derivative_sharing_permitted>false</training_derivative_sharing_permitted>
    <training_pii_included>false</training_pii_included>
    <training_sensitive_data_included>false</training_sensitive_data_included>
    <training_provenance_required>true</training_provenance_required>
    <training_audit_required>true</training_audit_required>
    <training_audit_retention unit="day">365</training_audit_retention>
    <model_output_usage_constraints>internal_only</model_output_usage_constraints>
    <right_to_be_forgotten_applicability>true</right_to_be_forgotten_applicability>
  10. Understand DocLang terminology and structure

    main

    DocLang is an XML-based format designed for AI document representation. It uses several key concepts:

    • document component: A cohesive part of a document (e.g., a table, list item, or bold text).
    • element: An XML element.
    • attribute: An XML attribute (referenced in prose using element@attribute notation, e.g., label@value).
    • tag: An XML tag (start-tag, end-tag, or empty-element tag).
    • block-level element: An element that starts on a new line and occupies the full width of its container (e.g., <p>).
    • inline element: An element used within a block element to shape in-line structure (e.g., <span>).

    Note that both block-level and inline elements may contain explicit new lines.

  11. Represent field regions and items in DocLang XML

    main

    DocLang uses <field_region> to group related form elements. Within a region, you can define individual data points using <field_item>.

    Common components of a <field_item> include:

    • <key>: The label or name of the field.
    • <value>: The data content (can include <marker> for indexing or <hint> for supplementary text).
    • <checkbox>: Represents a boolean or selection state, often with a class attribute (e.g., unselected).
    • <marker>: A positional or logical identifier (e.g., 'A', 'B', or numeric indices).
    • <text>: Used for complex items where a single key maps to multiple text/choice options (common in multiple-choice questions).
    • <checkbox class="..."/>: Used within <text> or <field_item> to denote selection UI.
    <field_region>
        <field_heading>M31</field_heading>
        <field_item>
            <marker>1</marker>
            <key>Tipo</key>
            <value></value>
        </field_item>
        <field_item>
            <marker>2</marker>
            <key>Codice Stato estero</key>
            <value></value>
        </field_item>
    </field_region>
  12. Understand DocLang versioning and compatibility

    main

    DocLang documents define their specification version using the version attribute on the root <doclang> element in MAJOR.MINOR format.

    Compatibility Rules (Semantic Versioning)

    DocLang follows Semantic Versioning principles for XSD schema validation:

    • Forward Compatibility: A document with version X.Y is compatible with an XSD schema of version X.Z (where $Y leq Z$). A document valid against X.Y will pass validation against X.Z.
    • Backward Incompatibility: A document with version X.Z is not guaranteed to be compatible with an XSD schema of version X.Y (where $Z > Y$).

    Version 0.x (Initial Development)

    In the 0.x series, every minor version increment is a breaking change.

    • A 0.1 document is incompatible with a 0.2 schema.
    • A 0.2 document is incompatible with a 0.1 schema.