I have troubles validating XML files with key/keyref constraints.
Here´s my schema:
<?xml version="1.0" encoding="utf-8"?>
<xsd:schema xmlns:xsd="http ://www.w3.org/2001/XMLSchema"
xmlns:transNS=" http://Festo.Common.Tr anslation"
xmlns="http://Festo.Common.Tr anslation"
targetNamespace ="http://Festo.Common.Tr anslation"
elementFormDefa ult="qualified" attributeFormDe fault="unqualif ied"
version="1.0.1" >
<xsd:simpleTy pe name="stVersion Syntax">
<xsd:restrictio n base="xsd:strin g">
<xsd:pattern value="V[0-9]+\.[0-9]+\.[0-9]+"/>
<!-- e.g. V1.0.0, checked -->
</xsd:restriction >
</xsd:simpleType>
<xsd:element name="Translati onTable" type="ctTransla tionTable">
<xsd:key name="LangKey">
<xsd:selector xpath="transNS: Language"/>
<xsd:field xpath="@ID"/>
</xsd:key>
<xsd:keyref name="LangKeyRe f" refer="transNS: LangKey">
<xsd:selector xpath="transNS: Item/Translation"/>
<xsd:field xpath="@Languag e"/>
</xsd:keyref>
</xsd:element>
<xsd:complexTyp e name="ctTransla tionItem">
<xsd:sequence >
<xsd:element name="Translati on" type="ctTransla tion"
maxOccurs="unbo unded"/>
<xsd:element name="Parameter " type="ctParamet er" minOccurs="0"
maxOccurs="unbo unded"/>
</xsd:sequence>
<xsd:attribut e name="ID" type="xsd:strin g" use="required"/>
<!-- word to translate -->
</xsd:complexType >
<xsd:complexTyp e name="ctTransla tionTable">
<xsd:sequence >
<!-- general file information -->
<xsd:element name="Language" type="ctLanguag e" minOccurs="0"
maxOccurs="unbo unded"/>
<xsd:element name="Item" type="ctTransla tionItem" minOccurs="0"
maxOccurs="unbo unded">
<xsd:key name="TransKey" >
<xsd:selector xpath="./Translation"/>
<xsd:field xpath="@Languag e"/>
</xsd:key>
</xsd:element>
</xsd:sequence>
<xsd:attribut e name="Version" type="stVersion Syntax"/>
<!-- version of the file -->
</xsd:complexType >
<xsd:complexTyp e name="ctTransla tion">
<xsd:attribut e name="Language" type="xsd:strin g" use="required"/>
<xsd:attribut e name="Content" type="xsd:strin g" use="required"/>
</xsd:complexType >
<xsd:complexTyp e name="ctParamet er">
<xsd:attribut e name="Index" type="xsd:int" use="required"/>
<xsd:attribut e name="Ref" type="xsd:strin g" use="required"/>
</xsd:complexType >
<xsd:complexTyp e name="ctLanguag e">
<xsd:attribut e name="ID" type="xsd:strin g" use="required"/>
<xsd:attribut e name="Descripti on" type="xsd:strin g" use="required"/>
</xsd:complexType >
</xsd:schema>
I define a key for the Languages and a keyref for each Translation item.
Here´s my XML-File:
<?xml version="1.0" encoding="UTF-8"?>
<TranslationTab le xmlns="http://Festo.Common.Tr anslation"
xmlns:xsi="http ://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocat ion="http://Festo.Common.Tr anslation
S:\Festo\Packag es\Common\Festo .Common.Transla tion\XmlTransla tionTable.xsd">
<Language Description="En glish" ID="EN"/>
<Item ID="Zero point">
<Translation Language="EN" Content="Zero point"/>
<Translation Language="DE" Content="Nullpu nkt"/>
</Item>
</TranslationTabl e>
The Translation Item references the language "DE" which is not a member of
the languages. So, this must lead to a validation error but it does not!
A duplicate key is handled correctly while a missing keyref is not.
What´s interesting to me is that, if there is no namespace defined,
validation works fine.
Here´s my C# code snipped which validates the xml file:
m_SchemaCollect ion = new XmlSchemaCollec tion();
XmlTextReader xsdReader = new
XmlTextReader(t ypeof(Translati onTable).Assemb ly.GetManifestR esourceStream(" Festo.Common.Tr anslation.XmlTr anslationTable. xsd"));
m_SchemaCollect ion.Add(m_XmlNa mespace, xsdReader);
xsdReader.Close ();
StreamReader s= System.IO.File. OpenText(sTrans lationTableFile );
string xmlFrag = s.ReadToEnd();
NameTable nt = new NameTable();
XmlNamespaceMan ager nsmgr = new XmlNamespaceMan ager(nt);
XmlParserContex t context = new XmlParserContex t(null, nsmgr, null,
XmlSpace.None);
xmlValidatingRe ader = new XmlValidatingRe ader(xmlFrag,
XmlNodeType.Ele ment, context);
xmlValidatingRe ader.Schemas.Ad d(m_SchemaColle ction);
xmlValidatingRe ader.Validation Type=Validation Type.Schema;
xmlValidatingRe ader.Validation EventHandler+=n ew
ValidationEvent Handler(transVa lidator_Validat ionEventHandler ); //PLEASE ADD A
VALIDATION HANDLER
while (xmlValidatingR eader.Read()){} //Loop through elements
//f you reach here without hitting validations, you are OK
MessageBox.Show ("DONE");
s.Close();
I validated the XML with both, the XMLSpy and the Sun-Xml Validator and both
detected the missing keyref!
What´s going wrong here?
Thank´s for your help in advance
Bernhard