xsdlibrary
Version:
Xml Schema Library
104 lines (100 loc) • 4.24 kB
JavaScript
const { xsd2jsonSchema, json2xsd, jsonSchema2xsd, xml2xsd, xml2json } = require('.')
const { json2xml } = require('./json2xml')
let beautify = require('json-beautify')
const test = `
<Invoice 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">
<cac:InvoiceLine>
<cbc:ID>2</cbc:ID>
<cbc:InvoicedQuantity unitCode="DAY">2</cbc:InvoicedQuantity>
<cbc:LineExtensionAmount currencyID="AUD">1000</cbc:LineExtensionAmount>
<cac:OrderLineReference>
<cbc:LineID>123</cbc:LineID>
</cac:OrderLineReference>
<cac:Item>
<cbc:Description>Description 2</cbc:Description>
<cbc:Name>item name 2</cbc:Name>
<cac:StandardItemIdentification>
<cbc:ID schemeID="0151">21382183120983</cbc:ID>
</cac:StandardItemIdentification>
<cac:OriginCountry>
<cbc:IdentificationCode>NO</cbc:IdentificationCode>
</cac:OriginCountry>
<cac:CommodityClassification>
<cbc:ItemClassificationCode listID="SRV">09348023</cbc:ItemClassificationCode>
</cac:CommodityClassification>
<cac:ClassifiedTaxCategory>
<cbc:ID>S</cbc:ID>
<cbc:Percent>10</cbc:Percent>
<cac:TaxScheme>
<cbc:ID>GST</cbc:ID>
</cac:TaxScheme>
</cac:ClassifiedTaxCategory>
</cac:Item>
<cac:Price>
<cbc:PriceAmount currencyID="AUD">500</cbc:PriceAmount>
</cac:Price>
</cac:InvoiceLine>
<cac:InvoiceLine>
<cbc:ID>3</cbc:ID>
<cbc:Note>Invoice Line Description</cbc:Note>
<cbc:InvoicedQuantity unitCode="M66">25</cbc:InvoicedQuantity>
<cbc:LineExtensionAmount currencyID="AUD">187.50</cbc:LineExtensionAmount>
<cbc:AccountingCost>Consulting Fees</cbc:AccountingCost>
<cac:InvoicePeriod>
<cbc:StartDate>2019-06-01</cbc:StartDate>
<cbc:EndDate>2019-07-30</cbc:EndDate>
</cac:InvoicePeriod>
<cac:OrderLineReference>
<cbc:LineID>123</cbc:LineID>
</cac:OrderLineReference>
<cac:DocumentReference>
<cbc:ID schemeID="HWB">9000074677</cbc:ID>
<cbc:DocumentTypeCode>130</cbc:DocumentTypeCode>
</cac:DocumentReference>
<cac:Item>
<cbc:Description>Widgets True and Fair</cbc:Description>
<cbc:Name>True-Widgets</cbc:Name>
<cac:BuyersItemIdentification>
<cbc:ID>W659590</cbc:ID>
</cac:BuyersItemIdentification>
<cac:SellersItemIdentification>
<cbc:ID>WG546767</cbc:ID>
</cac:SellersItemIdentification>
<cac:StandardItemIdentification>
<cbc:ID schemeID="0151">WG546767</cbc:ID>
</cac:StandardItemIdentification>
<cac:OriginCountry>
<cbc:IdentificationCode>AU</cbc:IdentificationCode>
</cac:OriginCountry>
<cac:CommodityClassification>
<cbc:ItemClassificationCode listID="SRV">09348023</cbc:ItemClassificationCode>
</cac:CommodityClassification>
<cac:ClassifiedTaxCategory>
<cbc:ID>S</cbc:ID>
<cbc:Percent>10</cbc:Percent>
<cac:TaxScheme>
<cbc:ID>GST</cbc:ID>
</cac:TaxScheme>
</cac:ClassifiedTaxCategory>
</cac:Item>
<cac:Price>
<cbc:PriceAmount currencyID="AUD">7.50</cbc:PriceAmount>
<cac:AllowanceCharge>
<cbc:ChargeIndicator>false</cbc:ChargeIndicator>
<cbc:Amount currencyID="AUD">0.00</cbc:Amount>
<cbc:BaseAmount currencyID="AUD">7.50</cbc:BaseAmount>
</cac:AllowanceCharge>
</cac:Price>
</cac:InvoiceLine>
</Invoice>`
// console.log(json2xml(json))
const jsonO = xml2json(test)
// console.log(beautify(JSON.parse(jsonO), null, 2, 100))
const xmlO = json2xml(jsonO)
// console.log(xmlO)
const json2 = xml2json(xmlO)
// console.log(beautify(JSON.parse(json2), null, 2, 100))
// const xml2 = json2xml(jsonO)
// console.log(xml2)
// const json3 = xml2json(xml2)
// const xml3 = json2xml(json3)