Page 1 of 1

DOMDocument::schemaValidate(): Problems with Regex patterns

Posted: Sun Oct 04, 2009 2:11 am
by jeff00seattle
I am having problems with function DOMDocument::schemaValidate() because it is not properly evaluating Regex patterns, even though they are correct.

I have validated my XML/XSD through Eclipse tools and this site with no problems:
http://tools.decisionsoft.com/schemaVal ... esults.jsp

Taking a simple example of a Regex pattern: "\/[a-zA-Z0-9_\-\+]+", schemaValidate() is failing on the initial forward-slash reference: "\/".

This Regex is inserted into this simpleType element of an XML Schema:

Code: Select all

<xs:simpleType name="patternUrlPath">
  <xs:restriction base="xs:string">
    <xs:pattern
      [color=#0000FF][b]value="\/[a-zA-Z0-9_\-\+]+"[/b][/color] >
    </xs:pattern>
  </xs:restriction>
</xs:simpleType>
When I run this through DOMDocument::schemaValidate(), it fails and libxml_get_errors() returns:
Error 1756: Element '{http://www.w3.org/2001/XMLSchema}pattern': The value '\/[a-zA-Z0-9_\-\+]+' of the facet 'pattern' is not a valid regular expression.
If I modify the Regex to be "\\/[a-zA-Z0-9_\-\+]+", whereby I add an additional back-slash before the initial forward-slash reference like so: "\\/", then schemaValidate() passes but this is not the Regex pattern I want.

What I am doing wrong or is there a better PHP class in validating XML/XSD?

Thanks

Jeff in Seattle

Re: DOMDocument::schemaValidate(): Problems with Regex patterns

Posted: Sun Oct 04, 2009 3:11 am
by requinix
If you want the / literally then you don't need a backslash.
If you want the \/ literally then you need to escape the \ with another \.

Re: DOMDocument::schemaValidate(): Problems with Regex patterns

Posted: Sun Oct 04, 2009 3:32 am
by jeff00seattle
Wow, thanks!

My head was so bent that a forward-slash had to be backed with a back-slash.