Page 1 of 1

elementFormDefault="qualified" validation and gene

Posted: Tue Apr 10, 2007 1:01 am
by mwr0707
Hello,

Here's a sample schema:
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" targetNamespace="java:normal.client" xmlns:n1="http://schemas.xmlsoap.org/wsdl/" xmlns:s0="java:normal.client" xmlns:s1="http://example.org" xmlns:s2="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="myRoot">
<xs:complexType>
<xs:sequence>
<xs:element name="LoanRequest" type="s0:LoanStruct"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:complexType name="LoanStruct">
<xs:sequence>
<xs:element minOccurs="0" name="Name" nillable="true" type="xs:string"/>
<xs:element minOccurs="0" name="SSN" nillable="true" type="xs:string"/>
<xs:element minOccurs="0" name="Rate" nillable="false" type="xs:double"/>
<xs:element minOccurs="0" name="Amount" nillable="false" type="xs:long"/>
<xs:element minOccurs="0" name="NumOfYear" nillable="false" type="xs:int"/>
<xs:element minOccurs="0" name="Notes" nillable="true" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:schema>

With elementFormDefault="qualified", elements in local declarations must use qualified element-type names in the instance.

If I understand this correctly, the following instance should be invalid:

<?xml version="1.0" encoding="UTF-8"?>
<myRoot xmlns="java:normal.client"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="java:normal.client file:/C:/TestCases/LoanStruct.xsd">
<LoanRequest>
<Name>Name0</Name>
<SSN>SSN0</SSN>
<Rate>3.14159E0</Rate>
<Amount>922337203</Amount>
<NumOfYear>2147483647</NumOfYear>
<Notes>Notes0</Notes>
</LoanRequest>
</myRoot>

In order to be valid, something like the following would be required:

<?xml version="1.0" encoding="UTF-8"?>
<s0:myRoot xsi:schemaLocation="java:normal.client LoanStruct1.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:s0="java:normal.client">
<s0:LoanRequest>
<s0:Name>String</s0:Name>
<s0:SSN>String</s0:SSN>
<s0:Rate>3.14159265358979E0</s0:Rate>
<s0:Amount>2147483647</s0:Amount>
<s0:NumOfYear>0</s0:NumOfYear>
<s0:Notes>String</s0:Notes>
</s0:LoanRequest>
</s0:myRoot>

oXygen validates both instance examples, but the first should fail.
The XML document generation feature produces a document instance with the invalid first format.

The brand "X" xml editor works as I would expect with this.

Regards

Re: elementFormDefault="qualified" validation and

Posted: Tue Apr 10, 2007 10:41 am
by sorin_ristache
Hello
mwr0707 wrote:If I understand this correctly, the following instance should be invalid:

...

In order to be valid, something like the following would be required:
The two instances are in fact the same. The only difference is the prefix used for the samespace java:normal.client. Using other prefix for the same namespace does not change the validity of the document.

The document is valid against the schema that you posted. You can read about the elementFormDefault="qualified" attribute in the specification.


Regards,
Sorin

Posted: Tue Apr 10, 2007 4:57 pm
by mwr0707
Thanks Sorin,
The two instances are in fact the same. The only difference is the prefix used for the samespace java:normal.client. Using other prefix for the same namespace does not change the validity of the document.
In this case it is not variation between two different prefixes. It's the difference between a prefix and no prefix at all.

The specification you linked seems to say: "If elementformdefault="qualified", then the element must specify a target namespace." This is what I was trying to say. In what I've been calling the invalid document instance, isn't <name> unassociated with any namespace?

Thanks and regards,
-Mark

Posted: Tue Apr 10, 2007 5:43 pm
by sorin_ristache
No, the XML Schema specification which I linked says that if you use elementFormDefault="qualified" on the root element and there is no form attribute for an element that overrides the attribute of the root element then all unprefixed local elements are by default in the schema target namespace, in your case "java:normal.client":
{target namespace} If form is present and its ·actual value· is qualified, or if form is absent and the ·actual value· of elementFormDefault on the <schema> ancestor is qualified, then the ·actual value· of the targetNamespace [attribute] of the parent <schema> element information item, or ·absent· if there is none, otherwise ·absent·.
So in your case a valid document instance implies that the elements Name, SSN, Rate, Amount, etc. are in the "java:normal.client" namespace. In what you call the invalid document instance all these elements are in the default namespace of that document instance which is exactly "java:normal.client" so this condition of valid document is satisfied.


Regards,
Sorin

Posted: Tue Apr 10, 2007 6:20 pm
by george
Hi Mark,

The namespace declarations are inherited. If you declare a default namespace then all elements without a prefix belong to that namespace. If you want to have elements in no namespace you need to reset the default namespace with something like xmlns="". You can eventually try adding that on the Name element and see that oXygen will report the error.

Best Regards,
George

Posted: Tue Apr 10, 2007 6:45 pm
by mwr0707
Thanks Sorin and George,

So if I understand you correctly, the reason why the unprefixed elements of the instance are valid is because of:

<myRoot xmlns="java:normal.client"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="java:normal.client file:/C:/TestCases/LoanStruct.xsd">

Which results in all of the elements contained within myRoot to inherit its default namespace of "java:normal.client" ?

Assuming I've got that right...

How should a server-side xpath expression be written to account for the possibility that there may be a prefix or not? Do you write the xpath select with the prefix, and the engine should realize that an unprefixed element is "effectively namespace qualified" as a result of its association with a default namespace? (This presumes xpath would never match on the literal prefix, but instead matches on the "effective namespace" for any given element, whether by prefix or default).

Thanks and regards,
-Mark

Posted: Tue Apr 10, 2007 8:35 pm
by george
Hi Mark,

Exactly, XPath matches on the actual namespace not on how that element is represented with one prefix or with no prefix at all. So no matther what prefix you use in your instance document you need to declare the namespace in the stylesheet with some prefix for instance and use that prefix that you declared in the stylesheet to write the name tests for your elements. Note that XPath 1.0 does not have a notion of default namespace, if you write an name test without a prefix that will always match no namespace names. In XPath 2.0 it is possible to specify a default namespace, for instance in an XLST 2.0 stylesheet you can do that with the xpath-default-namespace attribute.

Best Regards,
George

Posted: Tue Apr 10, 2007 10:15 pm
by mwr0707
Thanks again George,

I'll try this out.

Regards,

-Mark