Define an item as non-empty in RelaxNG

I started using RelaxNG to specify XML message schemas and using PHP DOMDocument to check and parse incoming messages, but I can’t figure out how to define node text so it cannot be empty. Example circuit:

<?xml version="1.0"?>
<element name="amhAPI" xmlns="http://relaxng.org/ns/structure/1.0">
    <element name="auth">
        <element name="validateUser">
            <element name="username">
                <text/>
            </element>

            <element name="password">
                <text/>
            </element>
        </element>
    </element>
</element>

However, the message below is checked using the DOMDocument :: relaxNGValidate method (since relaxng matches any arbitrary string [including the empty one] with the template text) and is equivalent):

<?xml version="1.0"?>
<amhAPI>
    <auth>
        <validateUser>
            <username/>
            <password/>
        </validateUser>
    </auth>
</amhAPI>

Because of this, I need to add a bunch of checks and checks for fields that should not be empty, which can be removed if the validator identified them as non-empty elements.

Is there a way to force non-blank text?

+3
source share
3

RELAX NG XSD ( ), :

<?xml version="1.0"?>
<element name="amhAPI" xmlns="http://relaxng.org/ns/structure/1.0"
  datatypeLibrary="http://www.w3.org/2001/XMLSchema-datatypes">
  <element name="auth">
    <element name="validateUser">
      <element name="username">
        <data type="string">
          <param name="pattern">.+</param>
        </data>
      </element>
      <element name="password">
        <data type="string">
          <param name="pattern">.+</param>
        </data>
      </element>
    </element>
  </element>
</element>
+4

. minLength "1", ( ). .*[\S]+.*, , " " "" (. ).

, ( ) - : (.|\n|\r)*\S(.|\n|\r)*, , .

+10

Alternatively, use minLengthseems more direct and clean than regular expressions. (This also requires XSD data types.)

<element name="amhAPI" xmlns="http://relaxng.org/ns/structure/1.0"
  datatypeLibrary="http://www.w3.org/2001/XMLSchema-datatypes">
  <element name="auth">
    <element name="validateUser">
      <element name="username">
        <data type="string">
          <param name="minLength">1</param>
        </data>
      </element>
      <element name="password">
        <data type="string">
          <param name="minLength">1</param>
        </data>
      </element>
    </element>
  </element>
</element>
0
source

All Articles