My F# code cannot read XML using XSD template - f#

I want to parse data from my bank account in F# that are provided in XML. I have an XSD schema of a statement.
My code reads the schema but it is not able to read the structure. I see the content in XML in data.XElement, however, data.AccountStatement property does not exist. The code goes to None option and then the application crashes. Do you have any idea why the code loads the schema but cannot work with data in XML?
open FSharp.Data
let TestAddress = "TESTADDRESS"
let dateRegex = #"\d{1,2}[.]\d{1,2}[.]\d{4}";
type BankTransactionList = XmlProvider<Schema="Schemas/IBSchema.xsd">
let data = BankTransactionList.Parse(Http.RequestString(TestAddress))
[<EntryPoint>]
let main argv =
let AccountStatement =
match data.AccountStatement with
| Some v -> Some v
| None -> None //code goes here
let TransactionList =
match AccountStatement.Value.TransactionList with //and crashes here
| Some v -> Some v
| None -> None
for transaction in TransactionList.Value.Transactions do
printf "%s" transaction.Column22.Value
0 // return an integer exit code
I am developing in .Net Core 2.1. You can see the anonymized example of XML data below.
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<AccountStatement>
<Info>
<accountId>X</accountId>
<bankId>X</bankId>
<currency>CZK</currency>
<iban>X</iban>
<bic>X</bic>
<openingBalance>X</openingBalance>
<closingBalance>X</closingBalance>
<dateStart>X</dateStart>
<dateEnd>X</dateEnd>
<idFrom>X</idFrom>
<idTo>16663326563</idTo>
</Info>
<TransactionList>
<Transaction>
<column_22 name="ID pohybu" id="22">0</column_22>
<column_0 name="Datum" id="0">2018-08-25+02:00</column_0>
<column_1 name="Objem" id="1">0</column_1>
<column_14 name="Měna" id="14">CZK</column_14>
<column_5 name="VS" id="5">1023</column_5>
<column_16 name="Zpráva pro příjemce" id="16">Message</column_16>
<column_8 name="Typ" id="8">Platba kartou</column_8>
<column_9 name="Provedl" id="9">Pešík, Jiří</column_9>
<column_17 name="ID pokynu" id="17">0</column_17>
</Transaction>
</TransactionList>
</AccountStatement>

I think the issue is that the schema requires the namespace: "http://www.fio.cz/IBSchema".
Adding the namespace in the document should solve the issue:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<AccountStatement xmlns="http://www.fio.cz/IBSchema">
<Info>
<accountId>X</accountId>
<bankId>X</bankId>
<currency>CZK</currency>
<iban>X</iban>
<bic>X</bic>
<openingBalance>0</openingBalance>
<closingBalance>0</closingBalance>
<dateStart>2000-01-01</dateStart>
<dateEnd>2001-01-01</dateEnd>
<idFrom>0</idFrom>
<idTo>16663326563</idTo>
</Info>
<TransactionList>
<Transaction>
<column_22 name="ID pohybu" id="22">0</column_22>
<column_0 name="Datum" id="0">2018-08-25+02:00</column_0>
<column_1 name="Objem" id="1">0</column_1>
<column_14 name="Měna" id="14">CZK</column_14>
<column_5 name="VS" id="5">1023</column_5>
<column_16 name="Zpráva pro příjemce" id="16">Message</column_16>
<column_8 name="Typ" id="8">Platba kartou</column_8>
<column_9 name="Provedl" id="9">Pešík, Jiří</column_9>
<column_17 name="ID pokynu" id="17">0</column_17>
</Transaction>
</TransactionList>
</AccountStatement>
I hope your bank is not producing documents lacking the namespace and a schema requiring it.

Related

XSLT: how to pass different prefix values for Node and its feilds

I have a requirement where Nodes have a different prefix value where as fields under them has a different prefix, how to achieve this using XSLT. I have attached sample input and expected its output. Can you please advise.
I am expecting nodes have prebix "cac" and its fields as "cbc" and also replace namespace ns2 with r1 prefix.
Input:
<?xml version="1.0" encoding="UTF-8"?>
<ns0:StandardBusinessDoc xmlns:ns0="http://www.unece.org/cefact/namespaces/StandardBusinessDocumentHeader">
<ns1:Invoice xmlns:ns1="urn:oasis:names:specification:ubl:schema:xsd:Invoice-2">
<ns1:CustomizationID>urn:cen.eu:en131:2017#compliant#urn:fdc:peppol.eu:2017:pocc:billing:3.0</ns1:CustomizationID>
<ns1:ProfileID>urn:fdc:peppol.eu:2017:poacc:billing:01:1.0</ns1:ProfileID>
<ns1:ID>80160238</ns1:ID>
<ns1:BuyerReference>202208_604</ns1:BuyerReference>
<ns1:BillingReference>
<ns1:InvoiceDocumentReference>
<ns1:ID>test</ns1:ID>
<ns1:IssueDate>2022-09-28</ns1:IssueDate>
</ns1:InvoiceDocumentReference>
</ns1:BillingReference>
<ns1:AdditionalDocumentReference>
<ns1:ID>06AB87FD6E1E1EED96F1653A13ADC23</ns1:ID>
<ns1:DocumentDescription>SupplierUID</ns1:DocumentDescription>
</ns1:AdditionalDocumentReference>
<ns1:AdditionalDocumentReference>
<ns1:ID>2M</ns1:ID>
<ns1:DocumentDescription>Series</ns1:DocumentDescription>
</ns1:AdditionalDocumentReference>
<ns2:Classification xmlns:ns2="rl:rl-einvoicing">
<ns2:Line>
<ns2:ID>000010</ns2:ID>
<ns2:VatCategory>
<ns2:VatRate>24</ns2:VatRate>
<ns2:IncomeClassification>
<ns2:Category>category1_2</ns2:Category>
<ns2:Type>E3_561_005</ns2:Type>
<ns2:Amount>112.33</ns2:Amount>
</ns2:IncomeClassification>
</ns2:VatCategory>
</ns2:Line>
</ns2:Classification>
</ns0:StandardBusinessDoc>
Expected Output:
<?xml-model href="http://www.unece.org/fileadmin/DAM/cefact/namespaces/StandardBusinessDocumentHeader/StandardBusinessDocumentHeader.xsd" type="application/xml" schematypens="http://purl.oclc.org/dsdl/schematron"?>
<StandardBusinessDoc xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xs="http://www.w3.org/2001/XMLSchema" xsi:schemaLocation="http://www.unece.org/cefact/namespaces/StandardBusinessDocumentHeader http://www.unece.org/fileadmin/DAM/cefact/namespaces/StandardBusinessDocumentHeader/StandardBusinessDocumentHeader.xsd" xmlns:rl="rl:rl-einvoicing" xmlns="http://www.unece.org/cefact/namespaces/StandardBusinessDocumentHeader">
<Invoice xmlns:cac="urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2" xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2" xmlns="urn:oasis:names:specification:ubl:schema:xsd:Invoice-2">
<cbc:CustomizationID>urn:cen.eu:en131:2017#compliant#urn:fdc:peppol.eu:2017:pocc:billing:3.0</cbc:CustomizationID>
<cbc:ProfileID>urn:fdc:peppol.eu:2017:poacc:billing:01:1.0</cbc:ProfileID>
<cbc:ID>80160238</cbc:ID>
<cbc:BuyerReference>202208_604</cbc:BuyerReference>
<cac:BillingReference>
<cac:InvoiceDocumentReference>
<cbc:ID>test</cbc:ID>
<cbc:IssueDate>2022-09-28</cbc:IssueDate>
</cac:InvoiceDocumentReference>
</cac:BillingReference>
<cac:AdditionalDocumentReference>
<cbc:ID>06AB87FD6E1E1EED96F1653A13ADC23</cbc:ID>
<cbc:DocumentDescription>SupplierUID</cbc:DocumentDescription>
</cac:AdditionalDocumentReference>
<cac:AdditionalDocumentReference>
<cbc:ID>2Μ</cbc:ID>
<cbc:DocumentDescription>Series</cbc:DocumentDescription>
</cac:AdditionalDocumentReference>
<rl:Classification>
<rl:Line>
<rl:ID>000010</rl:ID>
<rl:VatCategory>
<rl:VatRate>24</rl:VatRate>
<rl:IncomeClassification>
<rl:Category>category1_2</rl:Category>
<rl:Type>E3_561_005</rl:Type>
<rl:Amount>112.33</rl:Amount>
</rl:IncomeClassification>
</rl:VatCategory>
</rl:Line>
</rl:Classification>
</StandardBusinessDoc>

Loop in XML with Nokogiri and get a child element

I'm struggling with a XML / GPX files where I need to grab some specific datas:
<?xml version="1.0" encoding="UTF-8"?>
<trk>
<trkseg>
<trkde>
<ele>78</ele>
<time>2021-01-31T08:30:38.000Z</time>
<extensions>
<ns4:Extension>
<ns4:hr>115</ns4:hr>
</ns4:Extension>
</extensions>
</trkde>
<trkde>
<ele>77.8000030517578125</ele>
<time>2021-01-31T08:30:40.000Z</time>
<extensions>
<ns4:Extension>
<ns4:hr>115</ns4:hr>
</ns4:Extension>
</extensions>
</trkde>
</trkseg>
</trk>
</gpx>
So far I'm trying to do this
doc.css("trkde").each do |trkde|
unless trkde.css("time").blank?
time = Time.zone.parse(trkde.css("time").text)
hr = 0
unless trkde.css("extensions").blank?
puts trkde.css("ns4:Extension")
hr = trkde.css("ns4:hr")
end
puts time.to_s + ":" + hr.to_s
end
end
But I do not succeed in getting the value from ns4:hr
I read carefully https://nokogiri.org/tutorials/searching_a_xml_html_document.html but with my current understanding, I didn't succeed.
I did try to do hr = track_point.xpath("ns4:hr") but it doesn't work neither
So far I found this way
hr = at_xpath('.//*[name()="ns4:hr"]').text.to_i
But I feel it's not really elegant

GtkBuilder F# Object reference not set to an instance of an object

I have a problem with making the mainwindow on my simple app run, The error given is that - Object reference not set to an instance of an object.
this happens when the app is getting debugged and the error occurs at handler.window1.ShowAll()
I did find some code online which hints at adding some member code as in member this.Whatever() = window1 however i have no idea if this is relevent to my code, or where to put it.
i am happy for any help you can give me as i have been trying all day to get this working in many ways and simply cannot.
namespace potato
module Main =
open System
open Gtk
type Handler()=class
[<Object>]
[<DefaultValue(true)>]
val mutable window1 : Window
end
[<EntryPoint>]
let Main(args) =
Application.Init()
let builder = new Builder("GUI.ui")
let handler = new Handler()
builder.Autoconnect(handler)
handler.window1.ShowAll()
Application.Run()
0
Here is the glade.xml
<?xml version="1.0" encoding="UTF-8"?>
<!-- Generated with glade 3.20.0 -->
<interface>
<requires lib="gtk+" version="3.18"/>
<object class="GtkWindow" id="window1">
<property name="width_request">1024</property>
<property name="height_request">576</property>
<property name="can_focus">False</property>
<child>
<placeholder/>
</child>
</object>
</interface>
Right, the problem was right in front of my eyes, and i ended up having to go through old test projects to see and realise what #scrwtp was hinting at, this is the old working code fixed for gtk3 gtkbuilder.
namespace potato
module Main =
open System
open Gtk
type Handler()=class
[<Builder.Object>]
[<DefaultValue(true)>]
val mutable window1 : Window
end
let OnDelete (args:DeleteEventArgs) =
Application.Quit()
args.RetVal <- true
[<EntryPoint>]
let Main (args) =
Application.Init()
let gxml = new Builder("GUI.xml")
let handler = new Handler()
do gxml.Autoconnect(handler)
handler.window1.DeleteEvent
|> Event.add OnDelete
handler.window1.ShowAll()
Application.Run()
0
The reason, i now understand is that i had specified a handler and passed nothing to it, because nothing was passed it IE:(handler.window1.DeleteEvent) it simply wouldnt show when i called showall, hope this helps someone else with a similar problem

Get xml-stylesheet when using xml type provider?

How to get the xml-stylesheet using xml type provider?
let xml = XmlProvider<"""<?xml version="1.0" encoding="iso-8859-1"?>
<?xml-stylesheet type='text/xsl' href='/stylesheets/application_internet.xsl'?>
<application>......</application>""").GetSample()
let stylesheetHref = xml.....?
Expect string '/stylesheets/application_internet.xsl'.
There is no easy way I know of to get processing instructions and associated data using TypeProviders (or Linq to XML).
It can be done like this, though:
For your example XML GetSample returns just the root element content, i.e. ....... Changing that a bit lets us access the root XElement. Knowing the processing node is its preceding sibling, we can get a XProcessingInstruction and extract the url from its Data.
#I "../packages/FSharp.Data.2.2.5/lib/net40"
#r "System.Xml.Linq"
#r "FSharp.Data.dll"
open FSharp.Data
open System.Text.RegularExpressions
open System.Xml.Linq
let href s = Regex.Match(s, "href='(?<url>.*?)'").Groups.["url"].Value
type Xml = XmlProvider<"""<?xml version="1.0" encoding="iso-8859-1"?>
<?xml-stylesheet type='text/xsl' href='/stylesheets/application_internet.xsl'?>
<application><other></other></application>""">
let doc = Xml.GetSample()
let stylesheetProcessing = (doc.XElement.PreviousNode :?> XProcessingInstruction)
// /stylesheets/application_internet.xsl
let url = href stylesheetProcessing.Data
Obviously this code expects the XML to always have a valid instruction in the same place. Adding error handling is left as an exercise :-)

Xml parsing in rails

I have this XML data:
<?xml version="1.0" encoding="UTF-8"?>
<responseParam>
<RESULT>-1</RESULT>
<ERROR_CODE>509</ERROR_CODE>
</responseParam>
How can I fetch the value of error code only?
I have tried this :
result = Net::HTTP.get(URI.parse(otpUrl))
data = Hash.from_xml(result)
puts "#{data['ERROR_CODE']}"
puts data[:ERROR_CODE]
printing only "data" gives me the whole hash. I am not able to get only the value of ERROR_CODE.
Any help ?
you can use Nokigiri here.
suppose this is your error.xml
<?xml version="1.0" encoding="UTF-8"?>
<responseParam>
<RESULT>-1</RESULT>
<ERROR_CODE>509</ERROR_CODE>
</responseParam>
you can do something like:-
#doc = Nokogiri::XML(File.open("error.xml"))
#doc.xpath("//ERROR_CODE")
will give you something like:-
# => ["<ERROR_CODE>509</ERROR_CODE>]"
The Node methods xpath and css actually return a NodeSet, which acts very much like an array, and contains matching nodes from the document.

Resources