Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8220818

Validator does not find missing match for keyref error

XMLWordPrintable

    • b36
    • 9
    • b19
    • x86_64
    • windows_10

      A DESCRIPTION OF THE PROBLEM :
      The Java schema validator does not find an error in an invalid XML file.

      Output of Validator of XML plugin of Notepad++:
      ERROR: Element '{http://www.example.org/root}CollectionMember': No match found for key-sequence ['object1'] of keyref '{http://www.example.org/root}Collectiontype_defined'.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      1. Compile attached program
      2. Place attached XSD and XML in same folder as compiled program
      3 run program: java Test schema.xsd invalid.xml

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      An execption should be printed since the referenced type ("object1") is missing in "invalid.xml" (see "valid.xml" for a valid file).
      ACTUAL -
      No exception occurrs.

      ---------- BEGIN SOURCE ----------
      -------------------- start of Test.java --------------------
      package org.example;

      import java.io.File;

      import javax.xml.XMLConstants;
      import javax.xml.transform.stream.StreamSource;
      import javax.xml.validation.Schema;
      import javax.xml.validation.SchemaFactory;
      import javax.xml.validation.Validator;

      import org.xml.sax.SAXException;

      public class Test {

          public static void main(final String... args) throws SAXException {

              if (args.length != 2) {
                  System.out.println("Usage: java Test schema.xml filetovalidate.xml");
                  return;
              }

              final SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
              final Schema schema = schemaFactory.newSchema(new File(args[0]));

              final Validator validator = schema.newValidator();

              try {
                  validator.validate(new StreamSource(new File(args[1])));
                  System.out.println("No problems found in XML.");
              } catch (Exception e) {
                  System.out.println("Problems occurred:");
                  e.printStackTrace();
              }
          }

      }
      -------------------- end of Test.java --------------------

      -------------------- start of schema.xsd --------------------
      <?xml version="1.0" encoding="UTF-8"?>
      <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
      xmlns:example="http://www.example.org/root"
      xmlns="http://www.example.org/root"
      targetNamespace="http://www.example.org/root"
      elementFormDefault="qualified" attributeFormDefault="unqualified">
      <xs:element name="Root">
      <xs:complexType>
      <xs:choice maxOccurs="unbounded">
      <xs:element ref="Object" />
      </xs:choice>
      </xs:complexType>
      <xs:key name="name_key">
      <xs:selector xpath=".//example:Object" />
      <xs:field xpath="@name" />
      </xs:key>
      <xs:keyref name="Collectiontype_defined" refer="name_key">
      <xs:selector xpath=".//example:CollectionMember" />
      <xs:field xpath="@type" />
      </xs:keyref>
      </xs:element>
      <xs:element name="Members">
      <xs:complexType>
      <xs:group ref="example:memberTypes" minOccurs="0"
      maxOccurs="unbounded" />
      </xs:complexType>
      </xs:element>
      <xs:element name="Object">
      <xs:complexType>
      <xs:sequence>
      <xs:element ref="Members" minOccurs="0" />
      </xs:sequence>
      <xs:attributeGroup ref="example:typedMemberAttributes" />
      <xs:attributeGroup ref="example:namedMemberAttributes" />
      </xs:complexType>
      <xs:unique name="ObjectElementNameUniqueness">
      <xs:selector xpath="example:Members/*" />
      <xs:field xpath="@name" />
      </xs:unique>
      </xs:element>
      <xs:element name="Collection">
      <xs:complexType>
      <xs:sequence>
      <xs:element ref="CollectionMembers" />
      </xs:sequence>
      <xs:attributeGroup ref="example:namedMemberAttributes" />
      </xs:complexType>
      </xs:element>
      <xs:element name="CollectionMembers">
      <xs:complexType>
      <xs:sequence>
      <xs:element ref="CollectionMember" minOccurs="2"
      maxOccurs="unbounded" />
      </xs:sequence>
      </xs:complexType>
      </xs:element>
      <xs:element name="CollectionMember">
      <xs:complexType>
      <xs:sequence>
      <xs:element ref="Members" minOccurs="0"
      maxOccurs="unbounded" />
      </xs:sequence>
      <xs:attributeGroup ref="example:typedMemberAttributes" />
      <xs:attributeGroup ref="example:namedMemberAttributes" />
      </xs:complexType>
      </xs:element>
      <xs:element name="String">
      <xs:complexType>
      <xs:attributeGroup ref="example:namedMemberAttributes" />
      </xs:complexType>
      </xs:element>
      <xs:group name="memberTypes">
      <xs:choice>
      <xs:element ref="String" />
      <xs:element ref="Collection" />
      </xs:choice>
      </xs:group>
      <xs:attributeGroup name="namedMemberAttributes">
      <xs:attribute name="name" type="xs:Name" use="required" />
      </xs:attributeGroup>
      <xs:attributeGroup name="typedMemberAttributes">
      <xs:attribute name="type" type="xs:Name" />
      </xs:attributeGroup>
      </xs:schema>
      -------------------- start of schema.xsd --------------------

      -------------------- start of invalid.xml --------------------
      <?xml version="1.0" encoding="UTF-8" standalone="no"?>
      <Root xmlns="http://www.example.org/root">

      <Object name="object2">
      <Members>
      <Collection name="Collection1">
      <CollectionMembers>
      <CollectionMember name="Collectionmember1" type="object1" />
      <CollectionMember name="Collectionmember2" >
      <Members>
      <String name="string1" />
      </Members>
      </CollectionMember>
      </CollectionMembers>
      </Collection>
      </Members>
      </Object>
      </Root>
      -------------------- end of invalid.xml --------------------

      -------------------- start of valid.xml --------------------
      <?xml version="1.0" encoding="UTF-8" standalone="no"?>
      <Root xmlns="http://www.example.org/root">

      <Object name="object1">
      <Members>
      <String name="string1" />
      </Members>
      </Object>

      <Object name="object2">
      <Members>
      <Collection name="Collection1">
      <CollectionMembers>
      <CollectionMember name="Collectionmember1" type="object1" />
      <CollectionMember name="Collectionmember2" >
      <Members>
      <String name="string1" />
      </Members>
      </CollectionMember>
      </CollectionMembers>
      </Collection>
      </Members>
      </Object>
      </Root>
      -------------------- end of valid.xml --------------------

      ---------- END SOURCE ----------

      FREQUENCY : always


        1. invalid.xml
          0.4 kB
        2. JI9059775.java
          0.9 kB
        3. schema.xsd
          2 kB
        4. valid.xml
          0.5 kB

            joehw Joe Wang
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: