Product case study · November 26, 2026

Ask Claude Code to fix an XML field that looks missing

Repair an XML importer that mistakes a leaf element for a missing field. Give Claude Code fixed tests for present, blank, zero and absent values.

Reading time
5 min
Checked
Nov 26, 2026
A magnifying lens highlights a blue leaf on a charcoal paper tree beside an empty outlined slot.
Separate a missing field from a present field with no value.
Bottom line

Check whether the element is None before interpreting its text. A leaf can exist without having child elements.

An importer says an invoice has no reference. You open the XML and the reference is right there. The parser may have found it correctly: the mistake can be the condition that decides whether the result exists.

This exercise gives Claude Code a small repair with an observable finish line. You will keep five acceptance tests unchanged while the agent fixes one implementation file. The Python fixture and reference repair were run locally; the Claude Code prompt below is a proposed workflow, not a recorded agent run.

Define what counts as missing

For this practice importer, use the following contract:

InputRequired result
Reference containing INV-42Return INV-42
Reference containing 0Return the string 0
Empty reference elementRaise ValueError("blank reference")
Whitespace-only referenceRaise ValueError("blank reference")
No reference elementRaise ValueError("missing reference")

These are authored requirements for the exercise. Your production importer may use different policies, but deciding them before the repair prevents an agent from silently treating every unusual value as absent.

Use a new practice folder with Python 3 and an already configured Claude Code installation. The project-session lesson covers setup. This fixture uses small, trusted XML strings you type yourself.

Reproduce the wrong branch

Save this intentionally faulty implementation as invoice.py:

import xml.etree.ElementTree as ET


def read_reference(xml):
    root = ET.fromstring(xml)
    field = root.find("reference")
    if not field:
        raise ValueError("missing reference")
    value = (field.text or "").strip()
    if not value:
        raise ValueError("blank reference")
    return value

Then save test_invoice.py:

import unittest
from invoice import read_reference


class ReferenceTests(unittest.TestCase):
    def test_present_leaf(self):
        xml = "<invoice><reference> INV-42 </reference></invoice>"
        self.assertEqual(read_reference(xml), "INV-42")

    def test_zero_is_text(self):
        xml = "<invoice><reference>0</reference></invoice>"
        self.assertEqual(read_reference(xml), "0")

    def test_empty_leaf(self):
        with self.assertRaisesRegex(ValueError, "^blank reference$"):
            read_reference("<invoice><reference /></invoice>")

    def test_whitespace_leaf(self):
        xml = "<invoice><reference>   </reference></invoice>"
        with self.assertRaisesRegex(ValueError, "^blank reference$"):
            read_reference(xml)

    def test_absent_field(self):
        with self.assertRaisesRegex(ValueError, "^missing reference$"):
            read_reference("<invoice />")


if __name__ == "__main__":
    unittest.main()

Run this from the folder containing both files:

python3 --version
python3 -m unittest -v test_invoice.py

On the local CPython 3.14.4 run, the faulty version produced two errors, two failures and one passing test. The valid references took the missing-field branch; the blank references raised the wrong message. The absent-field test already passed. A passing missing-field test alone would have hidden the bug.

Python’s ElementTree documentation explains that a leaf element currently tests false because it has no subelements. find() returns None when no match exists. Testing an element’s truth value is deprecated from Python 3.12, and the documentation warns that this behavior will change. Record your interpreter version: a future runtime may not reproduce this exact failure pattern.

Give Claude Code the repair boundary

Start claude in the practice folder and send:

Read invoice.py and test_invoice.py. Run the five tests and report the
actual outcome before editing. Explain why the present reference takes
the missing-field branch on this Python version.

Change only invoice.py. Preserve the read_reference(xml) interface and
all five test expectations. Distinguish an absent reference from a blank
one, trim surrounding whitespace, and preserve the string "0".
Do not add dependencies or change the tests.

Run the same test command after the repair. Report the Python version,
test count, exit result, and exact implementation change. If the original
failure does not reproduce, say so rather than claiming a failing baseline.

Anthropic’s common workflows describes debugging from a reproduction and verifying fixes. This prompt makes that workflow reviewable: the agent must show a baseline, respect a file boundary, and rerun the same checks.

Review requested command permissions in the current session. If the agent proposes installing a new XML library for this exercise, ask it to explain why the existing standard-library parser cannot satisfy the five requirements. If it edits the expected errors, restore the test text above before evaluating the repair.

Inspect the reference change

The reference repair replaces if not field: with if field is None:. The complete repaired function is:

import xml.etree.ElementTree as ET


def read_reference(xml):
    root = ET.fromstring(xml)
    field = root.find("reference")
    if field is None:
        raise ValueError("missing reference")
    value = (field.text or "").strip()
    if not value:
        raise ValueError("blank reference")
    return value

Run the same command again:

python3 -m unittest -v test_invoice.py

The reference repair passed all five tests locally on CPython 3.14.4. That is evidence for this fixture, not a promise about what your agent will produce. Inspect both files afterward: the implementation should contain the explicit existence check, and the tests should retain all five cases.

The later if not value remains intentional. At that point, value is a stripped string and the exercise requires rejecting an empty string. The text 0 is nonempty. Replacing every truth test indiscriminately would obscure these different decisions.

Apply the lesson to a real importer

If your real document still reports a missing reference, keep a small sanitized sample and inspect its structure before broadening the patch. This exercise only looks for a direct, unnamespaced reference child. Namespaces, nested fields, repeated references and mixed content need their own requirements and fixtures; the five checks do not cover them.

Malformed XML and untrusted uploads also sit outside this lab. Consult Python’s linked XML security guidance before accepting external documents. A corrected existence check does not establish parser safety.

For the repair review, retain the input that failed, the unchanged tests, the interpreter version and the final diff. Those artifacts let another person repeat the decision without relying on an agent’s summary.

Sources

Checked November 26, 2026:

Put this to work

Why an ElementTree truth test does not reliably answer whether a field exists.

Try

Give Claude Code a two-file XML importer exercise in a practice folder.

Prove it worked

Run the same five tests before and after the change and inspect the implementation diff.

Keep in view

  • Reproduce an XML leaf being mistaken for a missing field.
  • Keep missing, blank and zero values distinct in fixed tests.
  • Review a one-condition repair before accepting an agent's result.
Learn the workflow: Set up your Claude Code project session