> ## Documentation Index
> Fetch the complete documentation index at: https://developer.kodexa.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# Document Structure

> Use kdx document print and select commands to inspect, navigate, and query the hierarchical structure of KDDB documents from the command line.

The `kdx document print` and `kdx document select` commands help you understand and navigate the hierarchical structure of KDDB documents.

## Print Command

Pretty print the document structure as an ASCII tree.

```bash theme={null}
kdx document print <file.kddb> [flags]
```

### Flags

| Flag         | Description                   | Default       |
| ------------ | ----------------------------- | ------------- |
| `--page N`   | Print only page N (1-indexed) | All pages     |
| `--depth N`  | Maximum tree depth            | 0 (unlimited) |
| `--features` | Show node features            | false         |

### Basic Usage

```bash theme={null}
kdx document print invoice.kddb
```

```text theme={null}
root
└── page
    └── content-area
        ├── line
        │   └── word: Invoice
        ├── line
        │   ├── word: Date:
        │   └── word: 2024-01-15
        ├── paragraph
        │   ├── line
        │   │   └── word: Item
        │   └── line
        │       └── word: Description
        └── line
            ├── word: Total:
            └── word: $1,234.56
```

### Limit Depth

Control how deep into the tree to display:

```bash theme={null}
# Show only top 2 levels
kdx document print invoice.kddb --depth 2
```

```text theme={null}
root
└── page
    └── content-area
```

### Print Specific Page

Focus on a single page:

```bash theme={null}
# Print page 1 only
kdx document print invoice.kddb --page 1

# Print page 3 with limited depth
kdx document print invoice.kddb --page 3 --depth 4
```

### Show Features

Display node features like bounding boxes and tags:

```bash theme={null}
kdx document print invoice.kddb --features --depth 2
```

```text theme={null}
root
└── page
    [spatial:bbox=0,0,612,792]
    └── content-area
        [spatial:bbox=50,50,562,742]
```

## Select Command

Query nodes by type using selector syntax.

```bash theme={null}
kdx document select <file.kddb> <selector>
```

### Selector Syntax

Currently supports type-based selectors:

| Selector | Description                        |
| -------- | ---------------------------------- |
| `//type` | Select all nodes of the given type |

### Examples

```bash theme={null}
# Find all paragraphs
kdx document select doc.kddb "//paragraph"
```

```text theme={null}
┌────┬───────────┬──────────────────────────────────────┬───────┐
│ ID │   TYPE    │               CONTENT                │ INDEX │
├────┼───────────┼──────────────────────────────────────┼───────┤
│ 15 │ paragraph │ This is the first paragraph of text. │ 0     │
│ 28 │ paragraph │ Another important paragraph here.    │ 1     │
│ 45 │ paragraph │ The final paragraph with conclusion. │ 2     │
└────┴───────────┴──────────────────────────────────────┴───────┘
```

```bash theme={null}
# Find all pages
kdx document select doc.kddb "//page"

# Find all lines
kdx document select doc.kddb "//line"

# Find all words
kdx document select doc.kddb "//word"

# Find all tables
kdx document select doc.kddb "//table"
```

### Output Columns

| Column    | Description                               |
| --------- | ----------------------------------------- |
| `ID`      | Internal node ID                          |
| `TYPE`    | Node type name                            |
| `CONTENT` | Node text content (truncated to 80 chars) |
| `INDEX`   | Position among siblings                   |

## Output Formats

Both commands support the global `-o` flag:

```bash theme={null}
# JSON output
kdx document select invoice.kddb "//paragraph" -o json

# YAML output
kdx document print invoice.kddb -o yaml
```

**JSON select output:**

```json theme={null}
[
  {
    "id": 15,
    "type": "paragraph",
    "content": "This is the first paragraph of text.",
    "index": 0
  },
  {
    "id": 28,
    "type": "paragraph",
    "content": "Another important paragraph here.",
    "index": 1
  }
]
```

## Common Node Types

Documents typically contain these node types:

| Type           | Description              |
| -------------- | ------------------------ |
| `root`         | Document root node       |
| `page`         | Page container           |
| `content-area` | Content region on a page |
| `paragraph`    | Paragraph of text        |
| `line`         | Line of text             |
| `word`         | Individual word          |
| `table`        | Table structure          |
| `table-row`    | Table row                |
| `table-cell`   | Table cell               |
| `image`        | Image placeholder        |

## Use Cases

### Document Analysis

Understand document structure before processing:

```bash theme={null}
# Quick overview
kdx document print invoice.kddb --depth 3

# Count paragraphs
kdx document select invoice.kddb "//paragraph" -o json | jq 'length'

# List all page numbers
kdx document select invoice.kddb "//page" -o json | jq '.[].index'
```

### Debugging Extraction

Investigate why extraction isn't finding expected content:

```bash theme={null}
# Check if lines exist
kdx document select problematic.kddb "//line"

# View full structure with features
kdx document print problematic.kddb --features

# Check specific page
kdx document print problematic.kddb --page 2 --features
```

### Content Location

Find where specific content appears:

```bash theme={null}
# Find all words (useful for searching)
kdx document select doc.kddb "//word" -o json | jq '.[] | select(.content | contains("Invoice"))'
```

### Structure Validation

Verify document structure is as expected:

```bash theme={null}
# Check page count
PAGE_COUNT=$(kdx document select doc.kddb "//page" -o json | jq 'length')
echo "Document has $PAGE_COUNT pages"

# Verify table structure exists
TABLES=$(kdx document select doc.kddb "//table" -o json | jq 'length')
if [ "$TABLES" -gt 0 ]; then
  echo "Found $TABLES tables"
fi
```

### Batch Analysis

Analyze structure across multiple documents:

```bash theme={null}
# Report structure summary for all documents
for kddb in *.kddb; do
  PAGES=$(kdx document select "$kddb" "//page" -o json 2>/dev/null | jq 'length')
  PARAGRAPHS=$(kdx document select "$kddb" "//paragraph" -o json 2>/dev/null | jq 'length')
  echo "$kddb: $PAGES pages, $PARAGRAPHS paragraphs"
done
```

## Tips

<Tip>
  Use `--depth 2` or `--depth 3` for a quick overview without overwhelming output on large documents.
</Tip>

<Tip>
  Pipe JSON output to `jq` for powerful filtering and transformation of results.
</Tip>

<Note>
  Content shown in the tree view is truncated to 60 characters. Use `select` with JSON output for full content.
</Note>

## Related

<CardGroup cols={2}>
  <Card title="Query Commands" icon="magnifying-glass" href="/guides/kdx-cli/document/query">
    Search and retrieve with grep and lines
  </Card>

  <Card title="SDK: Content Nodes" icon="code" href="/sdk/content-nodes">
    Work with content nodes programmatically
  </Card>

  <Card title="SDK: Selectors" icon="crosshairs" href="/sdk/selectors">
    Advanced selector syntax
  </Card>

  <Card title="Document Overview" icon="file-lines" href="/guides/kdx-cli/document/overview">
    Back to document commands overview
  </Card>
</CardGroup>
