Nick
Nick

Reputation: 147

xsd: Enforce single occurrence of attibute value in set of elements

I'm new in the field of xsd files and stranded with a question. I have an xml file like this:

<config version="1.6">
  <properties>
    <parameter name="path">Hello</parameter>
    <parameter name="name">World</parameter>
    <parameter name="run">13</parameter>
    <parameter name="rate">37</parameter>
  </properties>
</config>

I already created a xsd to validate the list of parameters with the 'name' attribute:

<xs:element name="config" type="configType"/>

<xs:complexType name="parameterType">
  <xs:simpleContent>
    <xs:extension base="xs:string">
      <xs:attribute type="xs:string" name="name"/>
    </xs:extension>
   </xs:simpleContent>
</xs:complexType>

<xs:complexType name="propertiesType">
  <xs:sequence>
    <xs:element type="parameterType" name="parameter" maxOccurs="unbounded" minOccurs="0"/>
 </xs:sequence>
</xs:complexType>
  
<xs:complexType name="configType">
  <xs:sequence>
    <xs:element type="propertiesType" name="properties"/>
  </xs:sequence>
  <xs:attribute type="xs:float" name="version"/>
</xs:complexType>

What I need in addition, is to ensure that exactly one parameter element with the attibute name="path" exists. Unfortunately can not change the input xml.

Do you have any ideas?

EDIT: Removed xsd 1.1 tag, due to your tool "XML Check" not supporting xsd 1.1

Upvotes: 0

Views: 38

Answers (1)

Michael Kay
Michael Kay

Reputation: 163665

If you're using XSD 1.1 as the tag suggests then it's easy:

<xs:assert test="count(parameter[@name='path'])=1"/>

Upvotes: 1

Related Questions