Search code examples
javaxmlxsdxsd-validationxsd-1.1

XSD attribute-value based regex-validation in XSD 1.1 assert


I have and XML snippet like this. I need to do an XSD validation such that, for example: if the field attribute 'name' is port, then it has to match a regex for whether it is a number (like '\p{Nd}+')

<Data>
    <Attributes>
        <Field name="nodeType">abcd</Field>
        <Field name="port">5462</Field>
    </Attributes>
</Data>

In my XSD , I'm using an assert as shown below

<xs:element name="Field" maxOccurs="unbounded" minOccurs="0">
                                <xs:complexType>
                                  <xs:simpleContent>
                                    <xs:extension base="xs:string">
                                      <xs:attribute type="xs:string" name="name" use="optional"/>
                                      <xs:assert test="if(@name='port') then matches($value,'\p{Nd}+') else false())"/>
                                    </xs:extension>
                                  </xs:simpleContent>
                                </xs:complexType>
                              </xs:element>

But, when I run, I am getting the following Exception thrown

xml is NOT valid reason:org.xml.sax.SAXParseException;cvc-xpath.3.13.4.2a: XPST0003 - Assertion XPath expression ('if(@name='port') then matches($value,'\p{Nd}+') else false())') on the schema type '#AnonType_FieldAttributesDataContainerRtms' couldn't compile successfully

Solution

  • You've got a stray closing bracket:

    false())
           ^
    

    Saxon error message for comparison:

    Error at xs:assert on line 7 column 102 of test.xsd:
      Unexpected token ")" beyond end of expression