user1185461
user1185461

Reputation: 11

How do I defined an XSD attribute name as a regular expression?

The XML allows a variable number of attributes of Q followed by a number, like this

<recognitionPattern Q0="12.5" Q1="12.5" Q2="12.5" Q3="12.5" Q4="12.5" Q5="12.5" Q6="12.5" Q7="12.5"/>

My current solution looks something like this:

<xs:complexType name="PerQuarterDoubleHack"> <!-- YUCK! /> -->
<xs:attribute name="Q0"                 type="xs:double"       />
<xs:attribute name="Q1"                 type="xs:double"       />
<xs:attribute name="Q2"                 type="xs:double"       />
<xs:attribute name="Q3"                 type="xs:double"       />
<xs:attribute name="Q4"                 type="xs:double"       />
<xs:attribute name="Q5"                 type="xs:double"       />
<xs:attribute name="Q6"                 type="xs:double"       />
<xs:attribute name="Q7"                 type="xs:double"       />
</xs:complexType>

I've seen lots of regular expressions applied to the value, but never an attribute name. Do I have any choice than changing the XML?

Upvotes: 1

Views: 667

Answers (2)

Jo&#227;o Silva
Jo&#227;o Silva

Reputation: 91329

XML Schema doesn't allow you to apply any validation to an attribute name, only its value. Your best bet is to change the XML, if possible, to something along these lines:

<recognitionPattern>
    <quarter id="0">12.55</quarter> <!-- equivalent to Q0 -->
    <quarter id="1">12.55</quarter> <!-- equivalent to Q1 -->
    <quarter id="2">12.55</quarter> <!-- equivalent to Q2 -->
    <quarter id="3">12.55</quarter> <!-- equivalent to Q3 -->
</recognitionPattern>

Upvotes: 1

Michael Kay
Michael Kay

Reputation: 163352

It's a very poor XML design. Generally, XML processing tools assume that the application knows all the element and attribute names statically, and if this isn't the case, writing applications will be difficult. Change the design if you have the chance.

Upvotes: 0

Related Questions