xml XSD 文件的用途是什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3403644/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is the purpose of XSD files?
提问by Red Swan
Since we can query on the XML file from C# (.NET), why do we need an XSD file? I know it is metadata file of particular XML file. We can specify the relationships in XSD, but what is its functioning then?
既然我们可以从 C# (.NET) 查询 XML 文件,为什么我们需要一个 XSD 文件?我知道它是特定 XML 文件的元数据文件。我们可以在 XSD 中指定关系,但它的功能是什么?
XML
XML
<?xml version="1.0" encoding="utf-8" ?>
<Root>
<Customers>
<Customer CustomerID="GREAL">
<CompanyName>Great Lakes Food Market</CompanyName>
<ContactName>Howard Snyder</ContactName>
<ContactTitle>Marketing Manager</ContactTitle>
<Phone>(503) 555-7555</Phone>
<FullAddress>
<Address>2732 Baker Blvd.</Address>
<City>Eugene</City>
<Region>OR</Region>
<PostalCode>97403</PostalCode>
<Country>USA</Country>
</FullAddress>
</Customer>
</Customers>
<Orders>
<Order>
<CustomerID>GREAL</CustomerID>
<EmployeeID>6</EmployeeID>
<OrderDate>1997-05-06T00:00:00</OrderDate>
<RequiredDate>1997-05-20T00:00:00</RequiredDate>
<ShipInfo ShippedDate="1997-05-09T00:00:00">
<ShipVia>2</ShipVia>
<Freight>3.35</Freight>
<ShipName>Great Lakes Food Market</ShipName>
<ShipAddress>2732 Baker Blvd.</ShipAddress>
<ShipCity>Eugene</ShipCity>
<ShipRegion>OR</ShipRegion>
<ShipPostalCode>97403</ShipPostalCode>
<ShipCountry>USA</ShipCountry>
</ShipInfo>
</Order>
<Order>
<CustomerID>GREAL</CustomerID>
<EmployeeID>8</EmployeeID>
<OrderDate>1997-07-04T00:00:00</OrderDate>
<RequiredDate>1997-08-01T00:00:00</RequiredDate>
<ShipInfo ShippedDate="1997-07-14T00:00:00">
<ShipVia>2</ShipVia>
<Freight>4.42</Freight>
<ShipName>Great Lakes Food Market</ShipName>
<ShipAddress>2732 Baker Blvd.</ShipAddress>
<ShipCity>Eugene</ShipCity>
<ShipRegion>OR</ShipRegion>
<ShipPostalCode>97403</ShipPostalCode>
<ShipCountry>USA</ShipCountry>
</ShipInfo>
</Order>
</Orders>
</Root>
I want to get data from the Orderelements according to a provided CustomerID.
我想Order根据提供的元素从元素中获取数据CustomerID。
Also: What is the purpose of giving the relationships in XSD?
另外:在XSD中给出关系的目的是什么?
回答by Oded
XSDfiles are used to validate that XML files conform to a certain format.
XSD文件用于验证 XML 文件是否符合某种格式。
In that respect they are similar to DTDs that existed before them.
在这方面,它们类似于之前存在的DTD。
The main difference between XSD and DTD is that XSD is written in XML and is considered easier to read and understand.
XSD 和 DTD 之间的主要区别在于 XSD 是用 XML 编写的,并且被认为更易于阅读和理解。
回答by Oleg
Without XML Schema (XSD file) an XML file is a relatively free set of elements and attributes. The XSD file defines which elements and attributes are permitted and in which order.
如果没有 XML 模式(XSD 文件),XML 文件是一组相对自由的元素和属性。XSD 文件定义允许哪些元素和属性以及以何种顺序。
In general XML is a metalanguage. XSD files define specific languages within that metalanguage. For example, if your XSD file contains the definition of XHTML 1.0, then your XML file is required to fit XHTML 1.0 rather than some other format.
一般来说,XML 是一种元语言。XSD 文件定义该元语言中的特定语言。例如,如果您的 XSD 文件包含 XHTML 1.0 的定义,那么您的 XML 文件需要适合 XHTML 1.0 而不是其他某种格式。
回答by Garry
You mention C# in your question so it may help to think of as XSD as serving a similar role to a C# interface.
您在问题中提到了 C#,因此将 XSD 视为与 C# 接口类似的角色可能会有所帮助。
It defines what the XML should 'look like' in a similar way that an interface defines what a class should implement.
它以类似于接口定义类应该实现什么的方式定义 XML 应该“是什么样子”。
回答by kjhughes
XSDs constrain the vocabulary and structure of XML documents.
XSD 限制了 XML 文档的词汇和结构。
- Without an XSD, an XML document need only follow the rules for being well-formedas given in the W3C XML Recommendation.
- With an XSD, an XML document must adhere to additional constraints placed upon the names and values of its elements and attributes in order to be considered validagainst the XSD per the W3C XML Schema Recommendation.
- 如果没有 XSD,XML 文档只需要遵循W3C XML Recommendation 中给出的格式良好的规则。
- 对于 XSD,XML 文档必须遵守对其元素和属性的名称和值施加的附加约束,以便根据W3C XML 模式建议被视为对 XSD有效。
XML is all about agreement, and XSDs provide the means for structuring and communicating the agreement beyond the basic definition of XML itself.
XML 完全是关于协议的,XSD 提供了在 XML 本身的基本定义之外构建和传达协议的方法。
回答by Robert
Also questions is: What is the purpose of giving the relationships in xsd.
还有一个问题是:在 xsd 中给出关系的目的是什么。
Suppose you want to generate some XML for an external party's tool, or similar - how would you know what structure it is allowed to follow to be used correctly for their tool? you write to a schema. Likewise if you want other people to use your tool, you would write a schema for them to follow. It may also be useful for validating your own XML.
假设您想为外部方的工具或类似工具生成一些 XML - 您如何知道允许遵循什么结构以正确用于他们的工具?您写入架构。同样,如果您希望其他人使用您的工具,您可以编写一个供他们遵循的模式。它也可用于验证您自己的 XML。
回答by Premraj
Before understanding the XSD(XML Schema Definition) let me explain;
在理解XSD(XML Schema Definition)之前让我解释一下;
What is schema?
什么是架构?
for example; emailID: peter#gmail
例如; 电子邮件 ID:彼得#gmail
You can identify the above emailID is not valid because there is no @, .com or .net or .org.
您可以确定上面的emailID 无效,因为没有@、.com 或.net 或.org。
We know the email schema it looks like [email protected].
我们知道它看起来像 [email protected] 的电子邮件架构。
Conclusion: Schema does not validate the data, It does the validation of structure.
结论:模式不验证数据,它验证结构。
XSD is actually one of the implementation of XML Schema. others we have relaxng
XSD 实际上是 XML Schema 的一种实现。其他我们有放松
We use XSD to validate XML data.
我们使用 XSD 来验证 XML 数据。
回答by troelskn
An XSD is a formal contract that specifies how an XML document can be formed. It is often used to validate an XML document, or to generate code from.
XSD 是一种正式合同,它指定了如何形成 XML 文档。它通常用于验证 XML 文档或从中生成代码。
回答by troelskn
An .xsd file is called an XML schema. Via an XML schema, we may require a certain structure in a given XML - which elements in which order, how many times, with which attributes, how they are nested, etc. If we have a schema for our XML input, we can verify that it contains the data we need it to contain, and nothing else, with a few lines invoking a schema validator.
.xsd 文件称为 XML 模式。通过 XML 模式,我们可能需要给定 XML 中的某种结构 - 哪些元素按什么顺序、多少次、哪些属性、它们如何嵌套等。如果我们有 XML 输入的模式,我们可以验证它包含我们需要它包含的数据,没有别的,只有几行调用模式验证器。
回答by a'r
An XSD file is an XML Schema Definition and it is used to provide a standard method of checking that a given XML document conforms to what you expect.
XSD 文件是一个 XML 模式定义,它用于提供一种标准方法来检查给定的 XML 文档是否符合您的期望。
回答by mort
The xsd file is the schema of the xml file - it defines which elements may occur and their restrictions (like amount, order, boundaries, relationships,...)
xsd 文件是 xml 文件的架构 - 它定义了可能出现的元素及其限制(如数量、顺序、边界、关系……)

