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>