2012-07-12 143 views
0

我有一个如下所示的XML,其中项目的数量可以从0到n变化。有没有办法编写XSD来验证架构。使用XSD进行XML模式验证

<?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> 

回答

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> 

现在是完全有可能这个文件与模式定义。