用于交替元素的XSD

时间:2015-06-14 18:47:27

标签: xml xsd

我有以下XML:

<?xml version="1.0" encoding="utf-8"?>
<main>
  <bbcor Location="string" Name="string">
    <PipeDiameter_INCH>3.1415926535</PipeDiameter_INCH>
    <InitialPressure_PSI>3335.566</InitialPressure_PSI>
    <LWISE TotalSection="2">
      <DL_FT>535.00</DL_FT>            <HL_FT>653.00</HL_FT>
      <DL_FT>245.98</DL_FT>            <HL_FT>395.0</HL_FT>
    </LWISE>
  </bbcor>
</main>

我正在尝试为上面的文件制作一个XSD验证器,但是卡在交替元素中,即

<DL_FT>2.4</DL_FT> <HL_FT>234.00</HL_FT>
<DL_FT>3.5</DL_FT> <HL_FT>456.00</HL_FT>
<DL_FT>6.8</DL_FT> <HL_FT>678.00</HL_FT>

我尝试了在线生成器等,但没有提供所需的XSD。当前的XSD文件如下所示:

<?xml version="1.0" encoding="utf-8"?>
<xsd:schema attributeFormDefault="unqualified"
            elementFormDefault="qualified" 
            version="1.0" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <xsd:element name="main">
    <xsd:complexType>
      <xsd:sequence>
        <xsd:element name="bbcor">
          <xsd:complexType>
            <xsd:sequence>
              <xsd:element name="PipeDiameter_INCH" type="xsd:decimal" />
              <xsd:element name="InitialPressure_PSI" type="xsd:decimal" />
              <xsd:element name="LWISE">
                <xsd:complexType>
                  <xsd:sequence>
                    <xsd:element maxOccurs="unbounded" name="DL_FT" type="xsd:decimal" />
                    <xsd:element maxOccurs="unbounded" name="HL_FT" type="xsd:decimal" />
                  </xsd:sequence>
                  <xsd:attribute name="TotalSection" type="xsd:int" />
                </xsd:complexType>
              </xsd:element>
            </xsd:sequence>
            <xsd:attribute name="Location" type="xsd:string" />
            <xsd:attribute name="Name" type="xsd:string" />
          </xsd:complexType>
        </xsd:element>
      </xsd:sequence>
    </xsd:complexType>
  </xsd:element>
</xsd:schema>

上述XSD有什么问题?我正在使用xmllint来验证上面的XML文件。 XSD不允许使用备用元素吗?元素发生时就像

一样有效
<DL_FT>2.45</DL_FT> 
<DL_FT>2.44</DL_FT>
<DL_FT>2.35</DL_FT>
<HL_FT>245</HL_FT>
<HL_FT>445</HL_FT>
<HL_FT>545</HL_FT>

1 个答案:

答案 0 :(得分:2)

你非常接近。只需maxOccurs="unbounded"因子xsd:sequence {<1}}

具体来说,改变

              <xsd:sequence>
                <xsd:element maxOccurs="unbounded" name="DL_FT" type="xsd:decimal" />
                <xsd:element maxOccurs="unbounded" name="HL_FT" type="xsd:decimal" />
              </xsd:sequence>

              <xsd:sequence maxOccurs="unbounded">
                <xsd:element name="DL_FT" type="xsd:decimal" />
                <xsd:element name="HL_FT" type="xsd:decimal" />
              </xsd:sequence>

因为它是DL_FT - HL_FT 而无限期重复。<​​/ p>

<强>更新

半对

我没有提到上述解决方案只是简单的 ,因为你的例子是基于配对的。 (感谢Michael Kay的提醒。)你是否需要允许非配对的落后者,例如正常配对后的孤独DL_FT落后者,

<DL_FT>2.4</DL_FT> <HL_FT>234.00</HL_FT>
<DL_FT>3.5</DL_FT> <-- no HL_FT here -->

你会遇到一个经典的,无望的模糊内容模型问题,并且无法通过XSD检查这种模式。然而,RELAX NG可以代表这种模式。