Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

XSD defining element whose text matches a pattern and has attributes

Status
Not open for further replies.

nwruiz

Programmer
Jun 22, 2005
60
US
I am attempting to define an attribute within my XSD that can represent something like the following:

Code:
<EnrollmentNumber alias="Account Number">0013423521</EnrollmentNumber>

This almost does the trick:
Code:
<xs:element name="EnrollmentNumber">
    <xs:complexType>
      <xs:simpleContent>
        <xs:extension base="xs:string">
          <xs:attribute ref="alias" default="Enrollment Number"/>
        </xs:extension>
      </xs:simpleContent>
    </xs:complexType>
  </xs:element>

However, I would like to require that the enrollment number matches the following restriction:

Code:
<xs:element name="EnrollmentNumber">
    <xs:simpleType>
      <xs:restriction base="xs:string">
        <xs:pattern value="\d{10}"/>
      </xs:restriction>
    </xs:simpleType>
  </xs:element>

Is there a way to combine these two definitions to create a "childless" element that meets this restriction and has an attribute? Thanks for your time.

Nick Ruiz
 
The neat way to do it is to define a simpleType apart.
[tt]
<xs:element name="EnrollmentNumber">
<xs:complexType>
<xs:simpleContent>
<xs:extension base="[red]EnrollmentNumberType[/red]">
<xs:attribute ref="alias" default="Enrollment Number"/>
</xs:extension>
</xs:simpleContent>
</xs:complexType>
</xs:element>

<xs:simpleType [blue]name="EnrollmentNumberType"[/blue]>
<xs:restriction base="xs:string">
<xs:pattern value="\d{10}"/>
</xs:restriction>
</xs:simpleType>
[/tt]
ps: I am not going to scrutinize the line on the <xs:attribute> because it involves data not show and I suppose it is correctly done.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top