Views: 249
Open Peppol C4 で受信したインボイスの検証
Nobuyuki SAMBUICHI
ISO/TC295 Audit data services/SG1 Semantic model Convener
1. Open Peppol C4でのチェックプログラム
1.1. BIS Billing 3.0で提供されていたBasicルールを代替するテスト用XMLスキーマファイル作成
BIS Billing 3.0で提供されていたBasicルールは、Syntax bindingに従って定義されているか検証していましたが、JP PINTではこうした構文チェックルールが提供されていません。デジタルインボイスがSyntax bindingに違反していてもC2でのチェックにかかりませんので、C4でもSyntax binding定義に従っているかチェックしなくてはならなくなっています。
次の記事もお読みください。
デジタルインボイスはBIS Billing 3.0拡張で 2022-09-04
サンプルのデジタルインボイスには、schemaLocationで具体的にどのXMLスーキーマを使用するか指定する情報が定義されていませんので、Syntax bindingの定義通りのXMLスキーマを用意して、XMLスキーマ検証してみることにしました。
規定値としてNAを指定することなどの要請がある項目については、より詳細な検証が必要であり、個別にスキーマトロンを定義する必要があります。
UBL 2.1のスキーマファイルは、OASISから次のURLで公開されています。
http://docs.oasis-open.org/ubl/os-UBL-2.1/xsd/maindoc/UBL-Invoice-2.1
http://docs.oasis-open.org/ubl/os-UBL-2.1/xsd/common/UBL-CommonAggregateComponents-2.1.xsd
http://docs.oasis-open.org/ubl/os-UBL-2.1/xsd/common/UBL-CommonExtensionComponents-2.1.xsd
http://docs.oasis-open.org/ubl/os-UBL-2.1/xsd/common/UBL-QualifiedDataTypes-2.1.xsd
http://docs.oasis-open.org/ubl/os-UBL-2.1/xsd/common/UBL-UnqualifiedDataTypes-2.1.xsd
http://docs.oasis-open.org/ubl/os-UBL-2.1/xsd/common/CCTS_CCT_SchemaModule-2.1.xsd
1.2. スキーマファイル編集
次の2つのXMLスキーマファイルの定義をJP PINT V1.0のSyntax bindingに合わせた内容に変更する。
. Syntax bindingで定義されていない要素をコメントアウト
. 必須要素については、minOccurs=”0″をminOccurs=”1″に変更
. 最大でも1つと定義されている要素については、maxOccurs=”unbounded”をmaxOccurs=”1″に変更
Syntax bindingでは、Invoice要素の先頭には、必須要素である cbc:CustomizationID, cbc:ProfileID, cbc:ID が並んでいると定義されています。
この画面で表示されているSyntax cardinalityは、必ずしもUBLで定義しているものと同じではありません。
ここでは、Syntax bindingに合わせた検証用のXMLスキーマを定義して定義されたXML文書の構文チェックに使用します。
XMLスキーマの定義は、要素の型定義とそこで定義された型の内容定義に分かれています。
先ず、Invoiceは、InvoiceType型であることが定義されます。
<!-- ===== Element Declarations ===== -->
<xsd:element name="Invoice" type="InvoiceType">
<xsd:annotation>
<xsd:documentation>This element MUST be conveyed as the root element in any instance document based on this Schema expression</xsd:documentation>
</xsd:annotation>
</xsd:element>
次に、InvoiceType型がxsd:complexTypeというXML型であり、その内容にはXML要素が並んでいることがxsd:sequenceで示されています。
<!-- ===== Type Definitions ===== -->
<!-- ===== Aggregate Business Information Entity Type Definitions ===== -->
<xsd:complexType name="InvoiceType">
<xsd:sequence>
<xsd:element ref="ext:UBLExtensions" minOccurs="0" maxOccurs="1">
<xsd:annotation>
<xsd:documentation>A container for all extensions present in the document.</xsd:documentation>
</xsd:annotation>
</xsd:element>
<xsd:element ref="cbc:UBLVersionID" minOccurs="0" maxOccurs="1">
<xsd:annotation>
<xsd:documentation>
<ccts:Component>
<ccts:ComponentType>BBIE</ccts:ComponentType>
<ccts:DictionaryEntryName>Invoice. UBL Version Identifier. Identifier</ccts:DictionaryEntryName>
<ccts:Definition>Identifies the earliest version of the UBL 2 schema for this document type that defines all of the elements that might be encountered in the current instance.</ccts:Definition>
<ccts:Cardinality>0..1</ccts:Cardinality>
<ccts:ObjectClass>Invoice</ccts:ObjectClass>
<ccts:PropertyTerm>UBL Version Identifier</ccts:PropertyTerm>
<ccts:RepresentationTerm>Identifier</ccts:RepresentationTerm>
<ccts:DataType>Identifier. Type</ccts:DataType>
<ccts:Examples>2.0.5</ccts:Examples>
</ccts:Component>
</xsd:documentation>
</xsd:annotation>
</xsd:element>
<xsd:element ref="cbc:CustomizationID" minOccurs="0" maxOccurs="1">
<xsd:annotation>
<xsd:documentation>
<ccts:Component>
<ccts:ComponentType>BBIE</ccts:ComponentType>
<ccts:DictionaryEntryName>Invoice. Customization Identifier. Identifier</ccts:DictionaryEntryName>
<ccts:Definition>Identifies a user-defined customization of UBL for a specific use.</ccts:Definition>
<ccts:Cardinality>0..1</ccts:Cardinality>
<ccts:ObjectClass>Invoice</ccts:ObjectClass>
<ccts:PropertyTerm>Customization Identifier</ccts:PropertyTerm>
<ccts:RepresentationTerm>Identifier</ccts:RepresentationTerm>
<ccts:DataType>Identifier. Type</ccts:DataType>
<ccts:Examples>NES</ccts:Examples>
</ccts:Component>
</xsd:documentation>
</xsd:annotation>
</xsd:element>
<xsd:element ref="cbc:CustomizationID" minOccurs="0" maxOccurs="1">
<xsd:annotation>
<xsd:documentation>
<ccts:Component>
<ccts:ComponentType>BBIE</ccts:ComponentType>
<ccts:DictionaryEntryName>Invoice. Customization Identifier. Identifier</ccts:DictionaryEntryName>
<ccts:Definition>Identifies a user-defined customization of UBL for a specific use.</ccts:Definition>
<ccts:Cardinality>0..1</ccts:Cardinality>
<ccts:ObjectClass>Invoice</ccts:ObjectClass>
<ccts:PropertyTerm>Customization Identifier</ccts:PropertyTerm>
<ccts:RepresentationTerm>Identifier</ccts:RepresentationTerm>
<ccts:DataType>Identifier. Type</ccts:DataType>
<ccts:Examples>NES</ccts:Examples>
</ccts:Component>
</xsd:documentation>
</xsd:annotation>
</xsd:element>
... 途中省略 ...
</sequence>
</xsd:complexType>
公開されているUBL-Invoice-2.1.xsdでは、JP PINTの構文バインディング(Syntax binding)では、定義されていない
<xsd:element ref="ext:UBLExtensions" minOccurs="0" maxOccurs="1">
<xsd:element ref="cbc:UBLVersionID" minOccurs="0" maxOccurs="1">
などの定義が含まれているので、これらの定義をコメントアウトします。
それぞれ、
<!-- <xsd:element ref="ext:UBLExtensions" minOccurs="0" maxOccurs="1">
途中省略
</xsd:element> -->
および
<!-- xsd:element ref="cbc:UBLVersionID" minOccurs="0" maxOccurs="1">
途中省略
</xsd:element> -->
と変更します。
また、 cbc:CustomizationID は、必須項目なので、minOccurs=”0″をminOccurs=”1″に変更します。
<xsd:element ref="cbc:CustomizationID" minOccurs="1" maxOccurs="1"> <!--minOccurs="0"-->
修正した内容の先頭部分は、次のようになります。
<!--
Modified to check JP PINT V1.0 aligned e-Invoice by SAMBUICHI Professional Engineer's Office.
Library: OASIS Universal Business Language (UBL) 2.1 OS
http://docs.oasis-open.org/ubl/os-UBL-2.1/
Release Date: 04 November 2013
Module: xsd/maindoc/UBL-Invoice-2.1.xsd
Generated on: 2013-10-31 17:17z
Copyright (c) OASIS Open 2013. All Rights Reserved.
-->
<xsd:schema xmlns="urn:oasis:names:specification:ubl:schema:xsd:Invoice-2"
xmlns:cac="urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2"
xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2"
xmlns:ext="urn:oasis:names:specification:ubl:schema:xsd:CommonExtensionComponents-2"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:ccts="urn:un:unece:uncefact:documentation:2"
targetNamespace="urn:oasis:names:specification:ubl:schema:xsd:Invoice-2"
elementFormDefault="qualified" attributeFormDefault="unqualified" version="2.1">
<!-- ===== Imports ===== -->
<xsd:import namespace="urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2" schemaLocation="../common/UBL-CommonAggregateComponents-2.1.xsd"/>
<xsd:import namespace="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2" schemaLocation="../common/UBL-CommonBasicComponents-2.1.xsd"/>
<!--<xsd:import namespace="urn:oasis:names:specification:ubl:schema:xsd:CommonExtensionComponents-2" schemaLocation="../common/UBL-CommonExtensionComponents-2.1.xsd"/>-->
<!-- ===== Element Declarations ===== -->
<xsd:element name="Invoice" type="InvoiceType">
<xsd:annotation>
<xsd:documentation>This element MUST be conveyed as the root element in any instance document based on this Schema expression</xsd:documentation>
</xsd:annotation>
</xsd:element>
<!-- ===== Type Definitions ===== -->
<!-- ===== Aggregate Business Information Entity Type Definitions ===== -->
<xsd:complexType name="InvoiceType">
<xsd:annotation>
<xsd:documentation>
<ccts:Component>
<ccts:ComponentType>ABIE</ccts:ComponentType>
<ccts:DictionaryEntryName>Invoice. Details</ccts:DictionaryEntryName>
<ccts:Definition>A document used to request payment.</ccts:Definition>
<ccts:ObjectClass>Invoice</ccts:ObjectClass>
</ccts:Component>
</xsd:documentation>
</xsd:annotation>
<xsd:sequence>
<!-- <xsd:element ref="ext:UBLExtensions" minOccurs="0" maxOccurs="1">
<xsd:annotation>
<xsd:documentation>A container for all extensions present in the document.</xsd:documentation>
</xsd:annotation>
</xsd:element> -->
<!-- <xsd:element ref="cbc:UBLVersionID" minOccurs="0" maxOccurs="1">
<xsd:annotation>
<xsd:documentation>
<ccts:Component>
<ccts:ComponentType>BBIE</ccts:ComponentType>
<ccts:DictionaryEntryName>Invoice. UBL Version Identifier. Identifier</ccts:DictionaryEntryName>
<ccts:Definition>Identifies the earliest version of the UBL 2 schema for this document type that defines all of the elements that might be encountered in the current instance.</ccts:Definition>
<ccts:Cardinality>0..1</ccts:Cardinality>
<ccts:ObjectClass>Invoice</ccts:ObjectClass>
<ccts:PropertyTerm>UBL Version Identifier</ccts:PropertyTerm>
<ccts:RepresentationTerm>Identifier</ccts:RepresentationTerm>
<ccts:DataType>Identifier. Type</ccts:DataType>
<ccts:Examples>2.0.5</ccts:Examples>
</ccts:Component>
</xsd:documentation>
</xsd:annotation>
</xsd:element> -->
<xsd:element ref="cbc:CustomizationID" minOccurs="1" maxOccurs="1"> <!--minOccurs="0"-->
<xsd:annotation>
<xsd:documentation>
<ccts:Component>
<ccts:ComponentType>BBIE</ccts:ComponentType>
<ccts:DictionaryEntryName>Invoice. Customization Identifier. Identifier</ccts:DictionaryEntryName>
<ccts:Definition>Identifies a user-defined customization of UBL for a specific use.</ccts:Definition>
<ccts:Cardinality>0..1</ccts:Cardinality>
<ccts:ObjectClass>Invoice</ccts:ObjectClass>
<ccts:PropertyTerm>Customization Identifier</ccts:PropertyTerm>
<ccts:RepresentationTerm>Identifier</ccts:RepresentationTerm>
<ccts:DataType>Identifier. Type</ccts:DataType>
<ccts:Examples>NES</ccts:Examples>
</ccts:Component>
</xsd:documentation>
</xsd:annotation>
</xsd:element>
<xsd:element ref="cbc:ProfileID" minOccurs="1" maxOccurs="1"> <!--minOccurs="0"-->
<xsd:annotation>
<xsd:documentation>
<ccts:Component>
<ccts:ComponentType>BBIE</ccts:ComponentType>
<ccts:DictionaryEntryName>Invoice. Profile Identifier. Identifier</ccts:DictionaryEntryName>
<ccts:Definition>Identifies a user-defined profile of the customization of UBL being used.</ccts:Definition>
<ccts:Cardinality>0..1</ccts:Cardinality>
<ccts:ObjectClass>Invoice</ccts:ObjectClass>
<ccts:PropertyTerm>Profile Identifier</ccts:PropertyTerm>
<ccts:RepresentationTerm>Identifier</ccts:RepresentationTerm>
<ccts:DataType>Identifier. Type</ccts:DataType>
<ccts:Examples>BasicProcurementProcess</ccts:Examples>
</ccts:Component>
</xsd:documentation>
</xsd:annotation>
</xsd:element>
<!-- <xsd:element ref="cbc:ProfileExecutionID" minOccurs="0" maxOccurs="1">
<xsd:annotation>
<xsd:documentation>
<ccts:Component>
<ccts:ComponentType>BBIE</ccts:ComponentType>
<ccts:DictionaryEntryName>Invoice. Profile Execution Identifier. Identifier</ccts:DictionaryEntryName>
<ccts:Definition>Identifies an instance of executing a profile, to associate all transactions in a collaboration.</ccts:Definition>
<ccts:Cardinality>0..1</ccts:Cardinality>
<ccts:ObjectClass>Invoice</ccts:ObjectClass>
<ccts:PropertyTerm>Profile Execution Identifier</ccts:PropertyTerm>
<ccts:RepresentationTerm>Identifier</ccts:RepresentationTerm>
<ccts:DataType>Identifier. Type</ccts:DataType>
<ccts:Examples>BPP-1001</ccts:Examples>
</ccts:Component>
</xsd:documentation>
</xsd:annotation>
</xsd:element> -->
<xsd:element ref="cbc:ID" minOccurs="1" maxOccurs="1">
<xsd:annotation>
<xsd:documentation>
<ccts:Component>
<ccts:ComponentType>BBIE</ccts:ComponentType>
<ccts:DictionaryEntryName>Invoice. Identifier</ccts:DictionaryEntryName>
<ccts:Definition>An identifier for this document, assigned by the sender.</ccts:Definition>
<ccts:Cardinality>1</ccts:Cardinality>
<ccts:ObjectClass>Invoice</ccts:ObjectClass>
<ccts:PropertyTerm>Identifier</ccts:PropertyTerm>
<ccts:RepresentationTerm>Identifier</ccts:RepresentationTerm>
<ccts:DataType>Identifier. Type</ccts:DataType>
<ccts:AlternativeBusinessTerms>Invoice Number</ccts:AlternativeBusinessTerms>
以下省略
InvoiceTypeでは、事業者、住所、税情報など xsd:complexType として定義されていて参照使用されているものがあります。
これらの xsd:complexType は、UBL-CommonAggregateComponents-2.1.xsdに定義されていますので、この定義内容も変更します。
<!--
Modified to check JP PINT V1.0 aligned e-Invoice by SAMBUICHI Professional Engineer's Office.
Library: OASIS Universal Business Language (UBL) 2.1 OS
http://docs.oasis-open.org/ubl/os-UBL-2.1/
Release Date: 04 November 2013
Module: xsd/common/UBL-CommonAggregateComponents-2.1.xsd
Generated on: 2013-10-31 17:17z
Copyright (c) OASIS Open 2013. All Rights Reserved.
-->
<xsd:schema xmlns="urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2" xmlns:cac="urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2" xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:ccts="urn:un:unece:uncefact:documentation:2" targetNamespace="urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2" elementFormDefault="qualified" attributeFormDefault="unqualified" version="2.1">
<!-- ===== Imports ===== -->
<xsd:import namespace="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2" schemaLocation="UBL-CommonBasicComponents-2.1.xsd"/>
<!-- ===== Element Declarations ===== -->
<xsd:element name="InvoicePeriod" type="PeriodType"/>
<xsd:element name="OrderReference" type="OrderReferenceType"/>
<xsd:element name="BillingReference" type="BillingReferenceType"/>
<xsd:element name="InvoiceDocumentReference" type="DocumentReferenceType"/>
<xsd:element name="DespatchDocumentReference" type="DocumentReferenceType"/>
<xsd:element name="ReceiptDocumentReference" type="DocumentReferenceType"/>
<xsd:element name="OriginatorDocumentReference" type="DocumentReferenceType"/>
<xsd:element name="ContractDocumentReference" type="DocumentReferenceType"/>
<xsd:element name="AdditionalDocumentReference" type="DocumentReferenceType"/>
<xsd:element name="Attachment" type="AttachmentType"/>
<xsd:element name="ExternalReference" type="ExternalReferenceType"/>
<xsd:element name="ProjectReference" type="ProjectReferenceType"/>
<xsd:element name="AccountingSupplierParty" type="SupplierPartyType"/>
<xsd:element name="Party" type="PartyType"/>
<xsd:element name="PartyIdentification" type="PartyIdentificationType"/>
<xsd:element name="PartyName" type="PartyNameType"/>
<xsd:element name="Address" type="AddressType"/>
<xsd:element name="PostalAddress" type="AddressType"/>
<xsd:element name="AddressLine" type="AddressLineType"/>
<xsd:element name="Country" type="CountryType"/>
<xsd:element name="PartyTaxScheme" type="PartyTaxSchemeType"/>
<xsd:element name="TaxScheme" type="TaxSchemeType"/>
<xsd:element name="PartyLegalEntity" type="PartyLegalEntityType"/>
<xsd:element name="Contact" type="ContactType"/>
<xsd:element name="AccountingCustomerParty" type="CustomerPartyType"/>
<xsd:element name="PayeeParty" type="PartyType"/>
<xsd:element name="TaxRepresentativeParty" type="PartyType"/>
<xsd:element name="Delivery" type="DeliveryType"/>
<xsd:element name="DeliveryLocation" type="LocationType"/>
<xsd:element name="DeliveryParty" type="PartyType"/>
<xsd:element name="PaymentMeans" type="PaymentMeansType"/>
<xsd:element name="CardAccount" type="CardAccountType"/>
<xsd:element name="PayeeFinancialAccount" type="FinancialAccountType"/>
<xsd:element name="FinancialInstitutionBranch" type="BranchType"/>
<xsd:element name="PaymentMandate" type="PaymentMandateType"/>
<xsd:element name="PayerFinancialAccount" type="FinancialAccountType"/>
<xsd:element name="PaymentTerms" type="PaymentTermsType"/>
<xsd:element name="PrepaidPayment" type="PaymentType"/>
<xsd:element name="AllowanceCharge" type="AllowanceChargeType"/>
<xsd:element name="TaxCategory" type="TaxCategoryType"/>
<xsd:element name="TaxTotal" type="TaxTotalType"/>
<xsd:element name="TaxSubtotal" type="TaxSubtotalType"/>
<xsd:element name="LegalMonetaryTotal" type="MonetaryTotalType"/>
<xsd:element name="InvoiceLine" type="InvoiceLineType"/>
<xsd:element name="OrderLineReference" type="OrderLineReferenceType"/>
<xsd:element name="DocumentReference" type="DocumentReferenceType"/>
<xsd:element name="DespatchLineReference" type="LineReferenceType"/>
<xsd:element name="Item" type="ItemType"/>
<xsd:element name="SellersItemIdentification" type="ItemIdentificationType"/>
<xsd:element name="BuyersItemIdentification" type="ItemIdentificationType"/>
<xsd:element name="StandardItemIdentification" type="ItemIdentificationType"/>
<xsd:element name="OriginCountry" type="CountryType"/>
<xsd:element name="CommodityClassification" type="CommodityClassificationType"/>
<xsd:element name="ClassifiedTaxCategory" type="TaxCategoryType"/>
<xsd:element name="AdditionalItemProperty" type="ItemPropertyType"/>
<xsd:element name="Price" type="PriceType"/>
<!-- ===== Type Definitions ===== -->
<!-- ===== Aggregate Business Information Entity Type Definitions ===== -->
<xsd:complexType name="PeriodType">
<xsd:annotation>
<xsd:documentation>
<ccts:Component>
<ccts:ComponentType>ABIE</ccts:ComponentType>
<ccts:DictionaryEntryName>Period. Details</ccts:DictionaryEntryName>
<ccts:Definition>A class to describe a period of time.</ccts:Definition>
<ccts:ObjectClass>Period</ccts:ObjectClass>
</ccts:Component>
</xsd:documentation>
</xsd:annotation>
<xsd:sequence>
<xsd:element ref="cbc:StartDate" minOccurs="0" maxOccurs="1">
<xsd:annotation>
<xsd:documentation>
<ccts:Component>
<ccts:ComponentType>BBIE</ccts:ComponentType>
<ccts:DictionaryEntryName>Period. Start Date. Date</ccts:DictionaryEntryName>
<ccts:Definition>The date on which this period begins.</ccts:Definition>
<ccts:Cardinality>0..1</ccts:Cardinality>
<ccts:ObjectClass>Period</ccts:ObjectClass>
<ccts:PropertyTerm>Start Date</ccts:PropertyTerm>
<ccts:RepresentationTerm>Date</ccts:RepresentationTerm>
<ccts:DataType>Date. Type</ccts:DataType>
</ccts:Component>
</xsd:documentation>
</xsd:annotation>
</xsd:element>
<!-- <xsd:element ref="cbc:StartTime" minOccurs="0" maxOccurs="1">
<xsd:annotation>
<xsd:documentation>
<ccts:Component>
<ccts:ComponentType>BBIE</ccts:ComponentType>
<ccts:DictionaryEntryName>Period. Start Time. Time</ccts:DictionaryEntryName>
<ccts:Definition>The time at which this period begins.</ccts:Definition>
<ccts:Cardinality>0..1</ccts:Cardinality>
<ccts:ObjectClass>Period</ccts:ObjectClass>
<ccts:PropertyTerm>Start Time</ccts:PropertyTerm>
<ccts:RepresentationTerm>Time</ccts:RepresentationTerm>
<ccts:DataType>Time. Type</ccts:DataType>
</ccts:Component>
</xsd:documentation>
</xsd:annotation>
</xsd:element> -->
<xsd:element ref="cbc:EndDate" minOccurs="0" maxOccurs="1">
<xsd:annotation>
<xsd:documentation>
<ccts:Component>
<ccts:ComponentType>BBIE</ccts:ComponentType>
<ccts:DictionaryEntryName>Period. End Date. Date</ccts:DictionaryEntryName>
<ccts:Definition>The date on which this period ends.</ccts:Definition>
<ccts:Cardinality>0..1</ccts:Cardinality>
<ccts:ObjectClass>Period</ccts:ObjectClass>
<ccts:PropertyTerm>End Date</ccts:PropertyTerm>
<ccts:RepresentationTerm>Date</ccts:RepresentationTerm>
<ccts:DataType>Date. Type</ccts:DataType>
以下省略
1.3. スキーマファイルをコピー
これらの変更を行った結果、次のファイルが登録された。
aligned
├── common
│ ├── CCTS_CCT_SchemaModule-2.1.xsd
│ ├── UBL-CommonAggregateComponents-2.1.xsd
│ ├── UBL-CommonBasicComponents-2.1.xsd
│ ├── UBL-CommonExtensionComponents-2.1.xsd
│ ├── UBL-QualifiedDataTypes-2.1.xsd
│ └── UBL-UnqualifiedDataTypes-2.1.xsd
└── maindoc
└── UBL-Invoice-2.1.xsd
1.4. XMLスキーマ検証のJavaプログラム
JavaのXMLスキーマ検証では、schemaLocationが定義されていないXML文書に対して、XMLスキーマを指定して検証可能です。
import java.io.File;
import java.io.IOException;
import javax.xml.XMLConstants;
import javax.xml.transform.stream.StreamSource;
import javax.xml.validation.Schema;
import javax.xml.validation.SchemaFactory;
import javax.xml.validation.Validator;
import org.xml.sax.SAXException;
/**
* XMLスキーマ検証
*/
public class XmlSchemaValidation {
/**
* The application's entry point
* @param args an array of command-line arguments for the application
*/
public static void main(String[] args) {
String schema_file = args[0];
String xml_file = args[1];
boolean result = validateXMLSchema(schema_file, xml_file);
if (result)
System.out.println(xml_file + " は、 " + schema_file + " で定義されているXMLスキーマ定義に従った、妥当な定義内容です。");
else
System.out.println(xml_file + " は、 " + schema_file + " で定義されているXMLスキーマ定義に違反しています。");
}
/**
*
* Reprinted from the page below.<br>
* https://www.digitalocean.com/community/tutorials/how-to-validate-xml-against-xsd-in-java
*
* @param xsdPath XMLスキーマファイル
* @param xmlPath XMLインスタンス文書ファイル
* @return boolean XMLスキーマ検証結果
*/
public static boolean validateXMLSchema(String xsdPath, String xmlPath) {
try {
SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
Schema schema = factory.newSchema(new File(xsdPath));
Validator validator = schema.newValidator();
validator.validate(new StreamSource(new File(xmlPath)));
} catch (IOException | SAXException e) {
String message = e.getMessage();
message = message.replaceAll("\"urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2\"", "cbc");
message = message.replaceAll("\"urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2\"", "cac");
System.out.println("Exception: " + message);
return false;
}
return true;
}
}
2. 公式配布サンプルを検証
Download resources に含まれているサンプル文書をXMLスキーマ検証すると次の結果でした。
いファイルも、Syntax bindingで未定義の<cbc:UBLVersionID>が含まれていて定義に違反しています。というメッセージが返されました。
$ java -classpath core-japan-0.0.1.jar wuwei.japan_core.utils.XmlSchemaValidation data/aligned/maindoc/UBL-Invoice-2.1.xsd “data/pint-jp-resources-dev/trn-invoice/example/Japan PINT Invoice UBL Example.xml”
$ Exception: cvc-complex-type.2.4.a: Invalid content was found starting with element ‘{cbc:UBLVersionID}’. One of ‘{cbc:CustomizationID}’ is expected
$ data/pint-jp-resources-dev/trn-invoice/example/Japan PINT Invoice UBL Example.xml は、data/aligned/maindoc/UBL-Invoice-2.1.xsd で定義されているXMLスキーマ定義に違反しています。
$ java -classpath core-japan-0.0.1.jar wuwei.japan_core.utils.XmlSchemaValidation data/aligned/maindoc/UBL-Invoice-2.1.xsd “data/pint-jp-resources-dev/trn-invoice/example/Japan PINT Invoice UBL Example1-minimum.xml”
$ Exception: cvc-complex-type.2.4.a: Invalid content was found starting with element ‘{cbc:UBLVersionID}’. One of ‘{cbc:CustomizationID}’ is expected
$ data/pint-jp-resources-dev/trn-invoice/example/Japan PINT Invoice UBL Example1-minimum.xml は、data/aligned/maindoc/UBL-Invoice-2.1.xsd で定義されているXMLスキーマ定義に違反しています。
$ java -classpath core-japan-0.0.1.jar wuwei.japan_core.utils.XmlSchemaValidation data/aligned/maindoc/UBL-Invoice-2.1.xsd “data/pint-jp-resources-dev/trn-invoice/example/Japan PINT Invoice UBL Example2-TaxAcctCur.xml”
$ Exception: cvc-complex-type.2.4.a: Invalid content was found starting with element ‘{cbc:UBLVersionID}’. One of ‘{cbc:CustomizationID}’ is expected
$ data/pint-jp-resources-dev/trn-invoice/example/Japan PINT Invoice UBL Example2-TaxAcctCur.xml は、data/aligned/maindoc/UBL-Invoice-2.1.xsd で定義されているXMLスキーマ定義に違反しています。
$ java -classpath core-japan-0.0.1.jar wuwei.japan_core.utils.XmlSchemaValidation data/aligned/maindoc/UBL-Invoice-2.1.xsd “data/pint-jp-resources-dev/trn-invoice/example/Japan PINT Invoice UBL Example3-SumInv1.xml”
$ Exception: cvc-complex-type.2.4.a: Invalid content was found starting with element ‘{cbc:UBLVersionID}’. One of ‘{cbc:CustomizationID}’ is expected
$ data/pint-jp-resources-dev/trn-invoice/example/Japan PINT Invoice UBL Example3-SumInv1.xml は、data/aligned/maindoc/UBL-Invoice-2.1.xsd で定義されているXMLスキーマ定義に違反しています。
$ java -classpath core-japan-0.0.1.jar wuwei.japan_core.utils.XmlSchemaValidation data/aligned/maindoc/UBL-Invoice-2.1.xsd “data/pint-jp-resources-dev/trn-invoice/example/Japan PINT Invoice UBL Example4-SumInv2.xml”
$ Exception: cvc-complex-type.2.4.a: Invalid content was found starting with element ‘{cbc:UBLVersionID}’. One of ‘{cbc:CustomizationID}’ is expected
$ data/pint-jp-resources-dev/trn-invoice/example/Japan PINT Invoice UBL Example4-SumInv2.xml は、data/aligned/maindoc/UBL-Invoice-2.1.xsd で定義されているXMLスキーマ定義に違反しています。
$ java -classpath core-japan-0.0.1.jar wuwei.japan_core.utils.XmlSchemaValidation data/aligned/maindoc/UBL-Invoice-2.1.xsd “data/pint-jp-resources-dev/trn-invoice/example/Japan PINT Invoice UBL Example5-AllowanceCharge.xml”
$ Exception: cvc-complex-type.2.4.a: Invalid content was found starting with element ‘{cbc:UBLVersionID}’. One of ‘{cbc:CustomizationID}’ is expected
$ data/pint-jp-resources-dev/trn-invoice/example/Japan PINT Invoice UBL Example5-AllowanceCharge.xml は、data/aligned/maindoc/UBL-Invoice-2.1.xsd で定義されているXMLスキーマ定義に違反しています。
$ java -classpath core-japan-0.0.1.jar wuwei.japan_core.utils.XmlSchemaValidation data/aligned/maindoc/UBL-Invoice-2.1.xsd “data/pint-jp-resources-dev/trn-invoice/example/Japan PINT Invoice UBL Example6-CorrInv.xml”
$ Exception: cvc-complex-type.2.4.a: Invalid content was found starting with element ‘{cbc:UBLVersionID}’. One of ‘{cbc:CustomizationID}’ is expected
$ data/pint-jp-resources-dev/trn-invoice/example/Japan PINT Invoice UBL Example6-CorrInv.xml は、data/aligned/maindoc/UBL-Invoice-2.1.xsd で定義されているXMLスキーマ定義に違反しています。
$ java -classpath core-japan-0.0.1.jar wuwei.japan_core.utils.XmlSchemaValidation data/aligned/maindoc/UBL-Invoice-2.1.xsd “data/pint-jp-resources-dev/trn-invoice/example/Japan PINT Invoice UBL Example7-Return.Quan.ItPr.xml”
$ Exception: cvc-complex-type.2.4.a: Invalid content was found starting with element ‘{cbc:UBLVersionID}’. One of ‘{cbc:CustomizationID}’ is expected
$data/pint-jp-resources-dev/trn-invoice/example/Japan PINT Invoice UBL Example7-Return.Quan.ItPr.xml は、data/aligned/maindoc/UBL-Invoice-2.1.xsd で定義されているXMLスキーマ定義に違反しています。
$ java -classpath core-japan-0.0.1.jar wuwei.japan_core.utils.XmlSchemaValidation data/aligned/maindoc/UBL-Invoice-2.1.xsd “data/pint-jp-resources-dev/trn-invoice/example/Japan PINT Invoice UBL Example9-SumInv1 and O.xml”
$ Exception: cvc-complex-type.2.4.a: Invalid content was found starting with element ‘{cbc:UBLVersionID}’. One of ‘{cbc:CustomizationID}’ is expected
$ data/pint-jp-resources-dev/trn-invoice/example/Japan PINT Invoice UBL Example9-SumInv1 and O.xml は、data/aligned/maindoc/UBL-Invoice-2.1.xsd で定義されているXMLスキーマ定義に違反しています。
3. テスト用XMLスキーマファイル
内容を変更したもの
https://www.wuwei.space/core-japan/server/data/aligned/maindoc/UBL-Invoice-2.1.xsd
https://www.wuwei.space/core-japan/server/data/aligned/common/UBL-CommonAggregateComponents-2.1.xsd
ファイルをコピーしたもの。
https://www.wuwei.space/core-japan/server/data/aligned/common/UBL-CommonExtensionComponents-2.1
https://www.wuwei.space/core-japan/server/data/aligned/common/UBL-QualifiedDataTypes-2.1
https://www.wuwei.space/core-japan/server/data/aligned/common/UBL-UnqualifiedDataTypes-2.1
https://www.wuwei.space/core-japan/server/data/aligned/common/CCTS_CCT_SchemaModule-2.1.xsd