使用 xsd:assert 基于非兄弟元素值的可选/强制元素? [英] Optional/mandatory element based on non-sibling element value using xsd:assert?

查看:23
本文介绍了使用 xsd:assert 基于非兄弟元素值的可选/强制元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试对 XML 的一个元素应用强制和可选的验证,该元素取决于其他元素的值.

I am trying to apply validation of mandatory and optional on one element of XML which is depending upon value of other element.

以下是 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>

下面是 XML:

<?xml version="1.0" encoding="utf-8"?>
<root>
<P1Message>
<Hdr>
  <I1Agt>
    <FinInstnId>
      <B1>str1234</B1>
    </FinInstnId>
  </I1Agt>
  <SequenceNum>str1234</SequenceNum>
  <SessionNum>str1234</SessionNum>
  <MsgTyp>123</MsgTyp>
  <IntComment>str1234</IntComment>
</Hdr>
<Inf>
   <PmtTpInf>
    <LclInstrm>str1234</LclInstrm>
    <Svc>
      <Cd>str1234</Cd>
    </Svc>
  </PmtTpInf>
</Inf>
</P1Message>
</root>

MsgTyp 的值为 103 时,我想让 LclInstrm 元素成为必需元素.

I want to make LclInstrm element mandatory when value of MsgTyp is 103.

当我尝试使用 Java 中的 XSD 验证以上 XML 时,出现以下错误:

When I am trying to validate above XML with XSD in Java I am getting below error:

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.

有人可以帮我吗?

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

Please let me know if you require more details.

推荐答案

你在断言中的 XPath 是关闭的,因为它假设 LclInstrmMsgTyp 在它们是兄弟时不是.

Your XPath in the assertion is off because it's assuming that LclInstrm and MsgTyp are siblings when they're not.

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

Your assertion will have to go on a common ancestor (P1Message) and reference the correct relative paths to each element.

因此,您对 P1Message 的断言将是:

So, your assertion on P1Message would be:

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

如何使用断言根据另一个元素的值有条件地要求一个元素的另一个工作示例可以在这里找到:当另一个元素具有特定值时需要 XSD 中的 XML 元素吗?

Another working example of how to use an assertion to conditionally require an element based upon the value of another element can be found here: Require XML element in XSD when another element has certain value?

这篇关于使用 xsd:assert 基于非兄弟元素值的可选/强制元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆