You are viewing a plain text version of this content. The canonical link for it is here.
Posted to j-dev@xerces.apache.org by ds...@mmm.com on 2002/05/06 20:39:59 UTC

Basic Schema Validation..HELP!

I am converting a webservice from a MS COM+ based application to Java.  Out
public interface(s) methods are all defined with an argument of String.
That String is (or should be) a valid XML document.  What I want to do is
load this string into a DOM document (or SAX could be fine) and validate it
against a given Schema document....However my incoming document (string)
does NOT have the xsi:location attribute...What I want to do is decide at
RUN time which Schema to validate this "document" against....load the
schema, load the document and call some method to say "validate this
document against this schema"....Does such a thing exist?


thanks
Doug

example of xml document

<?xml version="1.0" encoding="UTF-8" ?>
<request xmlns="mesapi/schemas/version1.0/getsomedata" server="s" database
="d">
        <facility_code>BF</facility_code>
        <id_nbr>32100000999</id_nbr>
    </request>

  Schema document...

  <?xml version="1.0" ?>
    - <xsd:schema targetNamespace="mesapi/schemas/version1.0/getsomedata"
    elementFormDefault="qualified" xmlns:src="
    mesapi/schemas/version1.0/getsomedata" xmlns:xsd="
    http://www.w3.org/2001/XMLSchema">
        <xsd:element name="request" type="src:requesttype" />
      - <xsd:attributeGroup name="connections">
          <xsd:attribute name="server" type="xsd:string" use="required" />
          <xsd:attribute name="database" type="xsd:string" use="required"
        />
      </xsd:attributeGroup>
      - <xsd:complexType name="requesttype">
        - <xsd:sequence>
          - <xsd:element name="facility_code">
            - <xsd:simpleType>
              - <xsd:restriction base="xsd:string">
                  <xsd:maxLength value="8" />
              </xsd:restriction>
            </xsd:simpleType>
          </xsd:element>
          - <xsd:element name="id_nbr">
            - <xsd:simpleType>
              - <xsd:restriction base="xsd:string">
                  <xsd:maxLength value="11" />
              </xsd:restriction>
            </xsd:simpleType>
          </xsd:element>
        </xsd:sequence>
          <xsd:attributeGroup ref="src:connections" />
      </xsd:complexType>
    </xsd:schema>


---------------------------------------------------------------------
To unsubscribe, e-mail: xerces-j-dev-unsubscribe@xml.apache.org
For additional commands, e-mail: xerces-j-dev-help@xml.apache.org


Re: Basic Schema Validation..HELP!

Posted by Andy Clark <an...@apache.org>.
dswanson1@mmm.com wrote:
> does NOT have the xsi:location attribute...What I want to do is decide at
> RUN time which Schema to validate this "document" against....load the
> schema, load the document and call some method to say "validate this
> document against this schema"....Does such a thing exist?

Check out the documentation for the properties supported by
Xerces, located at the following URL:

  http://xml.apache.org/xerces2-j/properties.html

The property identifiers that will probably help you out are
the following:

  http://apache.org/xml/properties/schema/external-schemaLocation
 
http://apache.org/xml/properties/schema/external-noNamespaceSchemaLocation

You might also want to consider using a custom EntityResolver 
to do the redirection from the schema specified in the document.
Since the xsi: attributes that point to the location of the
XML Schema are merely "hints", they are not strictly required
(and your documents don't have them anyway). In this case,
Xerces uses the namespace URI as a way to try to locate the
associated XML Schema grammar -- hence the use of a custom
EntityResolver.

>     - <xsd:schema targetNamespace="mesapi/schemas/version1.0/getsomedata"

Namespace URIs should NOT be relative. You should ALWAYS try
to use absolute URIs when defining a namespace. While not
strictly disallowed, relative URIs are strongly discouraged
and are not considered a "best practice".

-- 
Andy Clark * andyc@apache.org

---------------------------------------------------------------------
To unsubscribe, e-mail: xerces-j-dev-unsubscribe@xml.apache.org
For additional commands, e-mail: xerces-j-dev-help@xml.apache.org


RE: Basic Schema Validation..HELP!

Posted by Mark Lines-Davies <ml...@spd.sonybpe.com>.
Doug

I don't know if this is any help, but you can tell your DOMParser where to
find your schema by setting the following before you parse your document:

      setFeature("http://xml.org/sax/features/validation", true);
      setFeature("http://apache.org/xml/features/validation/schema", true);

setProperty("http://apache.org/xml/properties/schema/external-noNamespaceSch
emaLocation", "my.xsd");

If you have already loaded your schema into a DOM (and so don't want to
provide a location) you can create an EntityResolver and add it via
setEntityResolver. Then when it needs the schema the DOMParser will call
your EntityResolver's resolveEntity method. You can then provide the schema
from its DOM, or wherever you want.

regards

Mark Lines-Davies

-----Original Message-----
From: dswanson1@mmm.com [mailto:dswanson1@mmm.com]
Sent: 06 May 2002 19:40
To: xerces-j-dev@xml.apache.org
Subject: Basic Schema Validation..HELP!


I am converting a webservice from a MS COM+ based application to Java.  Out
public interface(s) methods are all defined with an argument of String.
That String is (or should be) a valid XML document.  What I want to do is
load this string into a DOM document (or SAX could be fine) and validate it
against a given Schema document....However my incoming document (string)
does NOT have the xsi:location attribute...What I want to do is decide at
RUN time which Schema to validate this "document" against....load the
schema, load the document and call some method to say "validate this
document against this schema"....Does such a thing exist?


thanks
Doug

example of xml document

<?xml version="1.0" encoding="UTF-8" ?>
<request xmlns="mesapi/schemas/version1.0/getsomedata" server="s" database
="d">
        <facility_code>BF</facility_code>
        <id_nbr>32100000999</id_nbr>
    </request>

  Schema document...

  <?xml version="1.0" ?>
    - <xsd:schema targetNamespace="mesapi/schemas/version1.0/getsomedata"
    elementFormDefault="qualified" xmlns:src="
    mesapi/schemas/version1.0/getsomedata" xmlns:xsd="
    http://www.w3.org/2001/XMLSchema">
        <xsd:element name="request" type="src:requesttype" />
      - <xsd:attributeGroup name="connections">
          <xsd:attribute name="server" type="xsd:string" use="required" />
          <xsd:attribute name="database" type="xsd:string" use="required"
        />
      </xsd:attributeGroup>
      - <xsd:complexType name="requesttype">
        - <xsd:sequence>
          - <xsd:element name="facility_code">
            - <xsd:simpleType>
              - <xsd:restriction base="xsd:string">
                  <xsd:maxLength value="8" />
              </xsd:restriction>
            </xsd:simpleType>
          </xsd:element>
          - <xsd:element name="id_nbr">
            - <xsd:simpleType>
              - <xsd:restriction base="xsd:string">
                  <xsd:maxLength value="11" />
              </xsd:restriction>
            </xsd:simpleType>
          </xsd:element>
        </xsd:sequence>
          <xsd:attributeGroup ref="src:connections" />
      </xsd:complexType>
    </xsd:schema>


---------------------------------------------------------------------
To unsubscribe, e-mail: xerces-j-dev-unsubscribe@xml.apache.org
For additional commands, e-mail: xerces-j-dev-help@xml.apache.org




*************************************************************************
The information contained in this message or any of its
attachments may be privileged and confidential and intended 
for the exclusive use of the addressee. If you are not the
addressee any disclosure, reproduction, distribution or other
dissemination or use of this communication is strictly prohibited
*************************************************************************

---------------------------------------------------------------------
To unsubscribe, e-mail: xerces-j-dev-unsubscribe@xml.apache.org
For additional commands, e-mail: xerces-j-dev-help@xml.apache.org