【问题标题】:XML Schema validation using the XSD使用 XSD 进行 XML Schema 验证
【发布时间】:2012-07-12 06:18:05
【问题描述】:

我有一个如下所示的 XML,其中项目的数量可以从 0 到 n 不等。有没有办法编写 XSD 来验证架构。

<?xml version="1.0" encoding="utf-8" ?> 
<ShoppingItems> 
  <CustomerName>John</CustomerName> 
  <Address>Walstreet,Newyork</Address> 
  <Item1>Milk</Item1> 
  <Price1>1$</Price1> 
  <Item2>IceCream</Item2> 
  <Price2>1$</Price2> 
  <Item3>Bread</Item3> 
  <Price3>1$</Price3> 
  <Item4>Egg</Item4> 
  <Price4>1$</Price4>   

  <Item..n>Egg</Item..n> 
  <Price..n>1$</Price..n> 
</ShoppingItems> 

【问题讨论】:

    标签: xsd xsd-validation


    【解决方案1】:

    不是现在的形式。 XSD 定义非常严格 - 在上述情况下,您将指定所有可能的 ShoppingItems 类型(包括 Item..n 和 Price..n),这当然是不可能的。

    更好的办法是更改 XML 文件,使其更具逻辑结构:

    <?xml version="1.0" encoding="utf-8" ?> 
    <ShoppingItems> 
      <CustomerName>John</CustomerName> 
      <Address>Walstreet,Newyork</Address> 
      <Items>
        <Item price="1$">Milk</Item> 
        <Item price="3$">IceCream</Item>
        <Item price="1$">Bread</Item> 
        <Item price="1.5$">Egg</Item> 
      </Items>
    </ShoppingItems> 
    

    现在完全可以使用模式定义此文档。

    【讨论】:

      猜你喜欢
      • 2016-02-03
      • 2018-04-10
      • 2012-04-13
      • 2016-07-09
      • 1970-01-01
      • 2014-03-27
      • 2016-10-18
      • 1970-01-01
      相关资源
      最近更新 更多