Is it possible to set an attribute's default value to a other attribute's value in XSD?
-
07-07-2021 - |
Domanda
Suppose I have an XML like this:
<foo ...>
<bar a="s1" b="s2" />
<bar a="s3" />
</foo>
What I'd like is to define in the XSD is that the default value of attribute b
should be the value of attribute a
. Is that possible?
Thanks in advance!
Soluzione
Short answer: no.
Medium answer, according to "XML Schema Part 1: Structures Second Edition", section "3.2.1 The Attribute Declaration Schema Component": "default specifies that the attribute is to appear unconditionally in the ·post-schema-validation infoset·, with the supplied value used whenever the attribute is not actually present"
Long and practical answer: the key is "post-schema-validation infoset". So ask yourself what you need the schema document for?
- if you need it to validate incoming XML documents, then just use
use="required"
for attributeb
- if you need it to build objects (e.g., using some (un)marshalling technologies such as Java's JAXB or .NET's
XmlSerializer
) then you must explicitely add some post-processing which satisifies this "post-schema-validation-infoset" term - you should detect if the value ofb
wasn't set and set it yourself - that's your business requirement - if you need to communicate your intent (*I/my app will treat empty
b
as having the same value asa
) add documentation. XML Schema doesn't have such concept. Using extrapolation - that would be the same requirement as "the value ofb
will be the current USD/EUR ratio" - it's just computed value
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow