Page 1 of 1
Limit the value of an element based on existing XML data
Posted: Thu May 03, 2018 4:34 pm
by daddyjones
I'm fairly sure what I want to do can't be done, but I thought I'd ask on here just in case.
If I have the following XML:
Code: Select all
<foo>
<bar name="lorem" />
<bar name="ipsum" />
<bar name="dolor" />
<bar name="sit" />
...
</foo>
Is it possible, in XML Schema to restrict a new element to the existing values of the name attribute? For example, I would define a new element called <foobar> which had an attribute called "name" and the value of the attribute could only be, in this case, lorem, ipsum, dolor or sit - and that's based on the XML and not an <xs:enumeration />. So if I added elements to the XML I would increase the possible values allowed.
I hope that all makes sense!
Re: Limit the value of an element based on existing XML data
Posted: Fri May 04, 2018 12:13 pm
by tavy
Hello,
If you want to restrict the values from an attribute based on some exiting values from the XML, you can use Schematron rules inside XSD or as a separate Schematron schema file. The Schematron rule in your case can be something like this:
Code: Select all
<sch:rule context="foobar/@name">
<sch:let name="values" value="//bar/@name"/>
<sch:assert test=". = $values"> Invalid value "<sch:value-of select="."/>",
should be one of: "<sch:value-of select="$values"/>"
</sch:assert>
</sch:rule>
Best Regards.
Octavian
Re: Limit the value of an element based on existing XML data
Posted: Thu May 10, 2018 4:42 pm
by daddyjones
thanks for the reply - so there's no way of doing this built into XML Schema itself? Would most standard parsers be able to validate based on both XML Schema and Schematron?
Re: Limit the value of an element based on existing XML data
Posted: Thu May 10, 2018 4:44 pm
by daddyjones
Could XPointer be used to achieve this?
Re: Limit the value of an element based on existing XML data
Posted: Thu May 10, 2018 5:34 pm
by daddyjones
Or XInclude? I think I could use XInclude to include an XML fragment - which might do, although I'm struggling to figure out how to get Oxygen to work with this...
Re: Limit the value of an element based on existing XML data
Posted: Fri May 11, 2018 1:30 pm
by Radu
Hi,
I second Octavian's post that Schematron is the only way to do this.
Regards,
Radu