0

I am trying to validate an XML via an XSD in c# code. However it keeps throwing an XmlSchemaValidationException, namely "The 'refname' attribute is not declared.".

The code doing the validation:

XmlReaderSettings xmlSettings = new XmlReaderSettings();
            xmlSettings.Schemas = new System.Xml.Schema.XmlSchemaSet();
            xmlSettings.Schemas.Add(null, @"\\[Network-drive path]\KVSchemaMod.xsd");
            xmlSettings.ValidationType = ValidationType.Schema;
            xmlSettings.DtdProcessing = DtdProcessing.Parse;
            xmlSettings.ValidationFlags = XmlSchemaValidationFlags.AllowXmlAttributes;
            xmlSettings.ValidationFlags = XmlSchemaValidationFlags.ProcessInlineSchema;
            xmlSettings.ValidationFlags = XmlSchemaValidationFlags.ReportValidationWarnings;
            XmlReader reader = XmlReader.Create(@"\\[Network-drive path]\KV_Article.xml", xmlSettings);

            // Parse the file.
            while (reader.Read()) ;

The XSD (with repetitous parts removed):

<?xml version="1.0" encoding="iso-8859-1"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:sql="urn:schemas-microsoft-com:mapping-schema">
<xs:annotation>
    <xs:appinfo>
        <sql:relationship name="ProductIdentifier"
        parent="tblKVProduct"
        parent-key="record_reference"
        child="tblKVProductIdentifier"
        child-key="record_reference" />
... (More SQL-mappings)
<xs:element name="ONIXMessage" sql:is-constant="1">
    <xs:complexType>
        <xs:sequence>
            <xs:element minOccurs="0" maxOccurs="unbounded" name="Product" sql:relation="tblKVProduct">
                <xs:complexType>
                    <xs:sequence>
                        <xs:element minOccurs="1" maxOccurs="1" name="RecordReference" sql:field="record_reference" >
                            <xs:simpleType>
                                <xs:restriction base="xs:string">
                                    <xs:minLength value="0"/>
                                    <xs:maxLength value="32"/>
                                </xs:restriction>
                            </xs:simpleType>
                        </xs:element>
...(More elements under Product)

The XML to be validated (ONIX-standard):

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE ONIXMessage SYSTEM "http://www.editeur.org/onix/2.1/reference/onix-  international.dtd">
<ONIXMessage>
<Product>
    <RecordReference>0786606274</RecordReference>
    <NotificationType>03</NotificationType>
    <ProductIdentifier>
        <ProductIDType>01</ProductIDType>
        <IDTypeName>Kustantajan tuotenumero</IDTypeName>
        <IDValue>95535</IDValue></ProductIdentifier>
    <ProductIdentifier>
        <ProductIDType>02</ProductIDType>
        <IDValue>0786606274</IDValue></ProductIdentifier>
    <ProductIdentifier>
        <ProductIDType>03</ProductIDType>
        <IDValue>9780786606276</IDValue>
    </ProductIdentifier>
...(And more Product-fields)

The XML also has a Header tag as the first child of ONIXMessage (followed by all Product tags that as also childrens of ONIXMessage), but since none of the fields there are referenced in the schema I don't see how they could cause this particular error.

As I googled the error I read up on the XmlSchemaElement.RefName property (MSDN Definition) it even explicitly says that "The value cannot be set if the containing element is the schema element." Since that is the case for the XSD I don't understand why it throws an error about it not being declared.

I've been trying to find out if this could be related to the lack of namespace, but in my mind that shouldn't be a problem since there is none in neither the XML nor the XSD. Or does one need to force a namespace onto the root-element in order to validate the XML?

The validating code does work with another set of XML/XSD (very different build-up on those though), could that just be a fluke though and the error actually in the way I validate?

Just for completion's sake, the Header tag in the XML:

<Header>
    <FromCompany></FromCompany>
    <FromPerson></FromPerson>
    <FromEmail></FromEmail>
    <AddresseeIdentifier>
        <AddresseeIDType></AddresseeIDType>
        <IDTypeName></IDTypeName>
        <IDValue></IDValue>
    </AddresseeIdentifier>
    <ToCompany></ToCompany>
    <ToPerson></ToPerson>
    <MessageNumber></MessageNumber>
    <SentDate></SentDate>
    <MessageNote></MessageNote>
</Header>
4

1 に答える 1

1

完全な例を提供していないため、問題を再現することはできません。憶測以外に基づいた回答が必要な場合は、質問の回答者が問題を再現してより詳細に調査できるように、スキーマとデータを問題を示す最小の例に切り詰める必要があります。もちろん、問題の小さな例を見つける過程で何がうまくいかないのかがわかるので、問題がその過程で解消されることはよくあります。しかし、それはあなたが取らなければならないリスクです。

そうは言っても、私は先に進んで推測します。XMLSchemaElement オブジェクトの RefName プロパティは、ほぼ間違いなく危険人物です。このメッセージは、検証しようとしている XML にrefnameどこか (省略した部分) に名前が付けられた属性があり、XSD スキーマにはそれに対する定義がないかのように聞こえます。確かに、提供する XSD フラグメントには定義がありません。

ただし、 http: //www.editeur.org/onix/2.1/reference/onix-international.dtd で定義されているボキャブラリ(XML で指定している) は、refname多くの要素 (おそらくすべての要素) の属性として定義されています。 、個別にはチェックしていませんが)。したがって、ONIXMessage 要素にrefname属性を持つ子孫が含まれる可能性はまったくあり得ません。

実際、それは信じられないというだけではありません。それは確かです。

XML が指す DTD は、refnameすべての要素 (少なくとも、私が見たすべての要素) の属性にデフォルト値を提供しますが、XSD スキーマ宣言は準備されていないようです。つまり、スキーマ バリデータによって認識されるドキュメントの最初の開始タグは not <ONIXMessage>but<ONIXMessage refname="ONIXMessage" shortname="ONIXmessage">です。

www.editeur.org から入手できる XSD を使用していない場合、おそらく最も簡単な方法は、その決定を再考し、独自のスキーマを展開する代わりに editeur.org のスキーマを使用することです。これには、refname および shortname 属性の宣言が含まれます。そのスキーマを使用している場合、または使用しようとしている場合は、何かがひどく間違っており、バリデーターがそれを見つけたり読み取ったりしていません。その場合、最善の方法は、システムが正しいスキーマ ドキュメントを見つけられない理由を突き止めることです。

于 2012-10-25T19:50:53.267 に答える