做elementFormDefault什么,什么时候应该使用?
所以我找到了一些elementFormDefault值的定义:
qualified - 元素和属性位于架构的targetNamespace中
不合格 - 元素和属性没有命名空间
因此,根据该定义,我认为如果将模式设置为限定,那么为什么必须在类型前加上命名空间?那些你甚至有一套不合格的场景是什么?我尝试使用谷歌搜索,但我得到的只是一些非常难以理解的W3C页面.
这是我现在正在使用的文件,为什么我需要声明类型,target:TypeAssignments因为我声明它targetNamespace是同一个xmlns:target?
<?xml version="1.0" encoding="UTF-8"?>
<schema xmlns="http://www.w3.org/2001/XMLSchema"
xmlns:target="http://www.levijackson.net/web340/ns"
targetNamespace="http://www.levijackson.net/web340/ns"
elementFormDefault="qualified">
<element name="assignments">
<complexType>
<sequence>
<element name="assignments" type="target:TypeAssignments"
minOccurs="1" maxOccurs="unbounded"/>
</sequence>
</complexType>
</element>
<complexType name="TypeAssignments">
<sequence>
<element name="assignment" type="target:assignmentInfo"
minOccurs="0" maxOccurs="unbounded"/>
</sequence>
</complexType>
<complexType name="assignmentInfo">
<sequence>
<element name="name" type="string"/>
<element name="page" type="target:TypePage"/>
<element name="file" type="target:TypeFile"
minOccurs="0" maxOccurs="unbounded"/>
</sequence>
<attribute name="id" type="string" use="required"/>
</complexType>
<simpleType name="TypePage">
<restriction base="integer">
<minInclusive value="50" />
<maxInclusive value="498" /> …Run Code Online (Sandbox Code Playgroud) 我正在使用xsd:all复杂的类型.当我错过任何强制性元素,同时验证它将显示所有元素.它不会显示确切的遗漏元素.
但如果我使用,xsd:sequence我可以得到确切的遗漏元素.
这两者有什么区别吗?
xsd:sequence:XML元素必须处于相同的顺序.
但是xsd:all:XML元素可以是任何顺序.
假设我有一个定义以下XML的模式:
<Values>
<Add Key="Key1">Value 1</Add>
<Add Key="Key2">Value 2</Add>
<Add Key="Key3">Value 3</Add>
<Add Key="Key4">Value 4</Add>
</Values>
Run Code Online (Sandbox Code Playgroud)
我希望,在模式级别,能够强制Key属性的值是唯一的,即上面的示例是有效的,但以下示例将无效:
<Values>
<Add Key="Key1">Value 1</Add>
<Add Key="Key2">Value 2</Add>
<Add Key="Key2">Value 3</Add>
<Add Key="Key3">Value 4</Add>
</Values>
Run Code Online (Sandbox Code Playgroud)
请注意,有两个Add元素与Key中Key2
这里参考的是简单的模式:
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified">
<xs:element name="Values">
<xs:complexType>
<xs:sequence>
<xs:element name="Add" maxOccurs="unbounded">
<xs:complexType>
<xs:simpleContent>
<xs:extension base="xs:string">
<xs:attribute name="Key" type="xs:token" use="required"/>
</xs:extension>
</xs:simpleContent>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
Run Code Online (Sandbox Code Playgroud)
我的印象是,这在模式层面是不可能的,但是我很满意.
对于以下XML片段:
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd">
Run Code Online (Sandbox Code Playgroud)
什么的xmlns,xmlns:xsi和xsi:schemaLocation属性究竟是什么意思?它们有什么关系?有什么:用?
并且有2个网址 xsi:schemaLocation=
xmlns但访问时出现404错误.)如果1不存在,为什么还要把它放在那里?
我有以下XSD代码:
<xsd:complexType name="questions">
<xsd:sequence>
<xsd:element name="location" type="location"/>
<xsd:element name="multipleChoiceInput" type="multipleChoiceInput" minOccurs="0" maxOccurs="unbounded"/>
<xsd:element name="textInput" type="textInput" minOccurs="0" maxOccurs="unbounded"/>
<xsd:element name="pictureInput" type="pictureInput" minOccurs="0"/>
</xsd:sequence>
</xsd:complexType>
Run Code Online (Sandbox Code Playgroud)
这里的问题是:元素位置,multipleChoiceInput等必须以它们声明的相同顺序出现.我不希望这种情况发生,我希望在验证过程中序列不应该相关.我怎样才能做到这一点?
我尝试过的另一种可能性是:
<xsd:complexType name="questions">
<xsd:choice maxOccurs="unbounded">
<xsd:element name="location" type="location"/>
<xsd:element name="multipleChoiceInput" type="multipleChoiceInput" minOccurs="0" maxOccurs="unbounded"/>
<xsd:element name="textInput" type="textInput" minOccurs="0" maxOccurs="unbounded"/>
<xsd:element name="pictureInput" type="pictureInput" minOccurs="0" maxOccurs="1"/>
</xsd:choice>
</xsd:complexType>
Run Code Online (Sandbox Code Playgroud)
在这个例子中,序列真的无关紧要,我可以拥有我想要的那么多元素("所有"不允许我做什么).但我仍然遇到min-和maxOccurs的问题.在这个例子中,我可以拥有尽可能多的"pictureInput",但是我希望拥有0或1的约束.
非常感谢您的帮助!
我正在尝试使用xsd验证一个非常简单的xml,但由于某种原因我得到了这个错误.如果有人能解释我为什么,我真的很感激.
XML文件
<?xml version="1.0" encoding="utf-8"?>
<MyElement>A</MyElement>
Run Code Online (Sandbox Code Playgroud)
XSD文件
<?xml version="1.0" encoding="UTF-8"?>
<schema xmlns="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.example.org/Test"
xmlns:tns="http://www.example.org/Test"
elementFormDefault="qualified">
<simpleType name="MyType">
<restriction base="string"></restriction>
</simpleType>
<element name="MyElement" type="tns:MyType"></element>
</schema>
Run Code Online (Sandbox Code Playgroud) <url>
<loc>http://www.ezed.in/ezed/courseDemoIntroPage.do?courseId=COU10000000138003530&checkingCourseFrom=preLogin#.U2DcKvmSySo</loc>
</url>
Run Code Online (Sandbox Code Playgroud)
第103行第102行的错误:EntityRef:expecting';'
无法弄清楚可能是什么问题.
也许是我,但似乎你有一个XSD
<?xml version="1.0" encoding="utf-8"?>
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="User">
<xs:complexType>
<xs:sequence>
<xs:element name="GivenName" />
<xs:element name="SurName" />
</xs:sequence>
<xs:attribute name="ID" type="xs:unsignedByte" use="required" />
</xs:complexType>
</xs:element>
</xs:schema>
Run Code Online (Sandbox Code Playgroud)
它定义了此文档的架构
<?xml version="1.0" encoding="utf-8" ?>
<User ID="1">
<GivenName></GivenName>
<SurName></SurName>
</User>
Run Code Online (Sandbox Code Playgroud)
它将无法验证您是否添加了另一个元素,例如EmailAddress,并混淆了订单
<?xml version="1.0" encoding="utf-8" ?>
<User ID="1">
<SurName></SurName>
<EmailAddress></EmailAddress>
<GivenName></GivenName>
</User>
Run Code Online (Sandbox Code Playgroud)
我不想将EmailAddress添加到文档中并将其标记为可选.
我只想要一个XSD来验证文档必须满足的最低要求.
有没有办法做到这一点?
marc_s在下面指出你可以使用xs:any内部xs:sequence允许更多元素,遗憾的是,你必须维护元素的顺序.
或者,我可以使用xs:all哪个不强制执行元素的顺序,但是,唉,不允许我放在xs:any它内部.
我对XML Schema等仍然有点新,并且一直致力于开发一些XML,Schema和样式表(XSLT).我已经取得了合理的进展,但随后意识到我的架构已停止工作,所以我把它带回了一个更简单的非描述性示例.
这是我的XML:
<?xml version="1.0" encoding="UTF-8"?>
<Root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="Test.Namespace"
schemaLocation="http://myNameSpace.com Test1.xsd">
<element1 id="001">
<element2 id="001.1">
<element3 id="001.1" />
</element2>
</element1>
</Root>
Run Code Online (Sandbox Code Playgroud)
我写了一个Schema,它在这里:
<?xml version="1.0"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
targetNamespace="Test.Namespace"
elementFormDefault="qualified">
<xsd:element name="Root">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="element1" maxOccurs="unbounded" type="element1Type"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
<xsd:complexType name="element1Type">
<xsd:sequence>
<xsd:element name="element2" maxOccurs="unbounded" type="element2Type"/>
</xsd:sequence>
<xsd:attribute name="id" type="xsd:string"/>
</xsd:complexType>
<xsd:complexType name="element2Type">
<xsd:sequence>
<xsd:element name="item" type="element3Type"/>
</xsd:sequence>
<xsd:attribute name="id" type="xsd:string"/>
</xsd:complexType>
<xsd:complexType name="element3Type">
<xsd:attribute name="id" type="xsd:string"/>
</xsd:complexType>
</xsd:schema>
Run Code Online (Sandbox Code Playgroud)
Schema代表了我真正的XML结构.
现在,当我尝试验证我的XML时,我收到此错误:
cvc-elt.1: Cannot find the declaration of element 'Root'. [7] …
这是我第一次使用XSD验证XML.
要验证的XML文件:
<?xml version="1.0" encoding="utf-8" ?>
<config xmlns="Schemas" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="config.xsd">
<levelVariant>
<filePath>SampleVariant</filePath>
</levelVariant>
<levelVariant>
<filePath>LegendaryMode</filePath>
</levelVariant>
<levelVariant>
<filePath>AmazingMode</filePath>
</levelVariant>
</config>
Run Code Online (Sandbox Code Playgroud)
XSD,位于"Schemas/config.xsd"中,相对于要验证的XML文件:
<?xml version="1.0" encoding="utf-8" ?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">
<xs:element name="config">
<xs:complexType>
<xs:sequence>
<xs:element name="levelVariant">
<xs:complexType>
<xs:sequence>
<xs:element name="filePath" type="xs:anyURI">
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
Run Code Online (Sandbox Code Playgroud)
现在,我只想准确地验证当前出现的XML文件.一旦我更好地理解了这一点,我就会进一步扩展.对于像XML文件一样简单的东西,我真的需要这么多行吗?
C#中的验证码:
public void SetURI(string uri)
{
XElement toValidate = XElement.Load(Path.Combine(PATH_TO_DATA_DIR, uri) + ".xml");
// begin confusion
// exception here
string schemaURI = toValidate.Attributes("xmlns").First().ToString()
+ toValidate.Attributes("xsi:noNamespaceSchemaLocation").First().ToString();
XmlSchemaSet schemas = new XmlSchemaSet();
schemas.Add(null, schemaURI); …Run Code Online (Sandbox Code Playgroud)