Source_sink dtd does not exist

I get the following error when trying to write a MusicXML document to disk :

?- [‘C:/path-to/file-name.pl’].
ERROR: c:/path-to/file-name.pl:757:
ERROR: source_sink `dtd(‘D:\path-to\schema\partwise.dtd’)’ does not exist
Warning: c:/path-to/file-name.pl:755:
Warning: Goal (directive) failed: user:write_musicxml

I’m using xml_write(Stream, Document, [doctype(‘score-partwise’), dtd(‘D:\path-to\schema\partwise.dtd’)]).

I get the same error for ‘D:/path-to/schema/partwise.dtd’ or when I drop the doctype(‘score-partwise’) option.

All .mod files the dtd refers to are in the directory of the partwise.dtd file.

I’m using the same dtd for transforming or selecting data from MusicXML files with the Saxon XSLT processor without issues.

For completeness reasons I include a test predicate :

write_musicxml :-
	Document = [ element('score-partwise', 
		[], 
		[ element(part, 
			[ id="P1"], 
			[ element(measure, 
				[ number="1"], 
				[ element(note, 
					[ default-x="50", default-y="70"], 
					[ element(pitch, 
						[], 
						[ element(step, [], ['C']),
                        element(octave, [], ['4'])
						]),
                    element(duration, [], ['4']),
                    element(type, [], ['quarter'])
					])
				])
			])
		])
	],
	open('R:/temp/test.xml', write, Stream),
	xml_write(Stream, Document, [doctype('score-partwise'), dtd('D:path-to/schema/partwise.dtd')]),
	close(Stream).

Can .xsd files be used for writing XML documents and how?

Thanks in advance for any help.

The DTD is searched for using the file search path dtd rather than an absolute file name. So, you need (always use forward / in Prolog code):

:- multifile user:file_search_path/2.
user:file_search_path(dtd, 'd:/path-to/schema').

And next the option dtd(partwise)

Not by the bundled libraries. There is some XSD support in add-ons. But in any case, unlike SGML, you do not need a schema file for writing valid XML.

Hello,
I also tried to use xml schemas for MusicXML or MEI in the past but could not understand a thing from the swi-prolog documentation.
In my case, I wanted to do automatic conversion of numbers from atom to prolog numbers when reading and writing xml, but I don’t think this is implemented at all.

Just for curiosity, could you tell us what kind of project are you using MusicXML and prolog for ?
In my case, I’m trying to write a pure bidirectional grammar of the common music notation.

I suspect writing might work. Reading definitely not. You need a separate pass over the tree. Note that this is typically very easily done using mapsubterms/3. Dealing with XML attributes could use the DTD. For content I guess you would need an XSD description.

Time to move to JSON and forget XML :slight_smile:

Well, after dabbling in music notation for a long time, the expressivity of XML is quite nice.
The use of a structured tree of nodes with explicit attributes and children maps really well to the complex structure of the music notation and is not easily replicated in JSON.

Do you mean out of the box, or do I need something with DTD or XSD ?
Please note that I am a novice when it comes to these things…

Thanks for the tip, I will definitely look into this.

I think out of the box, but I didn’t test. It would not allow to control the precision of floats. They are printed as Prolog does: with the minimum number of digits such that it reads back to precisely the same float.

I usually use MusicXML for MuseScore, but there are several other programs that use this standard for exporting or importing music data. It’s way more powerful than MIDI because it includes both MIDI and music notation. With MIDI we are limited by the lack of intelligent quantisation algorithms.

I usually use AutoHotkey for handling MusicXML because there exists an elegant XPath solution for writing and reading XML documents in AHK. I also use XSLT for selecting data.
I’m now testing if Prolog could be used as well, because I’m using Prolog more than any other language.

MusicXML is a W3C standard. The Music JSON proposals/implementations I’ve read about are not ready or widely used. I see XML most of the time and I’ve been using hundreds of music tools. Note that the most widely used music notation editor, MuseScore, uses XML as its internal data format.
Nevertheless, I’m also using ABC Notation and there seems to be a JSON library associated to it. ABC is used on web pages to show music notation.