Create local XML and XSD files can be evaluated successfully
-
I wanted to use XML files to store test cases for a little development project I am doing for fun. The test cases would be stored on my local hard drive, and the schema design would be very simple. I thought I Notepad++ would be able to help me evaluate the XML with the XSD, and the XSD with the W3 XSD schema quickly. That is wishful thinking. After lots of searching and reading, I found that lots of people are having this problem, and that there aren’t any posts that lead to a useful solution. I was able to solve the problem, and I am posting my files below. Hopefully, these files will be of assistance.
My problem is to create an XML file containing one or more “point buffers”, clouds of points to be used in a geometry program to create a line, circle, cone, plane, etc. Each point buffer has a name, a count of the number of points it contains, and a list of the x, y and z coordinates of all the points in the point buffer.
My initial idea for the Pointbuffers.xml file to hold this data follows:
<?xml version="1.0" encoding="UTF-8"?> <Pointbuffers> <Pointbuffer name="Quad 1 test case" count="4"> <!-- Pointbuffer #1: Quad 1 - four points in quad 1 (+x,+y) to test point fit algorithm --> <point id="1" x="4" y="5" z="0"/> <point id="2" x="5" y="4" z="0"/> <point id="3" x="6" y="5" z="0"/> <point id="4" x="5" y="6" z="0"/> </Pointbuffer> </Pointbuffers>
Then I created a Pointbuffers.xsd file that could be used to evaluate the XML file:
<?xml version="1.0" encoding="UTF-8"?> <xs:schema xmlns:xs ="http://www.w3.org/2001/XMLSchema"> <xs:element name="Pointbuffers"> <xs:complexType> <xs:sequence> <xs:element ref="Pointbuffer" maxOccurs="unbounded"/> </xs:sequence> </xs:complexType> </xs:element> <xs:element name="Pointbuffer"> <xs:complexType> <xs:sequence> <xs:element ref="point" maxOccurs="unbounded"/> </xs:sequence> <xs:attribute name="name" type="xs:string" use="required"/> <xs:attribute name="count" type="xs:integer" use="required" /> </xs:complexType> </xs:element> <xs:element name="point"> <xs:complexType> <xs:attribute name="id" type="xs:string" use="required" /> <xs:attribute name="x" type="xs:integer" use="required" /> <xs:attribute name="y" type="xs:integer" use="required" /> <xs:attribute name="z" type="xs:integer" use="required" /> </xs:complexType> </xs:element> </xs:schema>
The problem was now how to get the XML file to reference the XSD file (in the same folder) so Notepad++ can validate the file, and to get the XSD file to reference the standard schema templates to that Notepad++ can validate the schema. If you install the XML tools plugin, Notepad++ will give you a hint about how to make the XML file reference the local XSD file, but that hint is incomplete.
The modifications I made to the Pointbuffers.xsd header enable Notepad++ to successfully validate follow:
<?xml version="1.0" encoding="UTF-8"?> <xs:schema xmlns:xs ="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="https://www.w3.org/2001/XMLSchema https://www.w3.org/2009/XMLSchema/XMLSchema.xsd"> <!-- The rest of the original XSD file goes here --> </xs:schema>
Note that Notepad++ could know that it is working with an XSD file, and give a better hint about how to make validate work. The hint it give now doesn’t help.
To make the Pointbuffers.xml file reference the Pointbuffers.xsd file for validation I made the following modifications to its header:
<?xml version="1.0" encoding="utf-8"?> <Pointbuffers xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="file:///C:/Users/MarkA/source/XMLTest/PointBuffers/PointBuffers.xsd"> <!-- The rest of the original XML file goes here --> </Pointbuffers>
**Note: ** The noNameSpaceSchemaLocation value is a URI. This path has to be able to work as a URL in a browser. You need three slashes after “file:” (Think protocol://host/path where protocol is ‘file’ and host is empty), and you can also eliminate the double slashes along the path. I believe that the double slashes will help with file systems that allow spaces in file and directory names, but you would be wise to avoid that complication in your path naming.
If it is not working: copy the full file specification for the XSD into the address bar of of your browser:
file:///C:/Users/MarkA/source/XMLTest/PointBuffers/PointBuffers.xsd
If the XSD does not display in the browser, delete all but the last component of the path (email.xsd) and see if you can’t display the parent directory. Continue in this manner, walking up the directory structure until you discover where the path diverges from the reality of your local filesystem.
If the XSD does display in the browser, be prepared to hear that the tool is broken or that you must work around some limitation. File a bug, or post an article to the community.
Hope this helps someone!