fix: auto-inject JasperReports namespace before XSD validation
AI-generated JRXML often omits the xmlns declaration on the root element. The XSD schema requires targetNamespace, so validation would fail with "Element 'jasperReport': No matching global declaration available". _ensure_jr_namespace() detects missing xmlns and injects it before schema validation, making the validator tolerant of namespace-free JRXML.
This commit is contained in:
@@ -111,11 +111,28 @@ def _check_minimum_content(jrxml: str) -> list[str]:
|
||||
return issues
|
||||
|
||||
|
||||
JR_NAMESPACE = "http://jasperreports.sourceforge.net/jasperreports"
|
||||
|
||||
|
||||
def _ensure_jr_namespace(jrxml: str) -> str:
|
||||
"""如果 JRXML 根元素缺少命名空间声明,自动补上。"""
|
||||
import re
|
||||
if 'xmlns=' not in jrxml[:500]:
|
||||
return re.sub(
|
||||
r'(<jasperReport)\b',
|
||||
r'\1 xmlns="' + JR_NAMESPACE + '"',
|
||||
jrxml, count=1,
|
||||
)
|
||||
return jrxml
|
||||
|
||||
|
||||
def _validate_xsd(jrxml: str) -> tuple[bool, str]:
|
||||
"""根据 JasperReports XSD schema 验证 JRXML。"""
|
||||
if not SCHEMA_FILE.exists():
|
||||
return True, ""
|
||||
|
||||
jrxml = _ensure_jr_namespace(jrxml)
|
||||
|
||||
try:
|
||||
schema_doc = etree.parse(str(SCHEMA_FILE))
|
||||
xmlschema = etree.XMLSchema(schema_doc)
|
||||
|
||||
Reference in New Issue
Block a user