Commits

Nick Wellnhofer  committed 3a53a47

Make $schema->validate work with elements

This uses xmlSchemaValidateOneElement under the hood.

Fixes RT #93496:
https://rt.cpan.org/Ticket/Display.html?id=93496

  • Participants
  • Parent commits e722c79
  • Branches rt93496

Comments (0)

Files changed (3)

 Revision history for Perl extension XML::LibXML
 
 2.0112
+    - Make $schema->validate work with elements. This uses
+      xmlSchemaValidateOneElement under the hood.
+        - https://rt.cpan.org/Ticket/Display.html?id=93496
+        - Thanks to Jeremy Marshall for the report.
     - Fix https://rt.cpan.org/Ticket/Display.html?id=93429 .
         - Thanks to Nick Wellnhofer for the report and test.
 
 
 
 int
-validate( self, doc )
+validate( self, node )
         xmlSchemaPtr self
-        xmlDocPtr doc
+        xmlNodePtr node
     PREINIT:
         xmlSchemaValidCtxtPtr vctxt = NULL;
         PREINIT_SAVED_ERROR
     CODE:
         INIT_ERROR_HANDLER;
 
-        if (doc) {
-            PmmClearPSVI(doc);
-            PmmInvalidatePSVI(doc);
+        if (node->type == XML_DOCUMENT_NODE) {
+            PmmClearPSVI((xmlDocPtr)node);
+            PmmInvalidatePSVI((xmlDocPtr)node);
         }
         vctxt  = xmlSchemaNewValidCtxt( self );
         if ( vctxt == NULL ) {
                                   (xmlSchemaValidityWarningFunc)LibXML_error_handler_ctx,
                                   saved_error );
 
-        RETVAL = xmlSchemaValidateDoc( vctxt, doc );
+        if (node->type == XML_DOCUMENT_NODE) {
+            RETVAL = xmlSchemaValidateDoc(vctxt, (xmlDocPtr)node);
+        }
+        else {
+            RETVAL = xmlSchemaValidateOneElement(vctxt, node);
+        }
+
         xmlSchemaFreeValidCtxt( vctxt );
 
         CLEANUP_ERROR_HANDLER;

File t/26schema.t

 use XML::LibXML;
 
 if ( XML::LibXML::LIBXML_VERSION >= 20510 ) {
-    plan tests => 6;
+    plan tests => 7;
 }
 else {
     plan skip_all => 'No Schema Support compiled.';
     ok ( $@, 'Invalid file throws an excpetion.');
 }
 
+# 4 validate a node
+{
+    my $doc = $xmlparser->load_xml(string => <<'EOF');
+<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
+  <soap:Body>
+    <shiporder orderid="889923">
+      <orderperson>John Smith</orderperson>
+      <shipto>
+        <name>Ola Nordmann</name>
+      </shipto>
+    </shiporder>
+  </soap:Body>
+</soap:Envelope>
+EOF
+
+    my $schema = XML::LibXML::Schema->new(string => <<'EOF');
+<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
+  <xs:element name="shiporder">
+    <xs:complexType>
+      <xs:sequence>
+        <xs:element name="orderperson" type="xs:string"/>
+        <xs:element name="shipto">
+          <xs:complexType>
+            <xs:sequence>
+              <xs:element name="name" type="xs:string"/>
+            </xs:sequence>
+          </xs:complexType>
+        </xs:element>
+      </xs:sequence>
+      <xs:attribute name="orderid" type="xs:string" use="required"/>
+    </xs:complexType>
+  </xs:element>
+</xs:schema>
+EOF
+
+    my $nodelist = $doc->findnodes('/soap:Envelope/soap:Body/shiporder');
+    my $result = 1;
+    eval { $result = $schema->validate($nodelist->get_node(1)); };
+    # TEST
+    ok( $result == 0 && !$@, 'validate() works with elements.' );
+}
+