Home > Software engineering >  Using XmlProvider and updating xml gives wrong namespace format
Using XmlProvider and updating xml gives wrong namespace format

Time:09-21

I am using XmlProvider, and I am trying to make some changes to an xml file.

My problem is when writing to a file, the namespace format changes. I have tried two methods, where the first method gives me the correct format, but I am not able to use it for more complicated updates, like replacing all invoice lines with new invoicelines.

How can I update/save so that the format is like the xml given to the provider?

Common code:


open System
open System.Xml.Linq
open FSharp.Data
let cbc = XNamespace.Get("urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2")
let cac = XNamespace.Get("urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2")

type Invoice = XmlProvider<"""
<ehf:Invoice xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2" xmlns:cac="urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2" xmlns:ext="urn:oasis:names:specification:ubl:schema:xsd:CommonExtensionComponents-2" xmlns:ehf="urn:oasis:names:specification:ubl:schema:xsd:Invoice-2">
  <cbc:ID>5584-2021-2</cbc:ID>
  <cac:InvoiceLine>
    <cbc:ID>1a</cbc:ID>
    <cbc:Note>Note</cbc:Note>
    <cac:OrderLineReference>
      <cbc:LineID>1</cbc:LineID>
    </cac:OrderLineReference>
    <cac:Item>
      <cbc:Name>Item 1</cbc:Name>
    </cac:Item>
  </cac:InvoiceLine>
  <cac:InvoiceLine>
    <cbc:ID>1b</cbc:ID>
    <cbc:Note>Note</cbc:Note>
    <cac:OrderLineReference>
      <cbc:LineID>2</cbc:LineID>
    </cac:OrderLineReference>
    <cac:Item>
      <cbc:Name>Item 2</cbc:Name>
    </cac:Item>
  </cac:InvoiceLine>
</ehf:Invoice>
""">

The first method (result looks like what I want):

invoice.XElement.SetElementValue(cbc   "ID", "New id")
printfn $"{invoice |> string}"
//<?xml version="1.0" encoding="utf-8"?>
//<ehf:Invoice xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2" xmlns:cac="urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2" xmlns:ext="urn:oasis:names:specification:ubl:schema:xsd:CommonExtensionComponents-2" xmlns:ehf="urn:oasis:names:specification:ubl:schema:xsd:Invoice-2">
//  <cbc:ID>New id</cbc:ID>
//  <cac:InvoiceLine>
//    <cbc:ID>1a</cbc:ID>
//    <cbc:Note>Note</cbc:Note>
//    <cac:OrderLineReference>
//      <cbc:LineID>1</cbc:LineID>
//    </cac:OrderLineReference>
//    <cac:Item>
//      <cbc:Name>Item 1</cbc:Name>
//    </cac:Item>
//  </cac:InvoiceLine>
//  <cac:InvoiceLine>
//    <cbc:ID>1b</cbc:ID>
//    <cbc:Note>Note</cbc:Note>
//    <cac:OrderLineReference>
//      <cbc:LineID>2</cbc:LineID>
//    </cac:OrderLineReference>
//    <cac:Item>
//      <cbc:Name>Item 2</cbc:Name>
//    </cac:Item>
//  </cac:InvoiceLine>
//</ehf:Invoice>

Second medthod (gives me the wrong namespace format):

let line = invoice.InvoiceLines.[0]
let newLine = Invoice.InvoiceLine(
    "New id",
    line.Note,
    line.OrderLineReference,
    line.Item
    )
let changedInvoice = Invoice.Invoice(
      invoice.Id,
      [|newLine|]
      )
printfn $"{changedInvoice |> string}"

//<?xml version="1.0" encoding="utf-8"?>
//<Invoice xmlns="urn:oasis:names:specification:ubl:schema:xsd:Invoice-2">
//  <ID xmlns="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2">New id</ID>
//  <InvoiceLine xmlns="urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2">
//    <ID xmlns="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2">New id</ID>
//    <Note xmlns="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2">Note</Note>
//    <OrderLineReference>
//      <LineID xmlns="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2">1</LineID>
//    </OrderLineReference>
//    <Item>
//      <Name xmlns="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2">Item 1</Name>
//    </Item>
//  </InvoiceLine>
//</Invoice>

CodePudding user response:

The issue is that the newly created changed invoice does not have the top-level xmlns attributes that are in the original document and define the namespace prefixes. You can fix this by adding those back after creating the new changedInvoice:

let cbc = XNamespace.Get "urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2"
let cac = XNamespace.Get "urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2" 
let ext = XNamespace.Get "urn:oasis:names:specification:ubl:schema:xsd:CommonExtensionComponents-2" 
let ehf = XNamespace.Get "urn:oasis:names:specification:ubl:schema:xsd:Invoice-2"
changedInvoice.XElement.SetAttributeValue(XNamespace.Xmlns   "cbc", cbc)
changedInvoice.XElement.SetAttributeValue(XNamespace.Xmlns   "cac", cac)
changedInvoice.XElement.SetAttributeValue(XNamespace.Xmlns   "ext", ext)
changedInvoice.XElement.SetAttributeValue(XNamespace.Xmlns   "ehf", ehf)
changedInvoice.XElement.ToString()

Or perhaps an easier approach is to copy those from the original invoice:

for a in invoice.XElement.Attributes() do
  changedInvoice.XElement.SetAttributeValue(a.Name, a.Value)
changedInvoice.XElement.ToString()
  • Related