Skip to content

Add xs:id and xs:idref resolution#1222

Open
pahjbo wants to merge 12 commits into
tefra:mainfrom
pahjbo:idref
Open

Add xs:id and xs:idref resolution#1222
pahjbo wants to merge 12 commits into
tefra:mainfrom
pahjbo:idref

Conversation

@pahjbo

@pahjbo pahjbo commented Jun 26, 2026

Copy link
Copy Markdown

📒 Description

This pull request adds support for XML IDREF fields in dataclass models, and adds functionality that is similar to Java's JAXB handling of @XmlID and @XmlIDREF annotations in that when serialising an attribute that is marked as an IDREF, then only ID is emitted, and then when parsing such an XML instance the ID is replaced with the full dataclass instance.

This is related to the discussion item #1221

🔗 What I've Done

I have added two extra metadata items

  • at the field level - idref as bool that signifies if the attribute is an xs:IDREF
  • at the class Meta level - key as a list[str] that lists the attribute names that when the values are concatenated make up the xs:ID

This second one might seem a little complex, but I have a use case where I need such a thing, and hopefully the unit test example makes this plausible.

Once the data model has been set up, the render and parse are transparent to the caller with the conversion to an xs:ID and the instantiation of the appropriate object happening automatically

💬 Comments

I know that this is an unsolicited new feature, that you might not want to spend time on, but I believe that it is valuable to people with complex XML structures.

There are probably more tests that you might want, but I would need a bit more help with using your harness to do them.
Also If you like the idea, then I can spend some more time on documentation.

🛫 Checklist

  • Updated docs
  • Added unit-tests - only an "end to end" test - perhaps some of the individual components e.g. IdRefNode could have some tests
  • Sample tests pass
  • W3C tests pass

pahjbo added 12 commits June 25, 2026 12:13
the key attribute is a list of strings that are the attribute names that when concatenated make up the XMLID for the class
This is only a step on the way as it is
doing IDREF sweeps at the end of the parse - it would more efficient to do the resolution during the parse.
in order to test multiple unresolved idrefs
to check that is still working after list was supported.

also sort the imports
avoids clash with another test class
@sonarqubecloud

Copy link
Copy Markdown

@codecov

codecov Bot commented Jun 26, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 82.52427% with 18 lines in your changes missing coverage. Please review.
✅ Project coverage is 99.81%. Comparing base (00e6276) to head (595e45c).

Files with missing lines Patch % Lines
xsdata/formats/dataclass/parsers/nodes/element.py 69.69% 6 Missing and 4 partials ⚠️
xsdata/formats/dataclass/parsers/nodes/idref.py 84.61% 4 Missing and 2 partials ⚠️
xsdata/formats/dataclass/serializers/mixins.py 92.85% 0 Missing and 1 partial ⚠️
xsdata/utils/testing.py 50.00% 0 Missing and 1 partial ⚠️
Additional details and impacted files
@@             Coverage Diff             @@
##              main    #1222      +/-   ##
===========================================
- Coverage   100.00%   99.81%   -0.19%     
===========================================
  Files          116      117       +1     
  Lines         9376     9478     +102     
  Branches      1439     1462      +23     
===========================================
+ Hits          9376     9460      +84     
- Misses           0       10      +10     
- Partials         0        8       +8     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant