# XML

{% embed url="<https://www.hackerrank.com/domains/python?badge_type=python&filters%5Bsubdomains%5D%5B%5D=xml>" %}

## XML 1 - Find the Score

<figure><img src="https://3452970062-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FhMewGT4PbQ5e1zQPC4Ko%2Fuploads%2FKNAYMGqTy73EJwIBYQc0%2Fimage.png?alt=media&#x26;token=7c101842-793a-4ce6-986b-15daecb0227f" alt=""><figcaption></figcaption></figure>

```python
import sys
import xml.etree.ElementTree as etree

def get_attr_number(node):
    count = len(node.attrib)

    for each in node:
        if len(each) > 1:
            for i in each:
                count += len(i.attrib)
        else:
            count += len(each.attrib)

    return count


if __name__ == '__main__':
    sys.stdin.readline()
    xml = sys.stdin.read()
    tree = etree.ElementTree(etree.fromstring(xml))
    root = tree.getroot()
    print(get_attr_number(root))
```

***

## XML 2 - Find the Maximum Depth

<figure><img src="https://3452970062-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FhMewGT4PbQ5e1zQPC4Ko%2Fuploads%2FZJqRHt5YsixMYNzn5GAA%2Fimage.png?alt=media&#x26;token=bc5cec3a-f12d-4681-94a5-37006b573255" alt=""><figcaption></figcaption></figure>

```python
import xml.etree.ElementTree as etree

maxdepth = 0
def depth(elem, level):
    global maxdepth

    level += 1

    if level > maxdepth:
        maxdepth = level

    for each in elem:
        depth(each, level)

if __name__ == '__main__':
    n = int(input())
    xml = ""
    for i in range(n):
        xml =  xml + input() + "\n"
    tree = etree.ElementTree(etree.fromstring(xml))
    depth(tree.getroot(), -1)
    print(maxdepth)
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://thamizhiniyancs.gitbook.io/writeups/hackerrank/python/xml.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
