How to validate against multiple schemas

This should cover W3C XML Schema, Relax NG and DTD related problems.
AlexPetr
Posts: 5
Joined: Thu Mar 11, 2010 6:46 pm

How to validate against multiple schemas

Post by AlexPetr »

I have a problem:
configuration.xsd

Code: Select all

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified"
xmlns="http://schemas.com/configuration"
targetNamespace="http://schemas.com/configuration">
<xs:element name="root">
<xs:complexType>
<xs:sequence>
<xs:element name="param"/>
<xs:any namespace="##other" minOccurs="0"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
other.xsd

Code: Select all

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified"
xmlns="http://schemas.com/other" targetNamespace="http://schemas.com/other">
<xs:element name="other">
<xs:complexType>
<xs:attribute name="name"/>
</xs:complexType>
</xs:element>
</xs:schema>
configuration.xml

Code: Select all

<?xml version="1.0" encoding="UTF-8"?>
<root xmlns="http://schemas.com/configuration">
<param>sample</param>
<other xmlns="http://schemas.com/other" name="other_value"/>
</root>
configuration.xml validates normally in VisualStudio but i can not validate it in Oxygen.
How can i validate it?
adrian
Posts: 2850
Joined: Tue May 17, 2005 4:01 pm

Re: How to validate against multiple schemas

Post by adrian »

Hello,

In Oxygen you can use NVDL to validate against multiple schemas.

Here's a very simple NVDL for your two schemas(configuration.nvdl):

Code: Select all

<?xml version="1.0" encoding="UTF-8"?>
<rules xmlns="http://purl.oclc.org/dsdl/nvdl/ns/structure/1.0"
xmlns:a="http://relaxng.org/ns/compatibility/annotations/1.0">
<namespace ns="http://schemas.com/configuration">
<validate schema="configuration.xsd"/>
</namespace>
<namespace ns="http://schemas.com/other">
<validate schema="other.xsd"/>
</namespace>
</rules>
Save it in the same folder as the two schemas and associate it in the XML document(Document -> Schema -> Associate Schema).

The association will add an Oxygen specific PI before the root of the XML document:

Code: Select all

<?oxygen NVDLSchema="configuration.nvdl"?>
This will allow Validate as you type, manual validation and Content completion in Oxygen.

Let me know if you need further help or information.

PS:
If you don't mind me asking, how do you validate an XML with multiple schemas in Visual Studio?

Regards,
Adrian
Adrian Buza
<oXygen/> XML Editor, Schema Editor and XSLT Editor/Debugger
http://www.oxygenxml.com
AlexPetr
Posts: 5
Joined: Thu Mar 11, 2010 6:46 pm

Re: How to validate against multiple schemas

Post by AlexPetr »

Thank you!
Workaround with nvdl works: I created Document Type Association with schema type NVDL.

VisualStudio validation works if schemas included in project. Simple.

Also you can add schemas with menu XML/Schemas ->Select or Add schema.

I expected Oxygen can find schemas from catalogs through Document Type Association or from current project. Is that possible?

Scenario:
- create project
- add or create schemas
- work with xml and xml validation without any settings!!!!

Sorry for my English
AlexPetr
Posts: 5
Joined: Thu Mar 11, 2010 6:46 pm

Re: How to validate against multiple schemas

Post by AlexPetr »

NVDL doesnot work with this sample:

Code: Select all

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified"
xmlns="http://schemas.com/configuration" targetNamespace="http://schemas.com/configuration">
<xs:element name="root">
<xs:complexType>
<xs:choice>
<xs:element name="param"/>
<xs:any namespace="##other"/>
</xs:choice>
</xs:complexType>
</xs:element>
</xs:schema>
element in ##other namespace is mandatory but oxygen does not see it.
But this element validates normally
adrian
Posts: 2850
Joined: Tue May 17, 2005 4:01 pm

Re: How to validate against multiple schemas

Post by adrian »

Hi,

The Document Type Association with the NVDL schema is the right way to go if you don't want to associate the schema in each XML document.

Implicit associations with schemas are only done through the Document Type Association not from catalogs or the project.

I'm not sure it can be done from catalogs without some serious performance penalty. There are way too many schemas/DTDs, etc, not to mention that sometimes they are overlapping (different schemas with the same target namespace).

But I find the project + schemas + XML documents idea pretty good, I'm adding it to our issue tracking tool. We'll consider adding it in a future version of Oxygen.

Thank you for the feedback.

Regarding the last sample, the simplest way to make it work is to import the other.xsd schema in configuration.xsd, but I'm not sure that's what you're looking for:

Code: Select all

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified"
xmlns="http://schemas.com/configuration" targetNamespace="http://schemas.com/configuration">
<xs:import schemaLocation="other.xsd" namespace="http://schemas.com/other"/>
<xs:element name="root">
<xs:complexType>
<xs:choice>
<xs:element name="param"/>
<xs:any namespace="##other"/>
</xs:choice>
</xs:complexType>
</xs:element>
</xs:schema>
and then just validate the XML with configuration.xsd.

I'll look into why it doesn't work with NVDL.

Right now I think it's because <xs:any namespace="##other"/> makes the first schema accept an element from any namespace other that the targetNamespace but it's expecting it to be declared in that same schema. However, <other xmlns="http://schemas.com/other"/> is declared in the other schema.

Regards,
Adrian
Adrian Buza
<oXygen/> XML Editor, Schema Editor and XSLT Editor/Debugger
http://www.oxygenxml.com
AlexPetr
Posts: 5
Joined: Thu Mar 11, 2010 6:46 pm

Re: How to validate against multiple schemas

Post by AlexPetr »

Thank you for replies!
Oxygen is great product. We use it mostly for xsd editing.

We will be waiting for this feature (Xml validation with project schemas)

About nvdl:
I think that nvdl uses rules for validation. Inner element with other schema validates normally but element <other> doesnot used by first rule. So element other is not visible for first schema. Dont know right is that or not.

Спасибо!
george
Site Admin
Posts: 2095
Joined: Thu Jan 09, 2003 2:58 pm

Re: How to validate against multiple schemas

Post by george »

Hi,

Right, the element in the second namespace will not be sent to the first schema, you need to use an attach action on those sections to direct them to the validate action on the parent section.
An NVDL script that validates the whole document with the first schema and the fragments that are in another namespace with a specific schema for that namespace is below:

Code: Select all


<?xml version="1.0" encoding="UTF-8"?>
<rules xmlns="http://purl.oclc.org/dsdl/nvdl/ns/structure/1.0" startMode="config">
<mode name="config">
<namespace ns="http://schemas.com/configuration">
<validate schema="configuration.xsd" useMode="other"/>
</namespace>
</mode>

<mode name="other">
<namespace ns="http://schemas.com/other">
<validate schema="other.xsd">
<mode>
<anyNamespace>
<unwrap/>
</anyNamespace>
</mode>
</validate>
<attach useMode="attach"/>
</namespace>
<anyNamespace>
<attach useMode="attach"/>
</anyNamespace>
</mode>
<mode name="attach">
<anyNamespace>
<attach/>
</anyNamespace>
</mode>
</rules>
Best Regards,
George
George Cristian Bina
Post Reply