XML to be validated against multiple xsd schemas

前端 未结 4 1945
-上瘾入骨i
-上瘾入骨i 2020-12-10 08:46

I\'m writing the xsd and the code to validate, so I have great control here.

I would like to have an upload facility that adds stuff to my application based on an xm

相关标签:
4条回答
  • 2020-12-10 08:55

    Take a look at NVDL (Namespace-based Validation Dispatching Language) - http://www.nvdl.org/

    It is designed to do what you want to do (validate parts of an XML document that have their own namespaces and schemas).

    There is a tutorial here - http://www.dpawson.co.uk/nvdl/ - and a Java implementation here - http://jnvdl.sourceforge.net/

    Hope that helps! Kevin

    0 讨论(0)
  • 2020-12-10 09:00

    You can also use a "resource resolver" to allow "xml authors" to specify their own schema file, at least to some extent, ex: https://stackoverflow.com/a/41225329/32453 at the end of the day, you want a fully compliant xml file that can be validatable with normal tools, anyway :)

    0 讨论(0)
  • 2020-12-10 09:11

    You need to define a target namespace for each separately-validated portions of the instance document. Then you define a master schema that uses <xsd:include> to reference the schema documents for these components.

    The limitation with this approach is that you can't let the individual components define the schemas that should be used to validate them. But it's a bad idea in general to let a document tell you how to validate it (ie, validation should something that your application controls).

    0 讨论(0)
  • 2020-12-10 09:13

    I finally figured this out.

    First of all, in the foo schema, the bar-config and baz-config elements have a type which includes an any element, like this:

    <sequence>
        <any minOccurs="0" maxOccurs="1"
            processContents="lax" namespace="##any" />
    </sequence>
    

    In the xml, then, you must specify the proper namespace using the xmlns attribute on the child element of bar-config or baz-config, like this:

    <bar-config>
        <config xmlns="http://www.example.org/bar/Alpha">
            ... config xml here ...
        </config>
    </bar-config>
    

    Then, your XML schema file for bar Alpha will have a target namespace of http://www.example.org/bar/Alpha and will define the root element config.

    If your XML file has namespace declarations and schema locations for both of the schema files, this is sufficient for the editor to do all of the validating (at least good enough for Eclipse).

    So far, we have satisfied the requirement that the xml author may write the xml in such a way that it is validated in the editor.

    Now, we need the consumer to be able to validate. In my case, I'm using Java.

    If by some chance, you know the schema files that you will need to use to validate ahead of time, then you simply create a single Schema object and validate as usual, like this:

    Schema schema = factory().newSchema(new Source[] {
        new StreamSource(stream("foo.xsd")),
        new StreamSource(stream("Alpha.xsd")),
        new StreamSource(stream("Mercury.xsd")),
    });
    

    In this case, however, we don't know which xsd files to use until we have parsed the main document. So, the general procedure is to:

    1. Validate the xml using only the main (foo) schema
    2. Determine the schema to use to validate the portion of the document
    3. Find the node that is the root of the portion to validate using a separate schema
    4. Import that node into a brand new document
    5. Validate the brand new document using the other schema file

    Caveat: it appears that the document must be built namespace-aware in order for this to work.

    Here's some code (this was ripped from various places of my code, so there might be some errors introduced by the copy-and-paste):

    // Contains the filename of the xml file
    String filename;
    
    // Load the xml data using a namespace-aware builder (the method 
    // 'stream' simply opens an input stream on a file)
    Document document;
    DocumentBuilderFactory docBuilderFactory =
        DocumentBuilderFactory.newInstance();
    docBuilderFactory.setNamespaceAware(true);
    document = docBuilderFactory.newDocumentBuilder().parse(stream(filename));
    
    // Create the schema factory
    SchemaFactory sFactory = SchemaFactory.newInstance(
        XMLConstants.W3C_XML_SCHEMA_NS_URI);
    
    // Load the main schema
    Schema schema = sFactory.newSchema(
        new StreamSource(stream("foo.xsd")));
    
    // Validate using main schema
    schema.newValidator().validate(new DOMSource(document));
    
    // Get the node that is the root for the portion you want to validate
    // using another schema
    Node node= getSpecialNode(document);
    
    // Build a Document from that node
    Document subDocument = docBuilderFactory.newDocumentBuilder().newDocument();
    subDocument.appendChild(subDocument.importNode(node, true));
    
    // Determine the schema to use using your own logic
    Schema subSchema = parseAndDetermineSchema(document);
    
    // Validate using other schema
    subSchema.newValidator().validate(new DOMSource(subDocument));
    
    0 讨论(0)
提交回复
热议问题