3

I have an attribute that can be any string, but if it has starting and ending brackets "^[.*]$" - it must be only one of the following specific values:

"[EVENT]"  

and

"[PROTOCOL]"

So, "[EVENT]", "[PROTOCOL]", "SomeString" - are correct, but "[SomeString]" - isn't.

How can I achieve this?

Maarten Bodewes
  • 90,524
  • 13
  • 150
  • 263
lyres
  • 115
  • 7
  • Should `[SomeString` without the closing bracket be valid? – kjhughes Sep 25 '14 at 22:14
  • Actually, no. It shouldn't be valid. – lyres Sep 25 '14 at 22:35
  • And, I'm guessing that `SomeString]` without the opening bracket should also be invalid then, right? [My answer below](http://stackoverflow.com/a/26049756/290085) accepts and rejects the cases as listed in your question, plus rejects `"[SomeString"` and `"SomeString]"` too. – kjhughes Sep 26 '14 at 00:28

2 Answers2

3

Use an xs:simpleType and regular expressions to restrict the base xs:string type. You can have more than one xs:pattern to keep the alternative patterns simple. The element has to match one of the patterns or validation will fail. Since the patterns are regular expressions, special characters like "[" and "]" have to be escaped when used as literals.

XSD:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="root">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="elem" type="myElemType" maxOccurs="unbounded"/>
      </xs:sequence>
    </xs:complexType>
  </xs:element>

  <xs:complexType name="myElemType">
    <xs:attribute name="attrib" type="myAttribType"/>
  </xs:complexType>

  <xs:simpleType name="myAttribType">
    <xs:restriction base="xs:string">
      <xs:pattern value="\[EVENT\]"/><!-- "[EVENT]": okay -->
      <xs:pattern value="\[PROTOCOL\]"/><!-- "[PROTOCOL]": okay -->
      <xs:pattern value="[^\[].*"/><!-- Starts with anything but "[": okay -->
    </xs:restriction>
  </xs:simpleType>
</xs:schema>

XML:

<root>
  <elem attrib="[EVENT]"/>
  <elem attrib="[PROTOCOL]"/>
  <elem attrib="SomeString"/>
  <elem attrib="SomeString]"/>
  <elem attrib="   [SomeString]   "/>
  <!-- All the above are okay; the ones below fail validation -->
  <elem attrib="[SomeString]"/>
  <elem attrib="[SomeString"/>
</root>

Modify the regular expressions to your heart's content, e.g., to fail the example with leading and/or trailing spaces.

Edited to reflect OP's comment that "[SomeString" should also be invalid.

Burkart
  • 462
  • 4
  • 9
  • @lyres: Happy to help, and welcome to Stack Overflow. If this answer or any other one solved your issue, please mark it as accepted by clicking the green check mark. – Burkart Sep 25 '14 at 22:49
2

I like @Burkart's use of separate xs:pattern elements better (+1), but I had this pending while awaiting clarification in comments ("[SomeString" without a closing bracket should be invalid), so I'll post it anyway in case anyone might find it useful. Read the regular expression as EVENT or PROTOCOL within brackets or any string that doesn't start or end with a bracket.

<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="root">
    <xs:complexType>
      <xs:attribute name="attr">
        <xs:simpleType>
          <xs:restriction base="xs:string">
            <xs:pattern value="\[(EVENT|PROTOCOL)\]|[^\[].*[^\]]"/>
          </xs:restriction>
        </xs:simpleType>
      </xs:attribute>
    </xs:complexType>
  </xs:element>
</xs:schema>
kjhughes
  • 106,133
  • 27
  • 181
  • 240
  • Good idea! What about more complex case: actually I have not 2, but 10 "bracketed" variables ([EVENT], [PROTOCOL], [SESSION], etc...) Valid cases are: 1. Any string without brackets 2. Mentioned 10 "bracketed" variables 3. Any bracketed variable(s) concatenated with regular strings, for example: My[EVENT]_for_[PROTOCOL]@google.com Non-valid cases: 1. Any string inside brackets, except 10 predefined 2. The same with concatenations 3. Strings with only opening or closing bracket To be short - inside brackets can be only predefined variables – lyres Sep 26 '14 at 11:06
  • @lyes, I'd suggest you post a new question. Thanks. – kjhughes Sep 26 '14 at 11:47
  • Thanks, I posted new question here: http://stackoverflow.com/questions/26059526/xsdsimpletype-how-to-restrict-attribute-to-list-of-specific-values-with-concat – lyres Sep 26 '14 at 12:25