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 1 2111
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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>
|
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>
|
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">
|
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...
|
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;
| |
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...
|
by: Plop69 |
last post by:
need some help on following:
xml file 1
<TEST xmlns="http://test" >
<OK>mlkddflmkj</OK>
</TEST>
xml file 2
|
by: Plop69 |
last post by:
need some help on following:
xml file 1
<TEST xmlns="http://test" >
<OK>mlkddflmkj</OK>
</TEST>
|
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
|
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...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
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...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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
| |
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...
| |