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

# Querying XML

The XML datatype supports querying with the XPath 1.0 query language.

## Querying an Element

```java theme={null}
import static org.camunda.spin.Spin.XML;

String xml = "<root><child id=\"child\"><a id=\"a\"/><a id=\"b\"/></child></root>";

SpinXmlTreeElement child = XML(xml).xPath("/root/child").element();
```

## Querying an Element List

```java theme={null}
import static org.camunda.spin.Spin.XML;

String xml = "<root><child id=\"child\"><a id=\"a\"/><a id=\"b\"/></child></root>";

SpinList<SpinXmlTreeElement> childs = XML(xml).xPath("/root/child/a").elementList();
```

## Querying an Attribute

```java theme={null}
import static org.camunda.spin.Spin.XML;

String xml = "<root><child id=\"child\"><a id=\"a\"/><a id=\"b\"/></child></root>";

SpinXmlTreeAttribute attribute = XML(xml).xPath("/root/child/@id").attribute();
```

## Querying an Attribute List

```java theme={null}
import static org.camunda.spin.Spin.XML;

String xml = "<root><child id=\"child\"><a id=\"a\"/><a id=\"b\"/></child></root>";

SpinList<SpinXmlTreeAttribute> attributes = XML(xml).xPath("/root/child/a/@id").attributeList();
```

## Querying a String

```java theme={null}
import static org.camunda.spin.Spin.XML;

String xml = "<root><child id=\"child\"><a id=\"a\"/><a id=\"b\"/></child></root>";

String value = XML(xml).xPath("string(/root/child/@id)").string();
```

## Querying a Number

```java theme={null}
import static org.camunda.spin.Spin.XML;

String xml = "<root><child id=\"child\"><a id=\"a\"/><a id=\"b\"/></child></root>";

Double count = XML(xml).xPath("count(/root/child/a)").number();
```

## Querying a Boolean

```java theme={null}
import static org.camunda.spin.Spin.XML;

String xml = "<root><child id=\"child\"><a id=\"a\"/><a id=\"b\"/></child></root>";

Boolean exists = XML(xml).xPath("boolean(/root/child)").bool();
```

## Querying with Namespaces

To use namespaces in spin with XML, you can choose one of the following methods or combine both of them.

### 1. Using a Single Prefix - URI Pair

```java theme={null}
import static org.camunda.spin.Spin.XML;

String xml = "<root xmlns:t=\"http://camunda.org\"><t:child id=\"child\"><a id=\"a\"/></t:child></root>";

SpinXmlTreeElement child = XML(xml).xPath("/root/t:child")
                                   .ns("t", "http://camunda.org");
                                   .element();
```

### 2. Using a Map of Prefix - URI Pairs

```java theme={null}
import static org.camunda.spin.Spin.XML;

String xml = "<root xmlns:t=\"http://camunda.org\"><t:child id=\"child\"><a id=\"a\"/></t:child></root>";

Map<String, String> namespaceMap = new HashMap<String, String>();
namespaceMap.put("t", "http://camunda.org");
namespaceMap.put("s", "http://camunda.com");

SpinXmlTreeElement child = XML(xml).xPath("/root/t:child")
                                   .ns(namespaceMap);
                                   .element();
```

<Info>
  If you are using `xmlns="<URI>"` in your XML file, spin uses `DEFAULT` as prefix for the namespace.<br />
  E.g.,: `<root xmlns="http://camunda.org"></root>` -- prefix: DEFAULT, namespace: [http://camunda.org](http://camunda.org) so you need
  to use `XML(xml).xPath("/DEFAULT:root")` to fetch the correct element.
</Info>
