Search code examples
xmlxsdxsd-validationxml-validationxsd-1.1

xsd:assert not working : Assertion evaluation did not succeed


I am new to XSD 1.1 xs:assert and am getting an error when I try validate the XML with the XSD:

Assertion evaluation (MsgId eq 'ABC') for element GrpHdr on schema type GroupHeader32 did not succeed.

XSD

Sorry did not put the complete XSD, just a part of it:

<xs:schema attributeFormDefault="unqualified" xmlns="urn:iso:std:iso:20022:tech:xsd:pain.001.001.03"
    xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified"
    targetNamespace="urn:iso:std:iso:20022:tech:xsd:pain.001.001.03"
    xmlns:vc="http://www.w3.org/2007/XMLSchema-versioning" vc:minVersion="1.1"
    xmlns:xerces="http://xerces.apache.org">    <xs:element name="Document" type="Document"/>

    <xs:complexType name="Document">
        <xs:sequence>
            <xs:element name="CstmrCdtTrfInitn" type="CustomerCreditTransferInitiationV03"/>
        </xs:sequence>
    </xs:complexType>

    <xs:complexType name="CustomerCreditTransferInitiationV03">
        <xs:sequence>
            <xs:element name="GrpHdr" type="GroupHeader32"/>
            <xs:element maxOccurs="unbounded" minOccurs="1" name="PmtInf" type="PaymentInstructionInformation3"/>
        </xs:sequence>
    </xs:complexType>

    <xs:complexType name="GroupHeader32">       
        <xs:sequence>
            <xs:element name="MsgId" type="Max35Text"/>
            <xs:element name="CreDtTm" type="ISODateTime"/>
            <xs:element maxOccurs="2" minOccurs="0" name="Authstn" type="Authorisation1Choice"/>
            <xs:element name="NbOfTxs" type="Max15NumericText"/>
            <xs:element maxOccurs="1" minOccurs="0" name="CtrlSum" type="DecimalNumber"/>
            <xs:element name="InitgPty" type="PartyIdentification32"/>
            <xs:element maxOccurs="1" minOccurs="0" name="FwdgAgt" type="BranchAndFinancialInstitutionIdentification4"/>
        </xs:sequence>
        <xs:assert test="MsgId eq 'ABC'"/> 
    </xs:complexType>
</xs:schema>

Is this a correct place to put xs:assert condition to check the value of element MsgID equal to ABC ?

I am using oXygen XML software for validations.

Sample XML

 <CstmrCdtTrfInitn>
     <GrpHdr>
         <MsgId>ABC</MsgId>
         <CreDtTm>2009-02-17T12:49:35</CreDtTm>
         <NbOfTxs>1</NbOfTxs>
     </GrpHdr>
 </CstmrCdtTrfInitn>

Solution

  • You have a couple of namespace issues:

    1. Your XML is not in the target namespace of your XSD.
    2. Your assertion test references MsgId in no namespace, despite it being in the target namespace.

    Here's a complete, working example of your XML and an XSD that validates it successfully using the (repaired) sample assertion:

    XML

    <Document xmlns="urn:iso:std:iso:20022:tech:xsd:pain.001.001.03">
      <CstmrCdtTrfInitn>
        <GrpHdr>
          <MsgId>ABC</MsgId>
          <CreDtTm>2009-02-17T12:49:35</CreDtTm>
          <NbOfTxs>1</NbOfTxs>
        </GrpHdr>
      </CstmrCdtTrfInitn>
    </Document>
    

    XSD

    <xs:schema  xmlns:xs="http://www.w3.org/2001/XMLSchema" 
      targetNamespace="urn:iso:std:iso:20022:tech:xsd:pain.001.001.03"
      xmlns="urn:iso:std:iso:20022:tech:xsd:pain.001.001.03"
      xmlns:pa="urn:iso:std:iso:20022:tech:xsd:pain.001.001.03"
      attributeFormDefault="unqualified"
      elementFormDefault="qualified"
      xmlns:vc="http://www.w3.org/2007/XMLSchema-versioning" 
      vc:minVersion="1.1">
      
      <xs:element name="Document" type="Document"/>
     
      <xs:complexType name="Document">
        <xs:sequence>
          <xs:element name="CstmrCdtTrfInitn" type="CustomerCreditTransferInitiationV03"/>
        </xs:sequence>
      </xs:complexType>
      
      <xs:complexType name="CustomerCreditTransferInitiationV03">
        <xs:sequence>
          <xs:element name="GrpHdr" type="GroupHeader32"/>
        </xs:sequence>
      </xs:complexType>
      
      <xs:complexType name="GroupHeader32">       
        <xs:sequence>
          <xs:element name="MsgId" type="xs:string"/>
          <xs:element name="CreDtTm" type="xs:string"/>
          <xs:element maxOccurs="2" minOccurs="0" name="Authstn" type="xs:string"/>
          <xs:element name="NbOfTxs" type="xs:string"/>
        </xs:sequence>
        <xs:assert test="pa:MsgId eq 'ABC'"/> 
      </xs:complexType>
    </xs:schema>