You need to specify transNS prefix in ALL XPath expressions. In the schema
below, you only do it in a few places. When I updated the rest of the
validation has failed. Either a key is missing or the existing key has an
empty node.
validation has failed. Either a key is missing or the existing key has an
empty node.
validation has failed. Either a key is missing or the existing key has an
This posting is provided "AS IS" with no warranties, and confers no rights.
>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.Translation"
> xmlns="http://Festo.Common.Translation"
> targetNamespace="http://Festo.Common.Translation"
> elementFormDefault="qualified" attributeFormDefault="unqualified"
> version="1.0.1">
> <xsd:simpleType name="stVersionSyntax">
> <xsd:restriction base="xsd:string">
> <xsd:pattern value="V[0-9]+\.[0-9]+\.[0-9]+"/>
> <!-- e.g. V1.0.0, checked -->
> </xsd:restriction>
> </xsd:simpleType>
> <xsd:element name="TranslationTable" type="ctTranslationTable">
> <xsd:key name="LangKey">
> <xsd:selector xpath="transNS:Language"/>
> <xsd:field xpath="@ID"/>
> </xsd:key>
> <xsd:keyref name="LangKeyRef" refer="transNS:LangKey">
> <xsd:selector xpath="transNS:Item/Translation"/>
> <xsd:field xpath="@Language"/>
> </xsd:keyref>
> </xsd:element>
> <xsd:complexType name="ctTranslationItem">
> <xsd:sequence>
> <xsd:element name="Translation" type="ctTranslation"
> maxOccurs="unbounded"/>
> <xsd:element name="Parameter" type="ctParameter" minOccurs="0"
> maxOccurs="unbounded"/>
> </xsd:sequence>
> <xsd:attribute name="ID" type="xsd:string" use="required"/>
> <!-- word to translate -->
> </xsd:complexType>
> <xsd:complexType name="ctTranslationTable">
> <xsd:sequence>
> <!-- general file
> -->
> <xsd:element name="Language" type="ctLanguage" minOccurs="0"
> maxOccurs="unbounded"/>
> <xsd:element name="Item" type="ctTranslationItem" minOccurs="0"
> maxOccurs="unbounded">
> <xsd:key name="TransKey">
> <xsd:selector xpath="./Translation"/>
> <xsd:field xpath="@Language"/>
> </xsd:key>
> </xsd:element>
> </xsd:sequence>
> <xsd:attribute name="Version" type="stVersionSyntax"/>
> <!-- version of the file -->
> </xsd:complexType>
> <xsd:complexType name="ctTranslation">
> <xsd:attribute name="Language" type="xsd:string" use="required"/>
> <xsd:attribute name="Content" type="xsd:string" use="required"/>
> </xsd:complexType>
> <xsd:complexType name="ctParameter">
> <xsd:attribute name="Index" type="xsd:int" use="required"/>
> <xsd:attribute name="Ref" type="xsd:string" use="required"/>
> </xsd:complexType>
> <xsd:complexType name="ctLanguage">
> <xsd:attribute name="ID" type="xsd:string" use="required"/>
> <xsd:attribute name="Description" type="xsd:string" 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"?>
> <TranslationTable xmlns="http://Festo.Common.Translation"
> xmlns:xsi="
http://www.w3.org/2001/XMLSchema-instance" > xsi:schemaLocation="
http://Festo.Common.Translation > S:\Festo\Packages\Common\Festo.Common.Translation\XmlTranslationTable.xsd">
> <Language Description="English" ID="EN"/>
> <Item ID="Zero point">
> <Translation Language="EN" Content="Zero point"/>
> <Translation Language="DE" Content="Nullpunkt"/>
> </Item>
> </TranslationTable>
>
> 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_SchemaCollection = new XmlSchemaCollection();
> XmlTextReader xsdReader = new
> XmlTextReader(typeof(TranslationTable).Assembly.GetManifestResourceStream("Festo.Common.Translation.XmlTranslationTable.xsd"));
> m_SchemaCollection.Add(m_XmlNamespace, xsdReader);
> xsdReader.Close();
>
> StreamReader s= System.IO.File.OpenText(sTranslationTableFile);
> string xmlFrag = s.ReadToEnd();
> NameTable nt = new NameTable();
> XmlNamespaceManager nsmgr = new XmlNamespaceManager(nt);
> XmlParserContext context = new XmlParserContext(null, nsmgr, null,
> XmlSpace.None);
> xmlValidatingReader = new XmlValidatingReader(xmlFrag,
> XmlNodeType.Element, context);
> xmlValidatingReader.Schemas.Add(m_SchemaCollection);
> xmlValidatingReader.ValidationType=ValidationType.Schema;
> xmlValidatingReader.ValidationEventHandler+=new
> ValidationEventHandler(transValidator_ValidationEventHandler); //PLEASE
> ADD A
> VALIDATION HANDLER
> while (xmlValidatingReader.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