xml - Restriction on attributes depending on other attributes in XSD 1.1 -
in xml file "multiplechoice" node defined in following way:
<multiplechoice numberofchoices="" percentage="0"> text
corresponding needs of xsd schema, xsd definition of mentioned following one:
<xs:element name="multiplechoice" type="multiplechoicetype"/> <xs:complextype name="multiplechoicetype" mixed="true"> <xs:sequence> <xs:element minoccurs="0" maxoccurs="unbounded" ref="choice"/> </xs:sequence> <xs:attribute name="numberofchoices" type="xs:integer" use="required"/> <xs:attribute name="percentage" type="xs:integer" use="required"/> <xs:assert test="count(./choice) = @numberofchoices" /> </xs:complextype>
what need add restriction "percentage" attribute:
- if in "actor" attribute have string "me", "percentage" attribute has specified following syntax of point 2)
- there have many integers specified "numberofchoices" attribute, separated 1 white space.
for example: if "numberofchoices"="3" in "percentage" need 3 integers, separated 1 white space, example "percentage"= "30 40 30".
in case in "actor" attribute there else string "me", don't care what's happening in "numberofchoices" , "percentage" attributes.
i need "percentage" attribute required , need following situation accepted well:
<multiplechoice actor="" bar="" points="0" numberofchoices="3" percentage="">
since in "actor" attribute there not string "me" don't have check what's in "percentage" attribute. has there anyway.
thanks in advance!
first of all, in example percentage
xs:int
attribute, need change xs:int
list (and/or add regex if need 1 withespace between values).
then can use xpath tokenize function divide , count percentage value (example: tokenize('1 2 3 4 5', '\s') returns ('1', '2', '3', '4', '5').
example schema:
<?xml version="1.0" encoding="utf-8"?> <xs:schema xmlns:xs="http://www.w3.org/2001/xmlschema" elementformdefault="qualified" xmlns:vc="http://www.w3.org/2007/xmlschema-versioning" vc:minversion="1.1"> <xs:element name="multiplechoice" type="multiplechoicetype"/> <xs:complextype name="multiplechoicetype" mixed="true"> <xs:sequence> <xs:element minoccurs="0" maxoccurs="unbounded" name="choice" type="xs:string"/> </xs:sequence> <xs:attribute name="numberofchoices" type="xs:integer" use="required"/> <!-- percentage list of xs:int --> <xs:attribute name="percentage" use="required"> <xs:simpletype> <xs:list itemtype="xs:integer"/> </xs:simpletype> </xs:attribute> <!-- new actor attribute --> <xs:attribute name="actor" type="xs:string" use="required"/> <xs:assert test="count(./choice) = @numberofchoices" /> <!-- count needs satisfied if actor=me --> <xs:assert test="@actor != 'me' or count(tokenize(normalize-space(string(@percentage)),'\s')) = @numberofchoices"/> </xs:complextype> </xs:schema>
note i've used normalize-space xpath function function because ' 1 2 3'
valid xs:int
list (if want use regex instead).
Comments
Post a Comment