在XSD中使用xs:extension和xs:restriction

时间:2016-12-01 18:51:32

标签: c# xml xsd xsd-validation xml-validation

我想在我的XSD中同时使用xs:extensionsxs:restriction作为下面提到的XML

<?xml version="1.0" encoding="utf-8"?>
<records>
  <row id="1">
    <record>
        <Value class="field">486.89</Value>
    </record>
    <record>
        <Value class="field">811.49</Value>
    </record>
  </row>
  <row id="2">
    <record>
        <Value class="field">123.45</Value>
    </record>
    <record>
        <Value class="field">678.91</Value>
    </record>
  </row>
</records>

以下是我正在尝试的XSD:

<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="records">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="row">
          <xs:complexType>
            <xs:sequence>
              <xs:element name="record" maxOccurs="unbounded" minOccurs="0">
                <xs:complexType>
                  <xs:sequence>                    
                    <xs:element name="Value">
                      <xs:simpleType>
                        <xs:restriction base="xs:decimal">
                          <xs:totalDigits value="12"/>
                          <xs:fractionDigits value="2" />
                        </xs:restriction>
                      </xs:simpleType>
                      <xs:complexType>
                        <xs:simpleContent>
                          <xs:extension base="xs:decimal">
                            <xs:attribute type="xs:string" name="class" use="optional"/>
                          </xs:extension>
                        </xs:simpleContent>
                      </xs:complexType>
                    </xs:element>                    
                  </xs:sequence>
                </xs:complexType>
              </xs:element>
            </xs:sequence>
            <xs:attribute type="xs:byte" name="id" use="required"/>
          </xs:complexType>
        </xs:element>
      </xs:sequence>
    </xs:complexType>    
  </xs:element>
</xs:schema>

上面的XSD无效,这是我在Using xs:extension & xs:restriction together?找到的解决方案。它会抛出错误&#39; complexType&#39;元素已存在于内容模型中。

1 个答案:

答案 0 :(得分:3)

您必须全局定义并命名要扩展的受限Value,而不是在row下在本地定义。

另请注意,在maxOccurs="unbounded"上您需要1(或大于默认值<?xml version="1.0" encoding="utf-8"?> <xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema"> <xs:simpleType name="Decimal12.2"> <xs:restriction base="xs:decimal"> <xs:totalDigits value="12"/> <xs:fractionDigits value="2" /> </xs:restriction> </xs:simpleType> <xs:element name="records"> <xs:complexType> <xs:sequence> <xs:element name="row" maxOccurs="unbounded"> <xs:complexType> <xs:sequence> <xs:element name="record" maxOccurs="unbounded" minOccurs="0"> <xs:complexType> <xs:sequence> <xs:element name="Value"> <xs:complexType> <xs:simpleContent> <xs:extension base="Decimal12.2"> <xs:attribute type="xs:string" name="class" use="optional"/> </xs:extension> </xs:simpleContent> </xs:complexType> </xs:element> </xs:sequence> </xs:complexType> </xs:element> </xs:sequence> <xs:attribute type="xs:byte" name="id" use="required"/> </xs:complexType> </xs:element> </xs:sequence> </xs:complexType> </xs:element> </xs:schema> 的内容)。

总而言之,这个XSD将成功验证您的XML:

var departments = [
{
    "name": "Sales",
    "color": "green",
    "count": 5
}, 
{
    "name": "Tech Lead",
    "color": "red",
    "count": 8
}, 
{
    "name": "HR",
    "color": "orange",
    "count": 3
}, 
{
    "name": "Development",
    "color": "blue",
    "count": 12
}, 
{
    "name": "QA",
    "color": "pink",
    "count": 6
}, 
{
    "name": "Finance",
    "color": "purple",
    "count": 9
}, 
{
    "name": "PL",
    "color": "gray",
    "count": 1
}, 
{
    "name": "Marketing",
    "color": "yellow",
    "count": 4
}
];

var innerRadius = 50;
var outerRadius = 200;
var maxLeaveCount = departments.reduce(function(max, department) {
    return (max < department.count) ? department.count : max;
}, 0);

var svgContainer = d3.select("#container").append("svg")
.attr("width", 3 * outerRadius)
.attr("height", 3 * outerRadius);


var arc = d3.svg.arc()
.innerRadius(innerRadius)
.outerRadius(function(d) {
    (d.count / maxLeaveCount) * (outerRadius / 0.9)
})
.startAngle(function(d, i) {
    return (2 * Math.PI * i) / departments.length;
})
.endAngle(function(d, i) {
    return (2 * Math.PI * i) / departments.length + (2 * Math.PI) / departments.length;
})

svgContainer
.selectAll("path")
.data(departments)
.enter()
.append("svg:path")
.attr("d", arc)
.attr("transform", "translate(" + (3 * outerRadius) / 2 + "," + (3 * outerRadius) / 2 + ")")
.style("fill", function(d) {
    return d.color;
})