XSD未正确验证XML [英] XSD not validating XML correctly

查看:303
本文介绍了XSD未正确验证XML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个XSD& XMLSpy通过连接到MySQL客户表为我创建的XML。我已经删除了许多列,因为这是浪费空间。



XSD:customer.xsd

 <?  xml     version   =  1.0    encoding   =  UTF-8  >  
< xs:schema xmlns:xs = http://www.w3.org/2001/XMLSchema elementFormDe故障 = 合格 attributeFormDefault = 不合格 >
< xs:element name = customer >
< xs:complexType >
< xs:sequence >
< xs:element 名称 = customer_PK >
< xs:simpleType >
< xs:restriction base = xs:unsignedInt >
< xs:minInclusive = 0 / >
< xs:maxInclusive value = 4294967295 / >
< / xs:restriction >
< / xs:simpleType >
< / xs:element >

< xs:key 名称 = customer_PrimaryKey_1 >
< xs:selector xpath = / >
< xs:field xpath = customer_PK / >
< / xs:key >

< / xs:element >
< / xs :schema >



现在为XML:customer.xml

我无法显示代码,因此我更换了<用[。

 <? xml version =   1.0 encodong =   UTF -8 ?>  
< 导入 名称 = customer

xmlns = http:// .....

xsi:noNamespaceSchemaLocation = customer.xsd >
< customer >
< customer >
< / Import >

XSD的XML验证无法识别XML中的Import元素。我得到一个cvc-assessment-elt.1:元素声明和类型定义都不知道元素[导入]。严格评估失败。



任何想法如何让XSD和XML相同?

解决方案

首先,如果您了解XML,您可能应该知道HTML足以输入您的代码正确的。我已经为你做了,没有修复你的错误。显然,这不是格式良好的XML。请找到拼写错误和未封闭的标签。当你提问时,你需要复制确切的代码。如果这是真的代码,不是Schema定义来检测问题。不是格式良好的XML不是XML。



当然,每个人都可以看到元素导入未在您的架构中定义。您不可能期望从这对中获得任何好处:XML示例和完全无法匹配的架构。



-SA


除了解决方案1之外Sergey Alexandrovich Kryukov [ ^ ],我建议阅读这些文章:

使用XmlSchemaSet进行XML模式(XSD)验证 [ ^ ]

使用架构验证XML [ ^ ]



提示:

架构需要客户元素及其字段 customer_PK ,但xml中的第一个元素是 Import ,因此xml文件应如下所示:

 <  客户 >  
< customer_PK > 1 < / customer_PK >
< / customer >



如果您想存储多个客户,则需要将架构和xml结构更改为:

 <   customers  >  
< customer >
< customer_PK > 1 < / customer_PK >
< / customer >
< customer >
< < span class =code-leadattribute> customer_PK > 2 < / customer_PK >
< < span class =code-leadattribute> / customer >
< / customers >


I have an XSD & an XML that XMLSpy created for me from connecting to a MySQL customer table. I have cut many of the columns out because it's a waste of space.

XSD: customer.xsd

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified">
    <xs:element name="customer">
        <xs:complexType>
            <xs:sequence>
        <xs:element name="customer_PK">
          <xs:simpleType>
            <xs:restriction base="xs:unsignedInt">
              <xs:minInclusive value="0"/>
              <xs:maxInclusive value="4294967295"/>
            </xs:restriction>
          </xs:simpleType>
        </xs:element>                
   
        <xs:key name="customer_PrimaryKey_1">
            <xs:selector xpath="."/>
            <xs:field xpath="customer_PK"/>
        </xs:key>
       
    </xs:element>
</xs:schema>


NOW FOR THE XML: customer.xml
I could not get the code to display so I have replaced "<" with "[".

<?xml version="1.0" encodong="UTF-8"?>
<Import name = "customer"

xmlns="http://....."

   xsi:noNamespaceSchemaLocation="customer.xsd">
<customer>
<customer>
</Import>

XML verification by the XSD doesn't recognize the "Import" element in the XML. I get a "cvc-assess-elt.1: Neither an element declaration nor a type defintion is known for element [Import]. Strict assessment failed.

ANY IDEAS how to get the XSD & XML the same?

解决方案

First of all, you if know XML, you probably should know HTML enough to be able to enter your code correctly. I've done it for you, without fixing your bugs. Apparently, this is no well-formed XML. Please find the typo and unclosed tags. When you ask questions, you need to copy exact code. If this is your real code, it's not to Schema definition to detect the problem. Not well-formed XML is not XML.

And of course, everyone can see that the element Import is not defined in your schema. You could not possibly expect anything good from this pair: XML sample and totally unmatched schema.

—SA


In addition to solution 1 by Sergey Alexandrovich Kryukov[^], i'd suggest to read these articles:
XML Schema (XSD) Validation with XmlSchemaSet[^]
Validation of XML with Schemas[^]

Tip:
Schema expects customer element and its field customer_PK, but the first element in xml is Import, so xml file should looks like:

<customer>
    <customer_PK>1</customer_PK>
</customer>


If you would like to store several customers, you need to change schema and xml structure to:

<customers>
    <customer>
        <customer_PK>1</customer_PK>
    </customer>
    <customer>
        <customer_PK>2</customer_PK>
    </customer>
</customers>


这篇关于XSD未正确验证XML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆