---
title: "XsdRetriever"
description: "Descarga recursiva de esquemas XSD y sus dependencias import e include para validación sin conexión."
canonical_url: "https://nodecfdi.com/packages/xml-resource-retriever/usage/xsd-retriever"
---

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

`XsdRetriever` extiende [`AbstractXmlRetriever`](https://nodecfdi.com/packages/xml-resource-retriever/api-reference/retrievers) para buscar y descargar recursivamente esquemas XML dentro del namespace `http://www.w3.org/2001/XMLSchema`.

Para cada documento analizado, inspecciona los siguientes elementos y reescribe su atributo a una ruta relativa local:

<table>
<thead>
  <tr>
    <th>
      Elemento
    </th>
    
    <th>
      Atributo
    </th>
  </tr>
</thead>

<tbody>
  <tr>
    <td>
      <code>
        import
      </code>
    </td>
    
    <td>
      <code>
        schemaLocation
      </code>
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        include
      </code>
    </td>
    
    <td>
      <code>
        schemaLocation
      </code>
    </td>
  </tr>
</tbody>
</table>

## Descarga recursiva con `retrieve()`

El método `retrieve(url)` descarga el esquema indicado, analiza sus dependencias de forma recursiva (incluyendo referencias relativas y absolutas), guarda cada archivo bajo el directorio base y reescribe los atributos `schemaLocation` en los archivos modificados.

```ts [scripts/retrieve-xsd.ts]
import { XsdRetriever } from '@nodecfdi/xml-resource-retriever';

const retriever = new XsdRetriever('./storage/xsd');
const mainSchema = await retriever.retrieve(
  'http://www.sat.gob.mx/sitio_internet/cfd/4/cfdv40.xsd',
);

console.log(mainSchema);
// ./storage/xsd/www.sat.gob.mx/sitio_internet/cfd/4/cfdv40.xsd
```

### Cómo se reescriben las rutas

Si un esquema `http://example.com/xsd/entities/ticket.xsd` contiene:

```xml
<xs:import
  namespace="http://example.com/articles/books"
  schemaLocation="http://example.com/xsd/articles/books.xsd"
/>
```

Al descargarlo con `retrieve()`, el archivo local se actualiza automáticamente a:

```xml
<xs:import
  namespace="http://example.com/articles/books"
  schemaLocation="../articles/books.xsd"
/>
```

## Consultar el historial con `retrieveHistory()`

Después de llamar a `retrieve()`, puedes obtener un objeto con el mapa de todas las URLs descargadas y su ruta local correspondiente. Cada nueva llamada a `retrieve()` reinicia el historial automáticamente.

```ts
const retriever = new XsdRetriever('./storage/xsd');
await retriever.retrieve('http://www.sat.gob.mx/sitio_internet/cfd/4/cfdv40.xsd');

const history = retriever.retrieveHistory();
for (const [remoteUrl, localPath] of Object.entries(history)) {
  console.log(`${remoteUrl} -> ${localPath}`);
}
```

## Calcular la ruta local con `buildPath()`

Si necesitas conocer dónde se almacenará (o dónde se almacenó) una URL sin iniciar ninguna descarga, usa `buildPath(url)`:

```ts
const retriever = new XsdRetriever('./storage/xsd');
const expectedPath = retriever.buildPath(
  'http://www.sat.gob.mx/sitio_internet/cfd/4/cfdv40.xsd',
);
// ./storage/xsd/www.sat.gob.mx/sitio_internet/cfd/4/cfdv40.xsd
```

## Descarga individual con `download()`

Si solo deseas descargar un único archivo XSD sin resolver ni descargar sus `import` o `include`, utiliza `download(url)`. El archivo igualmente pasa por la validación XML.

```ts
const retriever = new XsdRetriever('./storage/xsd');
const localFile = await retriever.download(
  'http://www.sat.gob.mx/sitio_internet/cfd/tipoDatos/tdCFDI/tdCFDI.xsd',
);
```

<tip>

Puedes inyectar una instancia configurada de [`NodeDownloader`](https://nodecfdi.com/packages/xml-resource-retriever/usage/node-downloader) o un [descargador personalizado](https://nodecfdi.com/packages/xml-resource-retriever/advanced/custom-downloaders) como segundo argumento del constructor: `new XsdRetriever(basePath, downloader)`.

</tip>

## Sitemap

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