Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Xml 需要基于位置和属性值的元素_Xml_Xsd_Attributes_Element_Xsd 1.0 - Fatal编程技术网

Xml 需要基于位置和属性值的元素

Xml 需要基于位置和属性值的元素,xml,xsd,attributes,element,xsd-1.0,Xml,Xsd,Attributes,Element,Xsd 1.0,我有以下XSD(XSD的一部分) 在我的XML中,我有: <?xml version="1.0" encoding="UTF-8"?> <Record xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="assertion.xsd"> <SSN>33333332</SSN> <sourceValue

我有以下XSD(XSD的一部分)


在我的XML中,我有:

<?xml version="1.0" encoding="UTF-8"?>
<Record xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:noNamespaceSchemaLocation="assertion.xsd">
  <SSN>33333332</SSN>
  <sourceValue label="nrA">33333333</sourceValue>
  <sourceValue label="nrB">111111111</sourceValue>
  <Data>
    <Patient>
      <DateOfBirth>03-04-2000</DateOfBirth>
      <Sexe>M</Sexe>
      <Name>Patient A</Name>
    </Patient>
  </Data>
</Record>

33333332
33333333
111111111
03-04-2000
M
病人A
我想以这样一种方式更改XSD,即当sourceValue with label=“nrA”是必需的,而with label=“nrB”是可选的。但我不知道如何做到这一点。

XSD 1.0 不可能。相反,您应该为这两种情况使用不同的元素名称

XSD 1.1 仍然建议使用不同的元素名称,但如果必须遵循当前的方法,则可以使用断言:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
  xmlns:vc="http://www.w3.org/2007/XMLSchema-versioning" vc:minVersion="1.1">

  <xs:element name="Record">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="SSN" type="xs:string"/>
        <xs:element ref="sourceValue"/>
        <xs:element ref="sourceValue" minOccurs="0"/>
        <xs:element name="Data">
          <xs:complexType>
            <xs:sequence>
              <xs:element name="Patient">
                <xs:complexType>
                  <xs:sequence>
                    <xs:element name="DateOfBirth" type="xs:string"/>
                    <xs:element name="Sexe" type="xs:string"/>
                    <xs:element name="Name" type="xs:string"/>
                  </xs:sequence>
                </xs:complexType>
              </xs:element>
            </xs:sequence>
          </xs:complexType>
        </xs:element>
      </xs:sequence>
      <xs:assert test="sourceValue[1]/@label = 'nrA'"/>
      <xs:assert test="not(sourceValue[2]) or sourceValue[2]/@label = 'nrA'"/>
    </xs:complexType>
  </xs:element>

  <xs:element name="sourceValue">
    <xs:complexType>
      <xs:simpleContent>
        <xs:extension base="xs:string">
          <xs:attribute name="label" type="xs:string"/>
        </xs:extension>
      </xs:simpleContent>
    </xs:complexType>
  </xs:element>
</xs:schema>


您能提供有效文档和无效文档的样本吗?另外,您希望使用哪一版本的模式语言?在当前版本1.1中,您可以将断言与XPath结合使用,这比1.0提供了更高的表达能力。此外,根据
标签的值
,什么是可选的还是必需的?听起来你是在说
sourceValue
元素本身的必要性应该取决于它的一个属性的值,但这是没有意义的。嗨@Martin Honnen,版本是1.0。@kjhughes,你是对的。这两个标签都存在,但我希望第一个sourceValue必须带有label=“nrA”。完整的xml示例看起来像“代码”3333333 2 33333333 111111111 03-04-2000 M Patient A'code@Stephan:当您无法使用xsd-1.1时,请使用xsd-1.0标记,并将XML添加到问题中,而不是添加到注释中。这一次我为你做了两件事。不能在XSD 1.0中指定此类约束;看到了吧。谢谢你,不幸的是,我现在只能使用这个xml。所以在我的情况下,这是不可能的。我确实尝试过使用不同的元素名称,但是在我们使用的应用程序中,xml文件没有正确处理。否则,使用不同的元素名称确实是最好的选择。
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
  xmlns:vc="http://www.w3.org/2007/XMLSchema-versioning" vc:minVersion="1.1">

  <xs:element name="Record">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="SSN" type="xs:string"/>
        <xs:element ref="sourceValue"/>
        <xs:element ref="sourceValue" minOccurs="0"/>
        <xs:element name="Data">
          <xs:complexType>
            <xs:sequence>
              <xs:element name="Patient">
                <xs:complexType>
                  <xs:sequence>
                    <xs:element name="DateOfBirth" type="xs:string"/>
                    <xs:element name="Sexe" type="xs:string"/>
                    <xs:element name="Name" type="xs:string"/>
                  </xs:sequence>
                </xs:complexType>
              </xs:element>
            </xs:sequence>
          </xs:complexType>
        </xs:element>
      </xs:sequence>
      <xs:assert test="sourceValue[1]/@label = 'nrA'"/>
      <xs:assert test="not(sourceValue[2]) or sourceValue[2]/@label = 'nrA'"/>
    </xs:complexType>
  </xs:element>

  <xs:element name="sourceValue">
    <xs:complexType>
      <xs:simpleContent>
        <xs:extension base="xs:string">
          <xs:attribute name="label" type="xs:string"/>
        </xs:extension>
      </xs:simpleContent>
    </xs:complexType>
  </xs:element>
</xs:schema>