# How to Use CSS-Like Selectors and Boolean Operators in the OfficeCLI Query Command

> Master the OfficeCLI query command using CSS-like selectors and boolean operators. Target elements in Word, Excel, and PowerPoint documents efficiently with implicit AND, OR unions, and content pseudo-selectors.

- Repository: [OfficeAI/OfficeCLI](https://github.com/iofficeai/OfficeCLI)
- Tags: how-to-guide
- Published: 2026-07-20

---

**The `officecli query` command implements a CSS-inspired selector engine that supports implicit AND logic between attribute predicates, comma-separated OR unions, and content-based pseudo-selectors like `:contains` and `:empty` to target elements in Word, Excel, and PowerPoint documents.**

The **iOfficeAI/OfficeCLI** repository provides a command-line interface for programmatically searching Office documents. The `query` command uses a **CSS-like selector syntax** to filter paragraphs, table rows, shapes, and slides, allowing complex boolean logic to pinpoint exactly the data you need without manual parsing.

## Architecture of the Selector Engine

The query system is distributed across three architectural layers in the source code. In [`src/officecli/CommandBuilder.GetQuery.cs`](https://github.com/iOfficeAI/OfficeCLI/blob/main/src/officecli/CommandBuilder.GetQuery.cs), the CLI entry point receives the raw selector string and dispatches it to the appropriate document handler based on file extension. The [`src/officecli/Core/AttributeFilter.cs`](https://github.com/iOfficeAI/OfficeCLI/blob/main/src/officecli/Core/AttributeFilter.cs) file contains the core parsing logic, which tokenizes selectors into element types, attribute predicates, and pseudo-selectors while handling boolean composition. Finally, format-specific handlers—[`WordHandler.Query.cs`](https://github.com/iOfficeAI/OfficeCLI/blob/main/WordHandler.Query.cs), [`ExcelHandler.Query.RowWhere.cs`](https://github.com/iOfficeAI/OfficeCLI/blob/main/ExcelHandler.Query.RowWhere.cs), and [`PowerPointHandler.Query.cs`](https://github.com/iOfficeAI/OfficeCLI/blob/main/PowerPointHandler.Query.cs)—map these abstract selectors to concrete OOXML document object model queries.

## CSS-Like Selector Syntax

### Element and Attribute Selectors

Target specific OOXML element types using tag-like names followed by optional attribute predicates in square brackets. The engine supports standard comparison operators including equality (`=`), inequality (`!=`), greater than (`>`), less than (`<`), and pattern matching (`~=`).

```bash

# Select rows where Score is greater than 80

officecli query document.xlsx 'row[Score>80]'

# Select cells with non-zero values

officecli query document.xlsx 'cell[value!=0]'

# Regex match: formulas containing "SUM"

officecli query document.xlsx 'cell[formula~=SUM]'

```

### Pseudo-Selectors

Filter elements based on content state using CSS-style pseudo-selectors. According to [`AttributeFilter.cs`](https://github.com/iOfficeAI/OfficeCLI/blob/main/AttributeFilter.cs) implementation, the following filters are supported:

- `:contains("text")` – Elements containing the specified substring
- `:empty` – Elements with no text content
- `:no-alt` – Elements missing alternative text attributes

```bash

# Find paragraphs containing the word "Quarterly"

officecli query document.docx 'paragraph:contains("Quarterly")'

# Find empty table cells

officecli query document.xlsx 'cell:empty'

```

### Combinators

Navigate the document hierarchy using child and descendant combinators:

- **Space** (descendant): Matches any depth nesting (`slide shape` finds shapes anywhere inside slides)
- **`>`** (direct child): Matches only immediate children (`slide > shape` excludes nested group shapes)

```bash

# Direct children only

officecli query presentation.pptx 'slide > shape'

# Any depth descendants

officecli query presentation.pptx 'slide shape'

```

## Boolean Operators in OfficeCLI Queries

### Implicit AND (Conjunction)

Multiple attribute predicates within the same square brackets create an implicit **AND** relationship. The [`AttributeFilter.cs`](https://github.com/iOfficeAI/OfficeCLI/blob/main/AttributeFilter.cs) engine requires all predicates to evaluate true for an element to match. Separate predicates with spaces.

```bash

# Rows where Score > 80 AND Year < 2025

officecli query report.xlsx 'row[Score>80 Year<2025]'

```

You can also chain element selectors with combinators, which also implies AND logic (the element must satisfy both positional and attribute constraints).

### Explicit OR (Union)

Use commas to separate selectors and create a union of result sets, equivalent to CSS group selectors. This implements logical **OR**.

```bash

# All shapes OR pictures

officecli query presentation.pptx 'shape, picture'

# Rows matching either condition

officecli query data.xlsx 'row[Status="Draft"], row[Priority=1]'

```

### Complex Boolean Logic

Combine commas and bracketed predicates to express nested boolean logic. The comma operator has lower precedence than the implicit AND within brackets.

```bash

# (Score > 80 AND Year < 2025) OR Status = "Closed"

officecli query metrics.xlsx 'row[Score>80 Year<2025], row[Status="Closed"]'

```

## Practical Examples by File Type

### Word Documents (.docx)

Use `paragraph`, `run`, and `field` selectors to search Word documents stored in the Open XML format.

```bash

# Fields containing page references

officecli query contract.docx 'field:contains("Page")' --json

# Empty paragraphs that are direct children of the body

officecli query document.docx 'body > paragraph:empty' --json

```

### Excel Spreadsheets (.xlsx)

The `row` and `cell` selectors work with the [`ExcelHandler.Query.RowWhere.cs`](https://github.com/iOfficeAI/OfficeCLI/blob/main/ExcelHandler.Query.RowWhere.cs) implementation to filter tabular data.

```bash

# Cells with formulas containing errors

officecli query budget.xlsx 'cell[formula]:contains("#REF!")' --json

# Numeric comparison on cell values

officecli query sales.xlsx 'cell[Revenue>100000]' --json

```

### PowerPoint Presentations (.pptx)

Target slides, shapes, pictures, and text frames using the [`PowerPointHandler.Query.cs`](https://github.com/iOfficeAI/OfficeCLI/blob/main/PowerPointHandler.Query.cs) implementation.

```bash

# Shapes with specific fill colors

officecli query deck.pptx 'shape[fill="1E2761"]' --json

# Pictures missing alt text

officecli query presentation.pptx 'picture:no-alt' --json

```

## Advanced Regex Matching

The `~=` operator enables substring matching against attributes, useful for detecting partial formula matches or pattern-based content.

```bash

# All cells with formulas containing "VLOOKUP" or "HLOOKUP"

officecli query data.xlsx 'cell[formula~=LOOKUP]' --json

```

## Summary

- **Element selection** uses CSS-like tags (`row`, `cell`, `shape`) filtered by attributes in square brackets
- **Implicit AND** combines multiple predicates within `[attr1=value1 attr2=value2]`
- **Explicit OR** uses comma-separated selectors (`selector1, selector2`) to union result sets
- **Pseudo-selectors** (`:contains`, `:empty`, `:no-alt`) filter based on content state
- **Combinators** (`>`, space) navigate parent-child relationships in the OOXML hierarchy
- **All selectors are case-insensitive** for element names and attribute keys

## Frequently Asked Questions

### How do I perform an OR operation between different element types?

Use a comma to separate selectors. The query `shape, picture, table` returns a union of all shapes, pictures, and tables found in the document. This is parsed by [`AttributeFilter.cs`](https://github.com/iOfficeAI/OfficeCLI/blob/main/AttributeFilter.cs) as three independent queries whose results are merged.

### Are CSS selectors case-sensitive in OfficeCLI?

No. According to the implementation in [`AttributeFilter.cs`](https://github.com/iOfficeAI/OfficeCLI/blob/main/AttributeFilter.cs), both element names (like `Row` vs `row`) and attribute keys are normalized to lowercase before evaluation, making the query syntax case-insensitive for easier command-line usage.

### Can I nest boolean operators to create complex conditions?

Yes. You can combine implicit AND within brackets with explicit OR via commas. For example, `slide[layout="Title"] > shape, picture:contains("Logo")` finds shapes that are direct children of title slides OR pictures containing the text "Logo".

### What file formats support the query command?

The [`CommandBuilder.GetQuery.cs`](https://github.com/iOfficeAI/OfficeCLI/blob/main/CommandBuilder.GetQuery.cs) dispatcher supports Word `.docx` (handled by [`WordHandler.Query.cs`](https://github.com/iOfficeAI/OfficeCLI/blob/main/WordHandler.Query.cs)), Excel `.xlsx` (handled by [`ExcelHandler.Query.RowWhere.cs`](https://github.com/iOfficeAI/OfficeCLI/blob/main/ExcelHandler.Query.RowWhere.cs)), and PowerPoint `.pptx` (handled by [`PowerPointHandler.Query.cs`](https://github.com/iOfficeAI/OfficeCLI/blob/main/PowerPointHandler.Query.cs)) formats based on the Open XML standard.