Skip to content

Commit 5222ce3

Browse files
fix(terminology): keep exported XML well-formed for text with & and <
The terminology and data-training exporters build the document with ElementTree, then restore '&lt;', '&gt;' and '&amp;' to raw characters. A description such as '投入 < 10% & 增长 > 5%' therefore came out as markup that no longer parses, and that string is inserted into a prompt template, so the model received broken structure instead of escaped text. Restore only the quote entities, which cannot break the document, and keep the structural ones escaped.
1 parent 79ea0e1 commit 5222ce3

3 files changed

Lines changed: 99 additions & 8 deletions

File tree

‎backend/apps/data_training/curd/data_training.py‎

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -571,11 +571,10 @@ def to_xml_string(_dict: list[dict] | dict, root: str = 'sql-examples') -> str:
571571
end_index = pretty_xml.find('>') + 1
572572
pretty_xml = pretty_xml[end_index:].lstrip()
573573

574-
# 替换所有 XML 转义字符
574+
# 只还原不会破坏 XML 结构的引号转义。'&lt;'、'&gt;'、'&amp;' 必须保留:
575+
# 术语描述或示例 SQL 里出现 '&' 或 '<' 时,还原成原字符会让整段内容不再是
576+
# 合法 XML,而这段文本要作为模板片段交给模型。
575577
escape_map = {
576-
'&lt;': '<',
577-
'&gt;': '>',
578-
'&amp;': '&',
579578
'&quot;': '"',
580579
'&apos;': "'"
581580
}

‎backend/apps/terminology/curd/terminology.py‎

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -926,11 +926,10 @@ def to_xml_string(_dict: list[dict] | dict, root: str = 'terminologies') -> str:
926926
end_index = pretty_xml.find('>') + 1
927927
pretty_xml = pretty_xml[end_index:].lstrip()
928928

929-
# 替换所有 XML 转义字符
929+
# 只还原不会破坏 XML 结构的引号转义。'&lt;'、'&gt;'、'&amp;' 必须保留:
930+
# 术语描述或示例 SQL 里出现 '&' 或 '<' 时,还原成原字符会让整段内容不再是
931+
# 合法 XML,而这段文本要作为模板片段交给模型。
930932
escape_map = {
931-
'&lt;': '<',
932-
'&gt;': '>',
933-
'&amp;': '&',
934933
'&quot;': '"',
935934
'&apos;': "'"
936935
}

‎backend/tests/test_xml_escaping.py‎

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
"""Terminology and example XML must stay well-formed for real text.
2+
3+
Both ``to_xml_string`` helpers build the document with ElementTree and then
4+
restored ``&lt;``/``&gt;``/``&amp;`` to raw characters, so a description such as
5+
``投入 < 10% & 增长 > 5%`` produced output that no longer parses. That string is
6+
inserted into a prompt template, so broken markup reached the model. Only the
7+
quote entities are safe to restore; the structural ones must stay escaped.
8+
9+
Load the functions through AST like the other isolated tests: no driver, ORM or
10+
web stack is imported.
11+
"""
12+
13+
import ast
14+
import xml.etree.ElementTree as ET
15+
from pathlib import Path
16+
from xml.dom.minidom import parseString
17+
18+
import pytest
19+
20+
BACKEND = Path(__file__).resolve().parents[1]
21+
22+
23+
def _load(path: Path, names: set):
24+
tree = ast.parse(path.read_text(encoding="utf-8"))
25+
module = ast.Module(
26+
body=[node for node in tree.body if getattr(node, "name", None) in names],
27+
type_ignores=[],
28+
)
29+
namespace = {"parseString": parseString, "ET": ET}
30+
exec(compile(module, str(path), "exec"), namespace)
31+
return namespace
32+
33+
34+
@pytest.fixture(scope="module")
35+
def xml_helpers():
36+
base = _load(BACKEND / "common/utils/dict_to_xml.py", {"_add_element", "dict_to_xml"})
37+
terminology = _load(
38+
BACKEND / "apps/terminology/curd/terminology.py", {"to_xml_string"}
39+
)
40+
training = _load(
41+
BACKEND / "apps/data_training/curd/data_training.py", {"to_xml_string"}
42+
)
43+
for namespace in (terminology, training):
44+
namespace["dict_to_xml"] = base["dict_to_xml"]
45+
return {
46+
"dict_to_xml": base["dict_to_xml"],
47+
"ET": ET,
48+
"terminology": terminology["to_xml_string"],
49+
"training": training["to_xml_string"],
50+
}
51+
52+
53+
@pytest.mark.parametrize(
54+
"text",
55+
[
56+
"R&D budget",
57+
"投入 < 10% & 增长 > 5%",
58+
"a <b> tag and an & ampersand",
59+
],
60+
)
61+
def test_terminology_text_with_structural_characters_stays_parseable(xml_helpers, text):
62+
output = xml_helpers["terminology"](
63+
{"words": ["GDP"], "description": text}, "terminology"
64+
)
65+
66+
document = parseString(output)
67+
description = document.getElementsByTagName("description")[0].firstChild.data
68+
assert description == text
69+
70+
71+
@pytest.mark.parametrize("text", ["select a < b and c & d", "where x = 'A&B'"])
72+
def test_example_sql_with_structural_characters_stays_parseable(xml_helpers, text):
73+
output = xml_helpers["training"]({"sql-examples": [{"question": "q", "sql": text}]})
74+
75+
parseString(output)
76+
77+
78+
def test_quotes_are_still_restored_for_readability(xml_helpers):
79+
output = xml_helpers["terminology"](
80+
{"words": ["GDP"], "description": 'a "quoted" phrase'}, "terminology"
81+
)
82+
83+
document = parseString(output)
84+
assert document.getElementsByTagName("description")[0].firstChild.data == 'a "quoted" phrase'
85+
86+
87+
def test_plain_text_keeps_the_existing_shape(xml_helpers):
88+
output = xml_helpers["terminology"]({"words": ["GDP", "国内生产总值"], "description": "d"})
89+
90+
document = parseString(output)
91+
assert document.documentElement.tagName == "terminologies"
92+
words = [node.firstChild.data for node in document.getElementsByTagName("word")]
93+
assert words == ["GDP", "国内生产总值"]

0 commit comments

Comments
 (0)