473,801 Members | 2,259 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

XmlValidatingRe ader

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
Nov 12 '05 #1
1 2115
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
expressions, system.xml generated the following errors:
- The identity constraint 'http://Festo.Common.Tr anslation:LangK ey'
validation has failed. Either a key is missing or the existing key has an
empty node.
- The identity constraint 'http://Festo.Common.Tr anslation:Trans Key'
validation has failed. Either a key is missing or the existing key has an
empty node.
- The identity constraint 'http://Festo.Common.Tr anslation:Trans Key'
validation has failed. Either a key is missing or the existing key has an
empty node.

--
Stan Kitsis
Program Manager, XML Technologies
Microsoft Corporation

This posting is provided "AS IS" with no warranties, and confers no rights.
Use of included script samples are subject to the terms specified at
http://www.microsoft.com/info/cpyright.htm
"Bernhard Felkel" <Bernhard Fe****@discussi ons.microsoft.c om> wrote in
message news:7F******** *************** ***********@mic rosoft.com...
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
-->
<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

Nov 12 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
1628
by: Larry | last post by:
I believe the .Net XmlValidatingReader should fail when validating XML that contains a ComplexType element with white space when the ComplexType element has the mixed attribute set to false in the XSD used for validation. XSD fragment: <xs:element name="TRSellerBuyerData"> <xs:complexType mixed="false"> <xs:sequence>
1
1813
by: Larry | last post by:
Reposting due to lack of response - I believe the .Net XmlValidatingReader should fail when validating XML that contains a ComplexType element with white space when the ComplexType element has the mixed attribute set to false in the XSD used for validation. XSD fragment: <xs:element name="TRSellerBuyerData"> <xs:complexType mixed="false"> <xs:sequence>
2
1851
by: MT | last post by:
Hi, I am currently validating an XML file against a Schema using XMLValidatingReader. The schema actually contains ranges for particular elements and I have been using it to detect range errors before it gets to my program. The way the rangechecking works is that every element that needs range checking is defined as an element with a particular type in the XSD. For example, <xs:element name="Row" maxOccurs="48">
18
5572
by: Vlad | last post by:
I have the following code: I have a local copy of the DTD that I need to validate incoming XML documents against. The XML document has the <!DOCTYPE myname SYSTEM "myfile.dtd"> define. When the following code is executed the XML gets resolved through the XMLResolver and gets correctly validated against the locally stored DTD file. The problem occurs when the incoming XML contains no DOCTYPE attribute. The resolver code never gets called...
9
1942
by: jason | last post by:
how do you use the XmlValidatingReader to validate an XML document that is passed into the XmlValidatingReader constructor? it looks like the normal process is to use an underlying reader, as follows (C#): XmlValidatingReader oMyVReader = new XmlValidatingReader(oMyReader); oMyVReader.Schemas.Add(oMySchemaCollection); oMyVReader.ValidationType = ValidationType.Schema;
5
1414
by: Geoff | last post by:
I am using an XMLValidatingReader to validate an XML file received via a web service. I want to verify that the incoming file matches the XML schema. When testing the validation routine, the XMLValidatingReader correctly flags mis-matched tags such as <abc>some content</xyz> but does not catch other errors. For example, it doesn't catch tags that are not part of the schema, doesn't catch missing tags where the schema has minoccurs="1", and...
1
1848
by: Plop69 | last post by:
need some help on following: xml file 1 <TEST xmlns="http://test" > <OK>mlkddflmkj</OK> </TEST> xml file 2
12
8569
by: Plop69 | last post by:
need some help on following: xml file 1 <TEST xmlns="http://test" > <OK>mlkddflmkj</OK> </TEST>
1
3224
by: JoeZ | last post by:
Hi all, I have a question about using XMLValidatingReader. I have a schema files (xsd), and a xml data file. In the xml data file, if I don't specify the schema file path, XMLValidatingReader always complains. If schema file path is included, then it doesn't complain. Is there any way to tell XMLValidatingReader where the schema file is, or am
0
9698
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9101
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7589
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6829
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5479
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5616
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4156
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3773
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2959
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.