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

Limit the value of an element based on existing XML data

Thu May 03, 2018 4:34 pm

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: 153

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

Fri May 04, 2018 12:13 pm

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

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

Thu May 10, 2018 4:42 pm

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

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

Thu May 10, 2018 4:44 pm

Could XPointer be used to achieve this?
daddyjones
Posts: 9

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

Thu May 10, 2018 5:34 pm

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: 5816

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

Fri May 11, 2018 1:30 pm

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

Return to “XML Schemas”

Who is online

Users browsing this forum: No registered users and 0 guests