Xsd -xml regex validation not working for me using JAXB

I wrote a simple XSD where I wrote the following XSD template

<xsd:element name="xx"> <xsd:simpleType > <xsd:restriction base="xsd:string"> <xsd:pattern value="^[az]+"> </xsd:pattern> </xsd:restriction> </xsd:simpleType> </xsd:element> 

When I perform XML-XSD validation using jaxb, the XX tag error always occurs. eg:

 <xx>abcd</xx> 

I don’t know why this is strange behavior? I hope that my regular expression is correct, and the above example should pass the test. Did I miss something?

+4
source share
1 answer

You need to remove the anchor ^ from the expression. XSD validators do not recognize ^ or $ anchors, but interpret them as alphabetic characters. You can verify this by changing the XML to <xx>^abcd</xx> and checking against your current XSD.

The reason for this is because your template is automatically bound to the beginning and end of the line. Unlike regular expressions in which you are trying to match a value, this regular expression should check the entire contents of the element, so you need to create a template to fully match everything that appears in the field.

+6
source

Source: https://habr.com/ru/post/1479617/


All Articles