0

I want to apply non-empty validation for String datatype element and this is depend on other element value. I know this can be done with assertion but I dont know how to write Xpath expression for same.

This is extension of Optional/mandatory element based on non-sibling element value using xsd:assert? question.

Here I want validation that If 'Msgtyp' value is '103' then 'LclTpInf' should be non-empty or null and for other 'Msgtype' it can be empty or null.

Updated:

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>

Community
  • 1
  • 1
Bhagwat Gurle
  • 333
  • 1
  • 8
  • 19
  • Can you share exact `xml` without references to other tickets? Also clarify whether you want to get element as output or just `true`/`false`? – Andersson Mar 30 '17 at 14:54
  • @Andersson: I have updated question and i didn't get about 'clarify whether you want to get element as output or just true/false'. could you please elaborate. – Bhagwat Gurle Mar 30 '17 at 15:13
  • @Andersson: I just want to validate given XML with XSD using some Java code or other. If validation fails it should display error on screen. – Bhagwat Gurle Mar 30 '17 at 15:19

1 Answers1

0

After googling I am able to resolve this issue. Below is solution for same, hope it would be helpful for someone:

<xs:assert test="not(Hdr/MsgTyp = '103') or not(Inf/PmtTpInf/LclInstrm eq '')"/>
Bhagwat Gurle
  • 333
  • 1
  • 8
  • 19