# Draw.io XML Schema Guide This guide explains the structure of draw.io (diagrams.net) XML files to help you understand and create diagrams programmatically. ## Basic Structure A draw.io XML file has the following hierarchy: ```xml ``` ## Root Element: `` The root element of a draw.io file. **Attributes:** - `host`: The application that created the file (e.g., "app.diagrams.net") - `modified`: Last modification timestamp - `agent`: Browser / user agent information - `version`: Version of the application - `type`: File type (usually "device" or "google") **Example:** ```xml ``` ## Diagram Element: `` Each page in your draw.io document is represented by a `` element. **Attributes:** - `id`: Unique identifier for the diagram - `name`: The name of the diagram / page **Example:** ```xml ``` ## Graph Model: `` Contains the actual diagram data. **Attributes:** - `dx`: Grid size in x-direction (usually 1) - `dy`: Grid size in y-direction (usually 1) - `grid`: Whether grid is enabled (0 or 1) - `gridSize`: Grid cell size (usually 10) - `guides`: Whether guides are enabled (0 or 1) - `tooltips`: Whether tooltips are enabled (0 or 1) - `connect`: Whether connections are enabled (0 or 1) - `arrows`: Whether arrows are enabled (0 or 1) - `fold`: Whether folding is enabled (0 or 1) - `page`: Whether page view is enabled (0 or 1) - `pageScale`: Scale of the page (usually 1) - `pageWidth`: Width of the page (e.g., 850) - `pageHeight`: Height of the page (e.g., 1100) - `math`: Whether math typesetting is enabled (0 or 1) - `shadow`: Whether shadows are enabled (0 or 1) **Example:** ```xml ``` ## Root Cell Container: `` Contains all the cells in the diagram. **Note:** When generating diagrams, you only need to provide the mxCell elements - the root container and root cells (id="0", id="1") are added automatically. **Internal structure (auto-generated):** ```xml ``` ## Cell Elements: `` The basic building block of diagrams. Cells represent shapes, connectors, text, etc. **Attributes for all cells:** - `id`: Unique identifier for the cell - `parent`: ID of the parent cell (typically "1" for most cells) - `value`: Text content of the cell - `style`: Styling information (see Style section below) **Attributes for shapes (vertices):** - `vertex`: Set to "1" for shapes - `connectable`: Whether the shape can be connected (0 or 1) **Attributes for connectors (edges):** - `edge`: Set to "1" for connectors - `source`: ID of the source cell - `target`: ID of the target cell **Example (Rectangle shape):** ```xml ``` **Example (Connector):** ```xml ``` ## Geometry: `` Defines the position and dimensions of cells. **Attributes for shapes:** - `x`: The x-coordinate of the **top-left** point of the shape. - `y`: The y-coordinate of the **top-left** point of the shape. - `width`: The width of the shape. - `height`: The height of the shape. - `as`: Specifies the role of this geometry within its parent cell. Typically set to `"geometry"` for the main shape definition. **Attributes for connectors:** - `relative`: Set to "1" for relative geometry - `as`: Set to "geometry" **Example for shapes:** ```xml ``` **Example for connectors:** ```xml ``` ## Cell Style Reference Styles are specified as semicolon-separated `key=value` pairs in the `style` attribute of `` elements. ### Shape-specific Styles - Rectangle: `shape=rectangle` - Ellipse: `shape=ellipse` - Triangle: `shape=triangle` - Rhombus: `shape=rhombus` - Hexagon: `shape=hexagon` - Cloud: `shape=cloud` - Actor: `shape=actor` - Cylinder: `shape=cylinder` - Document: `shape=document` - Note: `shape=note` - Card: `shape=card` - Parallelogram: `shape=parallelogram` ### Connector Styles - `endArrow=classic`: Arrow type at the end (classic, open, oval, diamond, block) - `startArrow=none`: Arrow type at the start (none, classic, open, oval, diamond) - `curved=1`: Curved connector (0 or 1) - `edgeStyle=orthogonalEdgeStyle`: Connector routing style - `elbow=vertical`: Elbow direction (vertical, horizontal) - `jumpStyle=arc`: Jump style for line crossing (arc, gap) - `jumpSize=10`: Size of the jump ## Special Cells Draw.io files contain two special cells that are always present: 1. **Root Cell** (id = "0"): The parent of all cells 2. **Default Parent Cell** (id = "1", parent = "0"): The default layer and parent for most cells ## Tips for Creating Draw.io XML 1. **Generate ONLY mxCell elements** - wrapper tags and root cells (id="0", id="1") are added automatically 2. Start IDs from "2" (id="0" and id="1" are reserved for root cells) 3. Assign unique and sequential IDs to all cells 4. Define parent relationships correctly (use parent="1" for top-level shapes) 5. Use `mxGeometry` elements to position shapes 6. For connectors, specify `source` and `target` attributes 7. **CRITICAL: All mxCell elements must be siblings. NEVER nest mxCell inside another mxCell.** ## Common Patterns ### Grouping Elements To group elements, create a parent cell and set other cells' `parent` attribute to its ID: ```xml ``` ### Swimlanes Swimlanes use the `swimlane` shape style. **IMPORTANT: All mxCell elements (swimlanes, steps, and edges) must be siblings under ``. Edges are NOT nested inside swimlanes or steps.** ```xml ``` ### Tables Tables use multiple cells with parent-child relationships: ```xml ``` ## Advanced Features ### Custom Attributes Draw.io allows adding custom attributes to cells: ```xml ``` These custom attributes can store additional metadata or be used by plugins and custom behaviors. ### User-defined Styles You can define custom styles for cells by combining various style attributes: ```xml ``` ### Layers You can create multiple layers in a diagram to organize complex diagrams: ```xml ```