问题
I have an XML document with language specific elements, example:
<root lang="en fr">
<section>
<title lang="en">English Title</title>
<title lang="fr">French Title</title>
<sequence>2</sequence>
<field>
<type>date</type>
<label lang="en">English field label</label>
<label lang="fr">French field label</label>
</field>
</section>
<section>
<title lang="en">Another English Title</title>
<title lang="fr">Another French Title</title>
<sequence>1</sequence>
</section>
</root>
At the top, the root element is intended to declare the document defines both english and french information.
There are certain elements in the document that need to be provided in english and french, such as title
and label
, since the root element declares support for both.
Is it possible to to express such a constraint in XML Schema? I could express the root
element language attribute differently, such as lang-en="1" lang-fr="1"
if that helps.
回答1:
In XSD 1.0, you cannot represent such a constraint.
In XSD 1.1, you can use xsd:assert
, but it would have to be at the root
level. See How to access parent element in XSD assertion XPath? for an example of how to make an assertion using an every..in..satisfies
XPath 2.0 expression.
See also for general i18n in XML design considerations: xml:lang in XML document schemas
来源:https://stackoverflow.com/questions/38588631/how-to-validate-for-language-attributes-in-xsd