问题描述
例如,我可以使用xs:assert比较来自不同父项的两个xml元素吗?我想比较
此刻我出现错误,即 模式类型为'GroupHeader32'的元素'GrpHdr'的断言评估('pa:NbOfTxs = pa:EndToEndId')不成功。
如有需要,乐意提供更多的商品信息
<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="pa:Msgid eq pa:EndToEndId"/>
</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="Document">
<xs:sequence>
<xs:element name="CstmrCdtTrfInitn" type="CustomerCreditTransferInitiationV03"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="PaymentIdentification1">
<xs:sequence>
<xs:element maxOccurs="1" minOccurs="0" name="InstrId" type="Max35Text"/>
<xs:element name="EndToEndId" type="Max35Text"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="PaymentInstructioninformation3">
<xs:sequence>
<xs:element name="PmtInfId" type="Max35Text"/>
<xs:element name="PmtMtd" type="PaymentMethod3Code"/>
<xs:element maxOccurs="1" minOccurs="0" name="BtchBookg" type="BatchBookingIndicator"/>
<xs:element maxOccurs="1" minOccurs="0" name="NbOfTxs" type="Max15NumericText"/>
<xs:element maxOccurs="1" minOccurs="0" name="CtrlSum" type="DecimalNumber"/>
<xs:element maxOccurs="1" minOccurs="0" name="PmtTpInf" type="PaymentTypeinformation19"/>
<xs:element name="ReqdExctnDt" type="ISODate"/>
<xs:element maxOccurs="1" minOccurs="0" name="PoolgAdjstmntDt" type="ISODate"/>
<xs:element name="Dbtr" type="PartyIdentification32"/>
<xs:element name="DbtrAcct" type="CashAccount16"/>
<xs:element name="DbtrAgt" type="BranchAndFinancialInstitutionIdentification4"/>
<xs:element maxOccurs="1" minOccurs="0" name="DbtrAgtAcct" type="CashAccount16"/>
<xs:element maxOccurs="1" minOccurs="0" name="UltmtDbtr" type="PartyIdentification32"/>
<xs:element maxOccurs="1" minOccurs="0" name="ChrgBr" type="ChargeBearerType1Code"/>
<xs:element maxOccurs="1" minOccurs="0" name="ChrgsAcct" type="CashAccount16"/>
<xs:element maxOccurs="1" minOccurs="0" name="ChrgsAcctAgt" type="BranchAndFinancialInstitutionIdentification4"/>
<xs:element maxOccurs="unbounded" minOccurs="1" name="CdtTrftxInf" type="CreditTransferTransactioninformation10"/>
</xs:sequence>
</xs:complexType>
</xs:schema>
<xs:complexType name="CreditTransferTransactioninformation10">
<xs:sequence>
<xs:element name="PmtId" type="PaymentIdentification1"/>
<xs:element maxOccurs="1" minOccurs="0" name="PmtTpInf" type="PaymentTypeinformation19"/>
<xs:element name="Amt" type="AmountType3Choice"/>
<xs:element maxOccurs="1" minOccurs="0" name="XchgRateInf" type="ExchangeRateinformation1"/>
<xs:element maxOccurs="1" minOccurs="0" name="ChrgBr" type="ChargeBearerType1Code"/>
<xs:element maxOccurs="1" minOccurs="0" name="ChqInstr" type="Cheque6"/>
<xs:element maxOccurs="1" minOccurs="0" name="UltmtDbtr" type="PartyIdentification32"/>
<xs:element maxOccurs="1" minOccurs="0" name="IntrmyAgt1" type="BranchAndFinancialInstitutionIdentification4"/>
<xs:element maxOccurs="1" minOccurs="0" name="IntrmyAgt1Acct" type="CashAccount16"/>
<xs:element maxOccurs="1" minOccurs="0" name="IntrmyAgt2" type="BranchAndFinancialInstitutionIdentification4"/>
<xs:element maxOccurs="1" minOccurs="0" name="IntrmyAgt2Acct" type="CashAccount16"/>
<xs:element maxOccurs="1" minOccurs="0" name="IntrmyAgt3" type="BranchAndFinancialInstitutionIdentification4"/>
<xs:element maxOccurs="1" minOccurs="0" name="IntrmyAgt3Acct" type="CashAccount16"/>
<xs:element maxOccurs="1" minOccurs="0" name="CdtrAgt" type="BranchAndFinancialInstitutionIdentification4"/>
<xs:element maxOccurs="1" minOccurs="0" name="CdtrAgtAcct" type="CashAccount16"/>
<xs:element maxOccurs="1" minOccurs="0" name="Cdtr" type="PartyIdentification32"/>
<xs:element maxOccurs="1" minOccurs="0" name="CdtrAcct" type="CashAccount16"/>
<xs:element maxOccurs="1" minOccurs="0" name="UltmtCdtr" type="PartyIdentification32"/>
<xs:element maxOccurs="unbounded" minOccurs="0" name="InstrForCdtrAgt" type="InstructionForCreditorAgent1"/>
<xs:element maxOccurs="1" minOccurs="0" name="InstrForDbtrAgt" type="Max140Text"/>
<xs:element maxOccurs="1" minOccurs="0" name="Purp" type="Purpose2Choice"/>
<xs:element maxOccurs="10" minOccurs="0" name="RgltryRptg" type="RegulatoryReporting3"/>
<xs:element maxOccurs="1" minOccurs="0" name="Tax" type="Taxinformation3"/>
<xs:element maxOccurs="10" minOccurs="0" name="RltdRmtInf" type="RemittanceLocation2"/>
<xs:element maxOccurs="1" minOccurs="0" name="RmtInf" type="Remittanceinformation5"/>
</xs:sequence>
</xs:complexType>
我的示例XML看起来像这样:我正在尝试将Msgid与endtoendid进行比较,并且还想检查NbOfTxs字段值是否应等于出现的次数
<Document xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="urn:iso:std:iso:20022:tech:xsd:pain.001.001.03">
<CstmrCdtTrfInitn>
<GrpHdr>
<Msgid>1</Msgid>
<NbOfTxs>2</NbOfTxs>
</GrpHdr>
<PmtInf>
<PmtInfId>ABC</PmtInfId>
<CdtTrftxInf>
<PmtId>
<EndToEndId>1</EndToEndId>
</PmtId>
</CdtTrftxInf>
<CdtTrftxInf>
<PmtId>
<EndToEndId>2</EndToEndId>
</PmtId>
</CdtTrftxInf>
</PmtInf>
</CstmrCdtTrfInitn>
</Document>
解决方法
断言必须放在所有测试的XML组件共有的祖先中;断言不能引用不是出现断言的元素的后代的组件。
在您的情况下,在GrpHdr
上的断言不能引用EndToEndId
,因为EndToEndId
不是GrpHldr
的后代。相反,该断言必须出现在CstmrCdtTrfInitn
和GrpHdr
的共同祖先EndToEndId
(或更高版本)上。
还要注意确定您的断言是否打算适用于一个或所有组件(如果可以存在多个组件)。