DOMDocument::schemaValidate(): Problems with Regex patterns

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
jeff00seattle
Forum Commoner
Posts: 66
Joined: Sat Feb 28, 2009 3:27 pm

DOMDocument::schemaValidate(): Problems with Regex patterns

Post 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
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

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

Post 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 \.
jeff00seattle
Forum Commoner
Posts: 66
Joined: Sat Feb 28, 2009 3:27 pm

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

Post by jeff00seattle »

Wow, thanks!

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