如何将XML文件与XSD架构链接



我是XML的新手。有人能帮我把XSD连接到XML文件吗?

这是我的XML:

<WorldWideHappiness
xmlns="https://www.w3schools.com"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="https://www.w3schools.com/xml Hapiness.xsd">
<Record>
<Country>
<Name>The Netherlands</Name>
<Region>Western Europe</Region>
<HappinessRank>7</HappinessRank>
<HappinessScore>7.339</HappinessScore>
<LowerConfidenceInterval>7.284</LowerConfidenceInterval>
<UpperConfidenceInterval>7.394</UpperConfidenceInterval>
<Health>0.81231</Health>
</Country>
</Record>
</WorldWideHappiness>

这是我的XSD:

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="https://www.w3schools.com" xmlns="https://www.w3schools.com">
<xs:element name="WorldWideHappiness" type="WorldWideStatistics"/>
<xs:complexType name="WorldWideStatistics">
<xs:sequence>
<xs:element name="Country" type="CountryType"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="CountryType">
<xs:sequence>
<xs:element name="Name" type="xs:integer"/>
<xs:element name="Region" type="xs:string"/>
<xs:element name="HappinessRank" type="xs:int"/>
<xs:element name="HappinessScore" type="xs:double"/>
<xs:element name="LowerConfidenceInterval" type="xs:double"/>
<xs:element name="UpperConfidenceInterval" type="xs:double"/>
<xs:element name="Health" type="xs:double"/>
</xs:sequence>
</xs:complexType>
</xs:schema>

需要的修复包括

  1. 将XML中的命名空间与XSD的targetnamespace(检查(和xsi:schemaLocation中的一个命名空间文件对(修复(相匹配
  2. elementFormDefault="qualified"添加到XSD中。请参阅此答案了解原因
  3. 修复XML中的Name以匹配XSD类型

总之,以下XML对以下XSD有效:

XML

<WorldWideHappiness
xmlns="https://www.w3schools.com"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="https://www.w3schools.com Hapiness.xsd">
<Country>
<Name>1</Name>
<Region>Western Europe</Region>
<HappinessRank>7</HappinessRank>
<HappinessScore>7.339</HappinessScore>
<LowerConfidenceInterval>7.284</LowerConfidenceInterval>
<UpperConfidenceInterval>7.394</UpperConfidenceInterval>
<Health>0.81231</Health>
</Country>
</WorldWideHappiness>

XSD

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="https://www.w3schools.com"
elementFormDefault="qualified"
xmlns="https://www.w3schools.com">
<xs:element name="WorldWideHappiness" type="WorldWideStatistics"/>
<xs:complexType name="WorldWideStatistics">
<xs:sequence>
<xs:element name="Country" type="CountryType"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="CountryType">
<xs:sequence>
<xs:element name="Name" type="xs:integer"/>
<xs:element name="Region" type="xs:string"/>
<xs:element name="HappinessRank" type="xs:int"/>
<xs:element name="HappinessScore" type="xs:double"/>
<xs:element name="LowerConfidenceInterval" type="xs:double"/>
<xs:element name="UpperConfidenceInterval" type="xs:double"/>
<xs:element name="Health" type="xs:double"/>
</xs:sequence>
</xs:complexType>
</xs:schema>

另请参阅

  • 如何使用schemaLocation或noNamespaceSchemaLocation将XML链接到XSD

相关内容

  • 没有找到相关文章

最新更新