dantuch
dantuch

Reputation: 9293

validate element value with attribute value

<list count="3">foo bar baz</list>

Is there any way to check if list element contains 3 elements, as it's said in attribute count?

Upvotes: 1

Views: 42

Answers (1)

kjhughes
kjhughes

Reputation: 111716

XSD 1.0 alone cannot enforce this constraint, but XSD 1.1 can via xs:assert:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema attributeFormDefault="unqualified" 
           elementFormDefault="qualified" 
           xmlns:xs="http://www.w3.org/2001/XMLSchema"
           xmlns:vc="http://www.w3.org/2007/XMLSchema-versioning"
           vc:minVersion="1.1">

  <xs:element name="list">
    <xs:complexType>
    <xs:simpleContent>
      <xs:extension base="xs:string">
        <xs:attribute name="count" use="required" type="xs:int"/>        
        <xs:assert test="count(tokenize(., ' ')) = @count"/>
      </xs:extension>
    </xs:simpleContent>
    </xs:complexType>
  </xs:element>
</xs:schema>

Upvotes: 1

Related Questions