Last active
September 26, 2020 06:23
-
-
Save wulftone/5431049 to your computer and use it in GitHub Desktop.
Simple xml-dtd validator
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!ELEMENT note (to,from,heading,body)> | |
<!ELEMENT to (#PCDATA)> | |
<!ELEMENT from (#PCDATA)> | |
<!ELEMENT heading (#PCDATA)> | |
<!ELEMENT body (#PCDATA)> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="ISO-8859-1"?> | |
<!DOCTYPE note SYSTEM "note.dtd"> | |
<note> | |
<to>Tove</to> | |
<from>Jani</from> | |
<heading>Reminder</heading> | |
<body>Don't forget me this weekend!</body> | |
</note> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0"?> | |
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"> | |
<xs:element name="note"> | |
<xs:complexType> | |
<xs:sequence> | |
<xs:element name="to" type="xs:string"/> | |
<xs:element name="from" type="xs:string"/> | |
<xs:element name="heading" type="xs:string"/> | |
<xs:element name="body" type="xs:string"/> | |
</xs:sequence> | |
</xs:complexType> | |
</xs:element> | |
</xs:schema> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
# encoding: utf-8 | |
# Ensure you have the libxml gem installed: | |
# gem install 'libxml-ruby' | |
# | |
# Use this file as follows: | |
# ruby validate-xml "path/to/my/file.xml" "path/to/my/file.dtd" | |
# | |
# For example, if this file and your xml and dtd files are in the same directory: | |
# ruby validate-xml "file.xml" "file.dtd" | |
# | |
# It will throw an error if there is something wrong, read the error carefully. | |
# If nothing is wrong, it'll give you a happy message! | |
# | |
# Bonus points, if you're using linux/mac, you should be able to omit the "ruby" portion | |
# of the command by making this file executable, like so: | |
# chmod +x validate-xml | |
# | |
# then you can just use it like this: | |
# ./validate-xml "path/to/my/file.xml" "path/to/my/file.dtd" | |
# | |
# or like this (note that it uses an xsd file): | |
# ./validate-xml "path/to/my/file.xml" "path/to/my/file.xsd" | |
# | |
# NOTE: if you have the xmllint program on your computer, you don't need this! | |
# xmllint | |
require 'libxml' | |
include LibXML | |
schema_filename = ARGV.pop | |
doc = XML::Document.file ARGV.pop | |
result = if schema_filename.end_with? "dtd" | |
schema = XML::Dtd.new File.read(schema_filename) | |
doc.validate schema | |
else | |
schema = LibXML::XML::Schema.new(schema_filename) | |
result = doc.validate_schema(schema) do |message,flag| | |
log.debug(message) | |
puts message | |
end | |
end | |
puts "No errors, yay!" if result |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment