我的xsd架构有什么问题?它提供的type元素未在包含另一个xsd文件时声明

时间:2015-09-29 11:09:22

标签: c# xml xsd xsd-validation xml-validation

我正在尝试使用架构验证xml,但问题是xsd无效,我试过测试它是否有效,它给出了错误XSD架构错误:未声明类型'testNamespaces:eltyp_string035'。 我的验证架构的代码在这里:

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
using System.Xml.Schema;

namespace testingXmlValidation
{
    class Program
    {
        static StringBuilder errors = new StringBuilder();
        static void Main(string[] args)
        {

                string xsdPath = null;
                 xsdPath = "C:/Users/testing/a.xsd";
                elementName = "SpeTrans";

                try
                {
                    XmlSchemaCollection sc = new XmlSchemaCollection();
                    sc.Add("testNamespaces", xsdPath);
                    Console.WriteLine("No Schema error.");
                }
                catch (XmlSchemaException ex)
                {
                    Console.WriteLine("XSD schema Error: {0}", ex.Message);
                }

                Console.ReadKey();       

         }  
    }

}

我的主要xsd文件位于以下似乎问题出现在此include或包含的文件command.xsd中,因为它包含“eltyp_string035”声明

<?xml version="1.0" encoding="utf-8" ?>
<!--Created with Liquid XML 2015 Designer Edition 13.2.0.5980 (http://www.liquid-technologies.com)-->
<!-- Version 1.0.1  du 14/09/2015: TypNoiSuiviE à TypNotSuiviE -->
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="testNamespaces">
    <xs:include schemaLocation="C:/Users/ACER/Desktop/KT-iNDIA/September/24thseptember2015-KtIndia/Shakhar24092015/xsdFile/common.xsd" />
    <xs:element name="trans">                
                <xs:element name="ContDest" type="eltyp_string035" minOccurs="0" xmlns="testNamespaces">
                    <xs:annotation>
                        <xs:documentation>Nom du contact Destinataire</xs:documentation>
                    </xs:annotation>
                </xs:element>
                <xs:element name="MailDest" type="eltyp_string035" minOccurs="0" xmlns="testNamespaces">
                    <xs:annotation>
                        <xs:documentation>Email du contact Destinataire</xs:documentation>
                    </xs:annotation>
                </xs:element>
                <xs:element name="TelDest" type="eltyp_string020" minOccurs="0">
                    <xs:annotation>
                        <xs:documentation>Téléphone du contact Destinataire</xs:documentation>
                    </xs:annotation>
                </xs:element>                   
                <xs:element name="ValDecAssu" minOccurs="0">
                    <xs:annotation>
                        <xs:documentation>Valeur Assurance En Centimes</xs:documentation>
                    </xs:annotation>
                    <xs:simpleType>
                        <xs:restriction base="eltyp_string009">
                            <xs:pattern value="[0-9]{9}" />
                        </xs:restriction>
                    </xs:simpleType>
                </xs:element>
                <xs:element name="ValDevAssu" minOccurs="0" default="EUR">
                    <xs:annotation>
                        <xs:documentation>Devise valeur Assurance</xs:documentation>
                    </xs:annotation>
</xs:schema>

和common.xsd是:

  <?xml version="1.0" encoding="utf-8" ?>
<!--Created with Liquid XML 2015 Designer Edition 13.1.0.5909 (http://www.liquid-technologies.com)-->
<!--Version 1.0.1 du 24/06/2015  -->
<!--Version 1.0.2 du 07/07/2015  -->
<!--Version 1.0.3 du 16/07/2015  -->
<!--Version 1.0.4 du 21/07/2015  - Correction liste des trans/modtrans/Col -->
<xs:schema elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <!--Element De base -->

    </xs:simpleType>
    <xs:simpleType xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xs="http://www.w3.org/2001/XMLSchema" name="eltyp_string032">
        <xsd:restriction base="xsd:string">
            <xsd:maxLength value="32" />
        </xsd:restriction>
    </xs:simpleType>
    <xs:simpleType xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xs="http://www.w3.org/2001/XMLSchema" name="eltyp_string035">
        <xsd:restriction base="xsd:string">
            <xsd:maxLength value="35" />
        </xsd:restriction>
    </xs:simpleType>

</xs:schema>
<!--Fin des ꭩments de type complexes-->

为什么main.xsd无法获取“eltyp_string035”的声明

2 个答案:

答案 0 :(得分:2)

在您的主XSD中,将 xmlns =&#34; testNamespaces&#34; 添加到您的xs:schema元素。

当您写入type =&#34; eltyp_string035&#34;时,XSD处理器会考虑&#34; eltyp_string035&#34;作为限定名称,即具有命名空间。由于您的架构没有定义默认命名空间,因此&#34; eltyp_string035&#34; = {} eltyp_string035。

您在包含一个架构的架构中包含一个没有targetNamespace的架构。这有时被称为 chameleonic include,这意味着包含的模式中的所有组件都采用包含模式的命名空间(此处为testNamespaces)。因此,它查找{testNamespaces} eltyp_string035。

这些是你纠正的模式;将它们与您拥有的内容进行比较,并阅读XML名称空间。

<?xml version="1.0" encoding="utf-8" ?>
<!--Created with Liquid XML 2015 Designer Edition 13.2.0.5980 (http://www.liquid-technologies.com)-->
<!-- Version 1.0.1  du 14/09/2015: TypNoiSuiviE à TypNotSuiviE -->
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="testNamespaces" xmlns="testNamespaces">
    <xs:include schemaLocation="common.xsd" />
    <xs:element name="Spetrans">
        <xs:complexType>
            <xs:sequence minOccurs="1">
                <xs:element name="TypNotSuiviE" minOccurs="0" default="0">
                    <xs:annotation>
                        <xs:documentation>Forçage Type de notification pour le suivi « Expéditeur »</xs:documentation>
                    </xs:annotation>
                    <xs:simpleType>
                        <xs:restriction base="xs:integer">
                            <xs:totalDigits value="1" />
                            <xs:enumeration value="0" />
                            <xs:enumeration value="1" />
                            <xs:enumeration value="2" />
                        </xs:restriction>
                    </xs:simpleType>
                </xs:element>
                <xs:element name="TypNotSuiviO" minOccurs="0" default="0">
                    <xs:annotation>
                        <xs:documentation>Forçage Type de notification pour le suivi « Donneur d’ordre »</xs:documentation>
                    </xs:annotation>
                    <xs:simpleType>
                        <xs:restriction base="xs:integer">
                            <xs:totalDigits value="1" />
                            <xs:enumeration value="0" />
                            <xs:enumeration value="1" />
                            <xs:enumeration value="2" />
                        </xs:restriction>
                    </xs:simpleType>
                </xs:element>
                <xs:element name="TypNotSuiviD" minOccurs="1" default="0">
                    <xs:annotation>
                        <xs:documentation>Forçage Type de notification pour le suivi « Donneur d’ordre »</xs:documentation>
                    </xs:annotation>
                    <xs:simpleType>
                        <xs:restriction base="xs:integer">
                            <xs:totalDigits value="1" />
                            <xs:enumeration value="0" />
                            <xs:enumeration value="1" />
                            <xs:enumeration value="2" />
                        </xs:restriction>
                    </xs:simpleType>
                </xs:element>
                <xs:element name="ContDest" type="eltyp_string035" minOccurs="0">
                    <xs:annotation>
                        <xs:documentation>Nom du contact Destinataire</xs:documentation>
                    </xs:annotation>
                </xs:element>
                <xs:element name="MailDest" type="eltyp_string035" minOccurs="0">
                    <xs:annotation>
                        <xs:documentation>Email du contact Destinataire</xs:documentation>
                    </xs:annotation>
                </xs:element>
                <xs:element name="TelDest" type="eltyp_string020" minOccurs="0">
                    <xs:annotation>
                        <xs:documentation>Téléphone du contact Destinataire</xs:documentation>
                    </xs:annotation>
                </xs:element>
                <xs:element name="GsmDest" type="eltyp_string020" minOccurs="0">
                    <xs:annotation>
                        <xs:documentation>Téléphone GSM du contact Destinataire</xs:documentation>
                    </xs:annotation>
                </xs:element>
                <xs:element name="ValDecAssu" minOccurs="0">
                    <xs:annotation>
                        <xs:documentation>Valeur Assurance En Centimes</xs:documentation>
                    </xs:annotation>
                    <xs:simpleType>
                        <xs:restriction base="eltyp_string009">
                            <xs:pattern value="[0-9]{9}" />
                        </xs:restriction>
                    </xs:simpleType>
                </xs:element>
                <xs:element name="ValDevAssu" minOccurs="0" default="EUR">
                    <xs:annotation>
                        <xs:documentation>Devise valeur Assurance</xs:documentation>
                    </xs:annotation>
                    <xs:simpleType>
                        <xs:restriction base="eltyp_string003">
                            <xs:enumeration value="EUR" />
                        </xs:restriction>
                    </xs:simpleType>
                </xs:element>
                <xs:element name="ValDecDou" minOccurs="0">
                    <xs:annotation>
                        <xs:documentation>Valeur Douane En Centimes</xs:documentation>
                    </xs:annotation>
                    <xs:simpleType>
                        <xs:restriction base="eltyp_string009">
                            <xs:pattern value="[0-9]{9}" />
                        </xs:restriction>
                    </xs:simpleType>
                </xs:element>
                <xs:element name="ValDevDou" minOccurs="0" default="EUR">
                    <xs:annotation>
                        <xs:documentation>Devise valeur Douane</xs:documentation>
                    </xs:annotation>
                    <xs:simpleType>
                        <xs:restriction base="eltyp_string003">
                            <xs:enumeration value="EUR" />
                        </xs:restriction>
                    </xs:simpleType>
                </xs:element>
                <xs:element name="ValDecRep" minOccurs="0">
                    <xs:annotation>
                        <xs:documentation>Valeur déclarée CRBT En Centimes</xs:documentation>
                    </xs:annotation>
                    <xs:simpleType>
                        <xs:restriction base="eltyp_string009">
                            <xs:pattern value="[0-9]{9}" />
                        </xs:restriction>
                    </xs:simpleType>
                </xs:element>
                <xs:element name="ValDevRep" minOccurs="0" default="EUR">
                    <xs:annotation>
                        <xs:documentation>Devise valeur déclarée CRBT</xs:documentation>
                    </xs:annotation>
                    <xs:simpleType>
                        <xs:restriction base="eltyp_string003">
                            <xs:enumeration value="EUR" />
                        </xs:restriction>
                    </xs:simpleType>
                </xs:element>
                <xs:element name="TypeRep" minOccurs="0">
                    <xs:annotation>
                        <xs:documentation>Type de REP (Aller ou Retour) Ou bien  Type du SWAP déclaré (Aller ou Retour)
Valeur par défaut à vide.
SWPA = SWAP Aller, SWPR = SWAP Retour
REPA = REP Aller, REPR = REP Retour
</xs:documentation>
                    </xs:annotation>
                    <xs:simpleType>
                        <xs:restriction base="eltyp_string004">
                            <xs:enumeration value="" />
                            <xs:enumeration value="SWPA" />
                            <xs:enumeration value="SWPR" />
                            <xs:enumeration value="REPA" />
                            <xs:enumeration value="REPR" />
                        </xs:restriction>
                    </xs:simpleType>
                </xs:element>
                <xs:element name="RepRet" minOccurs="0">
                    <xs:annotation>
                        <xs:documentation>N° Chronopost du  REP ou du SWAP retour 
Si le colis déclaré est un SWAP Aller, indiquer ici le N° du Retour. Si le colis déclaré est un REP Aller, indiquer ici le N° du Retour.</xs:documentation>
                    </xs:annotation>
                    <xs:simpleType>
                        <xs:restriction base="eltyp_string013" />
                    </xs:simpleType>
                </xs:element>
                <xs:element name="RepDep" minOccurs="0">
                    <xs:annotation>
                        <xs:documentation>N° Chronopost du  REP ou du SWAP aller
Si le colis déclaré est un SWAP Retour, indiquer ici le N° du Aller. Si le colis déclaré est un REP Retour, indiquer ici le N° du Aller
</xs:documentation>
                    </xs:annotation>
                    <xs:simpleType>
                        <xs:restriction base="eltyp_string013" />
                    </xs:simpleType>
                </xs:element>
                <xs:element name="CptPart" type="eltyp_string045" minOccurs="0">
                    <xs:annotation>
                        <xs:documentation>information fourni par Chronopost</xs:documentation>
                    </xs:annotation>
                </xs:element>
                <xs:element name="DatDebRdv" type="Eltyp_DateAAAAMMJJHHMM" minOccurs="0">
                    <xs:annotation>
                        <xs:documentation> Date de début de rendez-vous Sous la forme AAAAMMJJHHMM Uniquement pour les produits à livraison sur RDV</xs:documentation>
                    </xs:annotation>
                </xs:element>
                <xs:element name="DatFinRdv" type="Eltyp_DateAAAAMMJJHHMM" minOccurs="0">
                    <xs:annotation>
                        <xs:documentation> Date de fin de rendez-vous Sous la forme AAAAMMJJHHMM Uniquement pour les produits à livraison sur RDV</xs:documentation>
                    </xs:annotation>
                </xs:element>
                <xs:element name="LivExpVen" minOccurs="0" default="A">
                    <xs:annotation>
                        <xs:documentation>Jour de livraison souhaité pour les envois du vendredi
0 : Normal ,1: Lundi, 2: Mardi, 3: Mercredi, 4: Jeudi, 5: Vend et 6: Samedi 
« A » Déterminé par Mutualisation Expédition
</xs:documentation>
                    </xs:annotation>
                    <xs:simpleType>
                        <xs:restriction base="eltyp_string001">
                            <xs:enumeration value="0" />
                            <xs:enumeration value="1" />
                            <xs:enumeration value="2" />
                            <xs:enumeration value="3" />
                            <xs:enumeration value="4" />
                            <xs:enumeration value="5" />
                            <xs:enumeration value="6" />
                            <xs:enumeration value="A" />
                        </xs:restriction>
                    </xs:simpleType>
                </xs:element>
                <xs:element name="GpsLivLat" type="eltyp_string045" minOccurs="0">
                    <xs:annotation>
                        <xs:documentation>Coordonnées GPS du point à livrer : Latitude </xs:documentation>
                    </xs:annotation>
                </xs:element>
                <xs:element name="GpsLivLon" type="eltyp_string045" minOccurs="0">
                    <xs:annotation>
                        <xs:documentation>Coordonnées GPS du point à livrer : Longitude </xs:documentation>
                    </xs:annotation>
                </xs:element>
                <xs:element name="InstPart1" type="eltyp_string045" minOccurs="0">
                    <xs:annotation>
                        <xs:documentation>Informations libre émetteur</xs:documentation>
                    </xs:annotation>
                </xs:element>
                <xs:element name="InstPart2" type="eltyp_string045" minOccurs="0">
                    <xs:annotation>
                        <xs:documentation>Informations libre émetteur</xs:documentation>
                    </xs:annotation>
                </xs:element>
                <xs:element name="NumCR" type="eltyp_string011" minOccurs="0">
                    <xs:annotation>
                        <xs:documentation>Informations libre émetteur</xs:documentation>
                    </xs:annotation>
                </xs:element>
                <xs:element name="MonTransHT" minOccurs="0">
                    <xs:annotation>
                        <xs:documentation>Montant du transport HT  En Centimes</xs:documentation>
                    </xs:annotation>
                    <xs:simpleType>
                        <xs:restriction base="eltyp_string009">
                            <xs:pattern value="[0-9]{9}" />
                        </xs:restriction>
                    </xs:simpleType>
                </xs:element>
                <xs:element name="MonTransTTC" minOccurs="0">
                    <xs:annotation>
                        <xs:documentation>Montant du transport TTC En Centimes</xs:documentation>
                    </xs:annotation>
                    <xs:simpleType>
                        <xs:restriction base="eltyp_string009">
                            <xs:pattern value="[0-9]{9}" />
                        </xs:restriction>
                    </xs:simpleType>
                </xs:element>
                <xs:element name="MonTransTVA" minOccurs="0">
                    <xs:annotation>
                        <xs:documentation>Montant du transport TVA En Centimes</xs:documentation>
                    </xs:annotation>
                    <xs:simpleType>
                        <xs:restriction base="eltyp_string009">
                            <xs:pattern value="[0-9]{9}" />
                        </xs:restriction>
                    </xs:simpleType>
                </xs:element>
                <xs:element name="ValDevTrans" minOccurs="0" default="EUR">
                    <xs:annotation>
                        <xs:documentation>Devise valeur Assurance</xs:documentation>
                    </xs:annotation>
                    <xs:simpleType>
                        <xs:restriction base="eltyp_string003">
                            <xs:enumeration value="EUR" />
                        </xs:restriction>
                    </xs:simpleType>
                </xs:element>
                <xs:element name="RefSwap" type="eltyp_string045" minOccurs="0">
                    <xs:annotation>
                        <xs:documentation>Référence SWAP</xs:documentation>
                    </xs:annotation>
                </xs:element>
                <xs:element name="NumColEmet" type="eltyp_string035" minOccurs="0">
                    <xs:annotation>
                        <xs:documentation>N° interne du colis chez l'émetteur Ligne à disposition émetteur</xs:documentation>
                    </xs:annotation>
                </xs:element>
                <xs:element name="RefExp" type="eltyp_string035" minOccurs="0">
                    <xs:annotation>
                        <xs:documentation>Référence Expéditeur Ligne à disposition émetteur</xs:documentation>
                    </xs:annotation>
                </xs:element>
            </xs:sequence>
        </xs:complexType>
    </xs:element>
</xs:schema>

普通的:

<?xml version="1.0" encoding="utf-8" ?>
<!--Created with Liquid XML 2015 Designer Edition 13.1.0.5909 (http://www.liquid-technologies.com)-->
<!--Version 1.0.1 du 24/06/2015  -->
<!--Version 1.0.2 du 07/07/2015  -->
<!--Version 1.0.3 du 16/07/2015  -->
<!--Version 1.0.4 du 21/07/2015  - Correction liste des trans/modtrans/Col -->
<xs:schema elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsd="http://www.w3.org/2001/XMLSchema" >
    <!--Element De base -->

    <xs:simpleType name="eltyp_string032">
        <xsd:restriction base="xsd:string">
            <xsd:maxLength value="32" />
        </xsd:restriction>
    </xs:simpleType>
    <xs:simpleType name="eltyp_string035">
        <xsd:restriction base="xsd:string">
            <xsd:maxLength value="35" />
        </xsd:restriction>
    </xs:simpleType>
    <xs:simpleType name="eltyp_string020">
        <xsd:restriction base="xsd:string">
            <xsd:maxLength value="20" />
        </xsd:restriction>
    </xs:simpleType>
    <xs:simpleType name="eltyp_string009">
        <xsd:restriction base="xsd:string">
            <xsd:maxLength value="9" />
        </xsd:restriction>
    </xs:simpleType>
    <xs:simpleType name="eltyp_string003">
        <xsd:restriction base="xsd:string">
            <xsd:maxLength value="3" />
        </xsd:restriction>
    </xs:simpleType>
    <xs:simpleType name="eltyp_string004">
        <xsd:restriction base="xsd:string">
            <xsd:maxLength value="4" />
        </xsd:restriction>
    </xs:simpleType>
    <xs:simpleType name="eltyp_string013">
        <xs:restriction base="xs:string">
            <xs:maxLength value="13" />
        </xs:restriction>
    </xs:simpleType>
    <xs:simpleType name="eltyp_string045">
        <xs:restriction base="xs:string">
            <xs:maxLength value="45" />
        </xs:restriction>
    </xs:simpleType>
    <xs:simpleType name="eltyp_string001">
        <xs:restriction base="xs:string">
            <xs:maxLength value="1" />
        </xs:restriction>
    </xs:simpleType>
    <xs:simpleType name="eltyp_string011">
        <xs:restriction base="xs:string">
            <xs:maxLength value="11" />
        </xs:restriction>
    </xs:simpleType>
    <xs:simpleType name="Eltyp_DateAAAAMMJJHHMM">
        <xs:restriction base="xs:string"/>
    </xs:simpleType>
</xs:schema>
<!--Fin des ꭩments de type complexes-->

答案 1 :(得分:0)

我发现了答案。实际上xsd代码没有任何问题,我故意在代码中保留targetNamespace="testNamespaces""xmlns="testNamespaces",因为我收到了未声明的type元素错误,并在此处找到了一些解决方案{ {3}}但这不是我的问题。

我的问题是我在 Notepad ++ 中打开了xsd文件,因此一些不可见的数据被附加到写入的文本中。所以当我打开@Petru写的代码并粘贴它时,它可以工作。但是我给他的只是我xsd文件的一小部分。但是当我为我的大文件整合他的代码时,它无法工作。

突然想到让我们在Visual Studio中打开.xsd,当我在那里打开它时它可以工作。 :)