XSD
<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">

<xsd:simpleType name="MgmtCodetyp">
    <xsd:restriction base="xsd:string">
         <xsd:pattern value="[A-Z][A-Z]([A-Z]|[0-9])"></xsd:pattern>
    </xsd:restriction>
</xsd:simpleType>

<xsd:element name="MgmtCode" type="MgmtCodetyp"></xsd:element>

</xsd:schema>

XML
<MgmtCode>AGF</MgmtCode>

PHP Code
<?php

    $file = "data.xml";

    function libxml_display_error($error) {

            $return = "\n";

            switch ($error->level) {
                    case LIBXML_ERR_ERROR:
                            $return .= "[ERROR] ";
                    break;
                    case LIBXML_ERR_FATAL:
                            $return .= "[FATAL] ";
                    break;
                    default:
                            $return .= "[UNKNOWN] ";
            }

            $return .= trim($error->message);
            $return .= " [line: $error->line]\n";

            return $return;
    }

    function libxml_display_errors() {

            $errors = libxml_get_errors();

            foreach ($errors as $error) {
                    if ($error -> level != LIBXML_ERR_WARNING) {
                            print libxml_display_error($error);
                    }
            }

            libxml_clear_errors();
    }

    // Enable user error handling
    libxml_use_internal_errors(true);

    $xml = new DOMDocument;
    $xml -> load("kkk.xml");
    if (!$xml -> schemaValidate("kkk.xsd")) {

            libxml_display_errors();
    }
    else {

            print "no error\n";
    }

?>

OUTPUT

[ERROR] Element 'MgmtCode': [facet 'pattern'] The value 'AGF' is not accepted by the pattern '[A-Z]A-Z'. [line: 1]

[ERROR] Element 'MgmtCode': 'AGF' is not a valid value of the atomic type 'MgmtCodetyp'. [line: 1]

ANALYSIS

The XML is a perfectly valid xml. I don't understand why it failed the XSD.

    Any difference if you make the pattern tag self-closing?:

    <xsd:pattern value="[A-Z][A-Z]([A-Z]|[0-9])" />
    

      Same error even with the self-closed tag..

      XSD
      <?xml version="1.0" encoding="UTF-8"?>
      <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">

      <xsd:simpleType name="MgmtCodetyp">
          <xsd:restriction base="xsd:string">
             [B]  <xsd:pattern value="[A-Z][A-Z]([A-Z]|[0-9])" />[/B]
          </xsd:restriction>
      </xsd:simpleType>
      
      <xsd:element name="MgmtCode" type="MgmtCodetyp"></xsd:element>

      </xsd:schema>

      [host] php xml.php

      [ERROR] Element 'MgmtCode': [facet 'pattern'] The value 'AGF' is not accepted by the pattern '[A-Z]A-Z'. [line: 1]

      [ERROR] Element 'MgmtCode': 'AGF' is not a valid value of the atomic type 'MgmtCodetyp'. [line: 1]

        Well, sure looks like it should work. Just for the fun(?) of it, any difference if you allow for anything before/after it, just in case you're picking up some white space or something? Also, you could simplify it a little by using one character class instead of the either/or for the 3rd character:

        \s*[A-Z][A-Z][A-Z0-9]\s*
        

          Interesting found....

          I changed the xsd as follow:

          <xsd:pattern value="[A-Z]|[0-9]" />

          It worked perfectly with the following two scenarios...

          <MgmtCode>F</MgmtCode>

          <MgmtCode>9</MgmtCode>

          But If I added a brackets () in the xsd as follow:

          <xsd:pattern value="([A-Z]|[0-9])" />

          <MgmtCode>9</MgmtCode> <-- PASSED

          <MgmtCode>F</MgmtCode> <-- FAILED

          [ERROR] Element 'MgmtCode': [facet 'pattern'] The value 'F' is not accepted by the pattern '([A-Z]|[0-9])'. [line: 1]

          [ERROR] Element 'MgmtCode': 'F' is not a valid value of the atomic type 'MgmtCodetyp'. [line: 1]

            I have no idea why that would be, but you can use [FONT=Courier New][A-Z0-9][/FONT] as a functional (and shorter) equivalent of FONT=Courier New[/FONT].

              Incidentally, if you want to avoid your posts being peppered with smileys, you'd be well served by using the [noparse]

              [/noparse] formatting tag to format your code.
                Write a Reply...