---
title: "Elementos personalizados"
description: "Cómo extender AbstractElement para crear nodos XML propios, addendas tipadas o complementos personalizados compatibles con @nodecfdi/cfdi-elements."
canonical_url: "https://nodecfdi.com/packages/cfdi-elements/advanced/custom-elements"
---

> Index of every page on this site: [llms.txt](https://nodecfdi.com/llms.txt)

Si necesitas modelar una **Addenda** propia o un nodo XML que no forma parte de los 25 módulos incluidos, puedes extender `AbstractElement<TAttributes>` directamente.

## Crear una clase extendiendo `AbstractElement`

Al extender `AbstractElement`:

1. Implementa `getElementName(): string` con el nombre XML del nodo (por ejemplo `miaddenda:Addenda`). El constructor valida automáticamente el nombre con `isValidXmlName` de `@nodecfdi/cfdi-core` y lanza `SyntaxError` si no es válido.
2. Opcionalmente sobrescribe `getFixedAttributes()` para incluir `xmlns`, `xsi:schemaLocation` o versiones fijas.
3. Opcionalmente sobrescribe `getChildrenOrder()` para garantizar el orden de serialización de los hijos.
4. Usa `this.helperGetOrAdd(new Hijo())` para nodos hijo únicos (singleton).

```ts [mi-addenda.ts]
import type { AttributesRecord } from '@nodecfdi/cfdi-core/types';
import { AbstractElement } from '@nodecfdi/cfdi-elements';
import { Comprobante } from '@nodecfdi/cfdi-elements/cfdi40';

type MiDetalleAttributes = 'Referencia' | 'OrdenCompra';

class MiDetalle extends AbstractElement<MiDetalleAttributes> {
  public getElementName(): string {
    return 'miaddenda:Detalle';
  }
}

type MiAddendaAttributes = 'Version';

class MiAddenda extends AbstractElement<MiAddendaAttributes> {
  public getElementName(): string {
    return 'miaddenda:Factura';
  }

  public getFixedAttributes(): Record<string, string> {
    return {
      'xmlns:miaddenda': 'https://ejemplo.com/addenda',
      'Version': '1.0',
    };
  }

  public getChildrenOrder(): string[] {
    return ['miaddenda:Detalle'];
  }

  public getDetalle(): MiDetalle {
    return this.helperGetOrAdd(new MiDetalle());
  }

  public addDetalle(attributes: AttributesRecord<MiDetalleAttributes> = {}): MiDetalle {
    const detalle = this.getDetalle();
    detalle.addAttributes(attributes);

    return detalle;
  }
}

const comprobante = new Comprobante();
const addenda = new MiAddenda();
addenda.addDetalle({ Referencia: 'REF-01', OrdenCompra: 'OC-998' });

comprobante.addAddenda(addenda);
```

## Sitemap

See the full [sitemap](https://nodecfdi.com/sitemap.md) for all pages.
