Repository navigation
Cache XML from url: references as text, not markup (#179, item 1) - #188
Conversation
A url: response that is XML was cached with its tags, so a quote crossing an inline tag (eCFR's <I>) failed. Now JATS articles (root <article> or <pmc-articleset>) are read by XMLExtractor, and any other XML is flattened by the new xml_to_text: tags and attributes dropped, a block element starting a line, an element inside running text adding nothing. Malformed XML is stored as fetched, since recovery can silently drop text. XML is recognised by an xml or +xml media type or an XML declaration; XHTML stays on the HTML path and SVG is left alone. URL_SOURCE_CACHE_VERSION goes to 3 so url: entries cached with raw XML are refreshed. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
Claude finished @caufieldjh's task in 1m 25s —— View job Review of PR #188: cache XML from
|
An element was inline only when its direct parent held text, so <P>a <I><B>x</B></I> d</P> split into three lines. Inline context now passes down: a child is inline when its parent holds text, is itself inline, or is a text block (p, para, head, title, li). Only true breaks (p, para, li, tr, row, br, section, sec, div*, h1-h6) break inside running text, so TEI's inline <title> stays in its sentence. A JATS root is matched on its local name, so <j:article> goes to XMLExtractor, and acquire's root_element is public now that url.py uses it. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
Thanks for the review. Addressed in 70a9a2b. Nested inline elements (bug): fixed as you suggested. Inline context now passes down the tree. A child is inline when its parent has text of its own, is itself inline, or is a text block (
1. 2. Private import: 3. Prefixed JATS roots: fixed. The root is matched on its local name, so 4. Title for XML responses: left for a follow-up. Today, 5. UTF-16 sniffing: left as is. It is rare, and consistent with Locally after the change: URL/XML/acquire tests 224 passed, |
|
Claude finished @caufieldjh's task in 1m 5s —— View job Re-review of PR #188 after 70a9a2b (nested inline / prefixed JATS)
The main bug from my last review is fixed. Three of the smaller points are also dealt with: the inline ✅ The fix in
|
| Input | Result |
|---|---|
<P>a <I><B>bold italic</B></I> d</P> |
a bold italic d ✅ |
<P><I>(a)</I><B>Identification.</B></P> |
(a)Identification., one line ✅ |
<p>See <title>Hamlet</title> act 1</p> |
See Hamlet act 1 ✅ |
<rec><name>Resin</name><class>2</class></rec> |
Resin / 2 ✅ (rec is not running text) |
<doc>before<BR/>after</doc> |
breaks ✅ (_is_break wins over inline) |
eCFR <DIV8><HEAD>…</HEAD><P>…</P></DIV8> |
HEAD is a block because DIV8 has no direct text ✅ |
eCFR <GPOTABLE><ROW><ENT>a</ENT><ENT>b</ENT></ROW> |
one cell per line ✅ (ROW is not a text block) |
Splitting XML_BREAK_ELEMENTS from XML_TEXT_BLOCK_ELEMENTS is the right call. Narrowing _NUMBERED_BREAK to div\d*|h\d also stops a bare <h> from counting as a break.
⚠️ Remaining points (all minor)
-
Inline context carries all the way down, so structured content inside a text block runs together. Everything under a
<p>/<li>/<title>is inline unless it is a named break. Two examples:<li><name>A</name><id>B</id></li>givesAB.- DITA allows
<simpletable>inside<p>, so<p><simpletable><strow><stentry>a</stentry><stentry>b</stentry></strow></simpletable></p>givesab.
This doesn't affect eCFR, where tables are siblings of
<P>, and it is the opposite trade-off to the bug just fixed. One cheap option: stop passing the flag down through an element that has no direct text and has more than one element child. That would keep<I><B>and still split cell-like siblings. Writing it down as a known limit in thexml_to_textdocstring would also do. -
The PR description is out of date. It still says "
P,HEAD,DIV/DIV1–DIV9,H1–H6,BR,LI,TRand a few others always start a line".HEAD(andtitle/item) no longer break inside running text.docs/how-to/validate-urls.mdis already correct, so only the PR body needs editing. -
Test
element-only-p-no-space(tests/test_url_xml_to_text.py) asserts(a)Identification.. That is right, since the source has no space, but it pins behaviour that is arguably surprising for something like<P><I>(a)</I><B>x</B></P>. Keep it if that's intended. A one-line comment ("no whitespace in source, so none added") would help the next reader. -
Prefixed JATS (
url.py,_is_jats): this relies on BeautifulSoup'sfind_all("body")/find_all("p")also matchingj:body/j:p. I believe BeautifulSoup does match on eithernameorprefix:name, and the newtest_a_prefixed_jats_root_is_read_as_jatscovers it. Please confirm that test passes locally, since I couldn't run it. -
Not addressed yet, and optional for this PR (both from the last review):
- An XML entry's title still falls back to the URL. The first flattened line would be a better fallback.
- Sniffing only skips a UTF-8 BOM, so a UTF-16 XML body without an XML media type isn't sniffed as XML.
✅ Security, performance and tests
- Still parsed with
recover=Falsethrough the shared untrusted parser. The walk is still iterative (iterwalk), and the extra_local_nameper element keeps it linear. root_elementis now public. All call sites inacquire.pyandurl.pyand its doctests were renamed._is_jatshas doctests for the plain, prefixed and non-JATS cases.- New tests cover nested and deeply nested inline elements, paragraphs that hold only elements, inline
title/head/item, records that still split, and prefixed JATS. Together they cover both cases I asked for.
Summary: the blocking issue is fixed and tested, and I'd merge. Point 1 is worth a docstring note or a small follow-up. Point 2 is a quick edit to the PR description.
Running text passed down through any text block or inline element, so <li><name>A</name><id>B</id></li> and a DITA <simpletable> in <p> read as "AB". It now stops at an element that holds several elements and no text of its own: those are fields or cells, and each gets a line. A break costs a quote nothing, since matching normalizes whitespace; welding costs the match. Formatting chains such as <I><B> stay inline. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
Claude finished @caufieldjh's task in 57s —— View job Re-review of PR #188 after d91a546 (sibling fields in running text)
This commit deals with point 1 from my last review, and it does it the way I suggested. I'd merge. The only thing left is one edit to the PR description. The rest is minor. ✅ The new rule (
|
| Input | Result |
|---|---|
<P>a <I><B>bold italic</B></I> d</P> |
a bold italic d ✅ (I has one child) |
<P>see <I><B>x</B></I></P> |
see x ✅ |
<li><name>A</name><id>B</id></li> |
A / B ✅ |
DITA <simpletable> inside <p> |
one cell per line ✅ |
<P><I>(a)</I> <B>Identification.</B> Zinc…</P> |
one line ✅ (P has text of its own, the tail Zinc…) |
eCFR <P><I>(a) Identification.</I> Zinc oxide…</P> |
one line ✅. The real-fixture quotes are not affected |
len(element)and comments: I first thought comments or PIs would count as children here. They don't, becauseetree.strip_tags(root, etree.Comment, etree.PI, etree.Entity)(line 662) runs before the walk. So<P><I>x</I></P>still has one child. ✅- Why breaking is safe: the reasoning in the docstring and comment holds up. A break only adds whitespace, which matching normalizes. Welding two words together breaks the match.
test_a_quote_across_split_siblings_still_matchestests this through the real fetch path, which is a good choice.
⚠️ Remaining points (minor)
-
The opposite case now adds a space between adjacent inline siblings. An inline element that holds two elements and no text of its own now splits them:
<P>see <I><B>Zinc</B><SU>1</SU></I> d</P>givessee/Zinc/1/d, so the text readsZinc 1, where the source saysZinc1.- A quote written as
Zinc1(for example, a footnote marker copied from rendered text) would no longer match.
This is the mirror of the problem just fixed, and the source gives no way to tell the two apart. I think erring toward a break is right. A doctest or a test that pins this case down would record the trade-off on purpose. Optional.
-
The PR description is still out of date. It says "
P,HEAD, … always start a line" and "Any other element starts a new line". Since 70a9a2b and this commit:HEAD,TITLEandITEMfollow the running-text rule.- Several sibling elements with no text between them each get their own line, even inside
<P>/<LI>.
docs/how-to/validate-urls.mdis already correct, so only the PR body needs updating. -
The docs wording is a little loose.
validate-urls.mdsays "a single element alone in a<P>… counts as running text". The code means one child element, and that child can itself carry tail text.<P><I>a</I> b</P>takes the_has_direct_textpath anyway, so the behaviour is right. Only the sentence could be tighter. Nit. -
Still deferred from earlier reviews, and fine as a follow-up:
- An XML entry's title falls back to the URL.
- Sniffing skips only a UTF-8 BOM, so UTF-16 XML without an XML media type is not detected.
✅ Security, performance and tests
- Nothing changed on the security side: still
recover=Falsethrough the shared untrusted parser, and still an iterativeiterwalk. len(element)costs O(1) per element, and_has_direct_textis still one pass over the children. The walk stays linear.- Tests:
- The removed
element-only-p/element-only-p-no-spacecases were moved to the newtest_sibling_elements_without_text_between_are_not_welded, which uses the new expectation ((a)/Identification.). This also settles point 3 from my last review. single-child-chainchecks that one child is still inline.- Together these cover both sides of the new rule.
- The removed
Summary: the fix is correct, tested, and documented. I'd merge after updating the PR description. Point 1 is optional.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
Claude finished @caufieldjh's task in 52s —— View job Re-review of PR #188 after 35ca085 (adjacent inline siblings test)
This commit closes the last open points from my previous re-review. Nothing is blocking, and I'd merge. This time I could run things. I installed the package with pip, since
✅ What changed
|
|
Re-review points, in 35ca085:
|
Addresses item 1 of #179. Item 2 (several fields per JSON API source) and the third ask in the comments are not in this PR.
Problem
URLSourceflattened only HTML. An XML response, such as one CFR section from the eCFR versioner API (text/xml), was cached with its tags, so a quote crossing an inline tag failed:Change
xmlor*+xml, or by a leading XML declaration. HTML is still checked first, so XHTML (also when served astext/xml) stays on the HTML path. SVG and drawio are left as they were. A Word or Excel media type (…openxmlformats…) containsxmlbut is not treated as XML.XMLExtractorfirst, as the issue suggests, but only when the root is<article>or<pmc-articleset>(with or without a namespace prefix). Other XML that happens to have<body>/<p>(DITA, TEI) would otherwise lose its headings and lists.xml_to_text(etl/extract/xml.py):<I>in<P>), nested formatting (<B>in that<I>), or a lone element inP/HEAD/TITLE/LI.<li>or<p>, so<name>Resin</name><class>2</class>gives two lines, notResin2. A break never stops a quote matching, because matching normalizes whitespace; welding two words together would.P,PARA,LI,TR,ROW,BR,SECTION,SEC,DIV/DIV1–DIV9,H1–H6.TITLE/HEADinside a sentence stay inline (TEI). Names are matched on the lowercased local name._parseis split out as_parse_rootand reused.<a>one</a><b>two</b>, "two" is lost.content_typestaysurl.URL_SOURCE_CACHE_VERSIONgoes from 2 to 3, sourl:entries cached with raw XML are refreshed. This refetches everyurl:entry once (HTML and PDF too), as the version 2 bump did.Behaviour note
A JATS article fetched through
url:is now narrowed to its body text, as from other article sources. A quote of its abstract or title that matched the raw XML will fail; the docs point to the article'sPMID:/DOI:for those. This was a deliberate choice over flattening JATS whole.Tests
tests/test_url_xml_to_text.py(new) tests against the real eCFR response for 21 CFR 872.3275 (tests/fixtures/xml/, fetched 2026-11-30). It covers:<body>test_url_source_cache_version.py: a version 2 entry is stale.test_url_html_sanitize.py::test_xml_is_left_alonechecked the old raw-XML behaviour that Support XML responses for url: references, and several fields per JSON API source #179 changes. It is rewritten astest_xml_is_flattened_not_sanitized_as_html.just doctest),just mypyandjust format.just testrun had 8 failures in unrelated suites: ClinicalTrials cache, PMID network, HTML acceptance, validation counts, and Wikipedia. Which ones failed varied between runs, and they pass on rerun, which points to network or timing flakiness. They were not compared againstmain.Docs
docs/how-to/validate-urls.md: new "XML responses" section, with an eCFR example.docs/troubleshooting.md: the version 3 cache refresh.🤖 Generated with Claude Code