Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/342.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/mercurial/2.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
Java 基于使用xsd:assert的非同级元素值的可选/必需元素?_Java_Xml_Xpath_Xsd_Xsd Validation - Fatal编程技术网

Java 基于使用xsd:assert的非同级元素值的可选/必需元素?

Java 基于使用xsd:assert的非同级元素值的可选/必需元素?,java,xml,xpath,xsd,xsd-validation,Java,Xml,Xpath,Xsd,Xsd Validation,我试图对XML的一个元素应用强制和可选的验证,这取决于另一个元素的值 以下是XSD: <?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:ele

我试图对XML的一个元素应用强制和可选的验证,这取决于另一个元素的值

以下是XSD:

<?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="root" type="root"></xs:element>

<xs:complexType name="root">
    <xs:sequence>
        <xs:element name="P1Message"
            type="p1Message">
        </xs:element>
    </xs:sequence>
</xs:complexType>

<xs:complexType name="p1Message">
    <xs:sequence>
        <xs:element name="Hdr" type="hdr"></xs:element>
        <xs:element name="Inf" type="inf"></xs:element>         
    </xs:sequence>
</xs:complexType>

<xs:complexType name="hdr">
    <xs:sequence>
        <xs:element name="I1Agt" type="i1Agt"></xs:element>
        <xs:element name="SequenceNum" type="xs:string"></xs:element>
        <xs:element name="SessionNum" type="xs:string"></xs:element>        
        <xs:element name="MsgTyp" type="xs:string"></xs:element>                
        <xs:element name="IntComment" type="xs:string"></xs:element>
    </xs:sequence>  
<xs:assert test="LclInstrm or not(MsgTyp = '103')"/>    
</xs:complexType>

<xs:complexType name="i1Agt">
    <xs:sequence>
        <xs:element name="FinInstnId" type="finInstnId"></xs:element>
    </xs:sequence>
</xs:complexType>

<xs:complexType name="finInstnId">
    <xs:sequence>
        <xs:element name="B1" type="xs:string"></xs:element>
    </xs:sequence>
</xs:complexType>

<xs:complexType name="inf">
    <xs:sequence>           
        <xs:element name="PmtTpInf" type="pmtTpInf"></xs:element>           
    </xs:sequence>
</xs:complexType>

<xs:complexType name="pmtTpInf">
    <xs:sequence>
        <xs:element name="LclInstrm" type="xs:string" minOccurs="0" maxOccurs="1"></xs:element>
        <xs:element name="Svc" type="svc"></xs:element>
    </xs:sequence>     
</xs:complexType>

<xs:complexType name="svc">
    <xs:sequence>
        <xs:element name="Cd" type="xs:string"></xs:element>
    </xs:sequence>
</xs:complexType>
</xs:schema>
有人能帮我吗


如果您需要更多详细信息,请告诉我。

断言中的XPath已关闭,因为它假定
LclInstrm
MsgTyp
是兄弟,而不是兄弟

您的断言必须基于一个共同的祖先(
P1Message
)并引用每个元素的正确相对路径

因此,您对
P1Message
的断言是:

<xs:assert test="Inf/PmtTpInf/LclInstrm or not(Hdr/MsgTyp = '103')"/>


关于如何使用断言根据另一个元素的值有条件地要求元素的另一个工作示例,请参见此处:

感谢kjhughes的快速回复。所以你的意思是说我需要使用它在以下条件下工作:当“MsgTyp”值不是'103'并且'LclInstrm'丢失时。但是,当“MsgTyp”为“103”且“LclInstrm”存在时,我在验证XML时会出现上述错误。我将在此处删除注释中过时的讨论(并在短时间内删除此注释)。您可能也想删除您的评论。
message.xml is not valid because cvc-assertion.3.13.4.1: Assertion evaluation ('LclInstrm or not(MsgTyp = '103')') for element `Hdr` with type `hdr` did not succeed.
<xs:assert test="Inf/PmtTpInf/LclInstrm or not(Hdr/MsgTyp = '103')"/>