Limit the value of an element based on existing XML data

This should cover W3C XML Schema, Relax NG and DTD related problems.
daddyjones
Posts: 9
Joined: Wed Apr 11, 2018 4:43 pm

Limit the value of an element based on existing XML data

Post 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!
tavy
Posts: 364
Joined: Thu Jul 01, 2004 12:29 pm

Re: Limit the value of an element based on existing XML data

Post 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
Octavian Nadolu
<oXygen/> XML Editor
http://www.oxygenxml.com
daddyjones
Posts: 9
Joined: Wed Apr 11, 2018 4:43 pm

Re: Limit the value of an element based on existing XML data

Post 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?
daddyjones
Posts: 9
Joined: Wed Apr 11, 2018 4:43 pm

Re: Limit the value of an element based on existing XML data

Post by daddyjones »

Could XPointer be used to achieve this?
daddyjones
Posts: 9
Joined: Wed Apr 11, 2018 4:43 pm

Re: Limit the value of an element based on existing XML data

Post 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...
Radu
Posts: 9041
Joined: Fri Jul 09, 2004 5:18 pm

Re: Limit the value of an element based on existing XML data

Post by Radu »

Hi,

I second Octavian's post that Schematron is the only way to do this.

Regards,
Radu
Radu Coravu
<oXygen/> XML Editor
http://www.oxygenxml.com
Post Reply