-
Notifications
You must be signed in to change notification settings - Fork 39
Add XSD Schema validation #111
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
wherrera10
wants to merge
23
commits into
JuliaIO:master
Choose a base branch
from
wherrera10:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
cc59a4d
add validate methods
wherrera10 3e31fb3
Add files via upload
wherrera10 f872179
Update runtests.jl
wherrera10 5781102
Add files via upload
wherrera10 61f80fb
add schema, export validate
wherrera10 2848996
add XMLValidationError
wherrera10 dfdbfaa
more tests
wherrera10 2ab855e
correct struct.ptr references
wherrera10 c5734fa
add methods
wherrera10 b12eaf5
documentation
wherrera10 beed841
Update README.md
wherrera10 2ebfab8
use Libc.free to free ctxt
wherrera10 c8ed390
change constructors, add a finalizer
wherrera10 7163c24
update XMLSchema constructor
wherrera10 360dca2
code review suggestions
wherrera10 06c55cd
code review suggested change
wherrera10 cfa8c5c
put tests into a testset
wherrera10 9c3ca91
Update src/schema.jl
omus 44bb208
recheck a failing test
wherrera10 b80f02a
Create invalid.xml
wherrera10 7d3d68d
add tests
wherrera10 f8dda7b
change two-file validation method
wherrera10 32dce3f
clean up testset
wherrera10 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
""" | ||
An XML Schema Document, produced by an XML file or XMLDocument that is XML for the schema. | ||
""" | ||
mutable struct XMLSchema | ||
ptr::Xptr | ||
function XMLSchema(ctxt::Xptr) | ||
schema = ccall((:xmlSchemaParse, libxml2), Xptr, (Xptr,), ctxt) | ||
schema != C_NULL || throw(XMLValidationError("Bad XML Schema in Document")) | ||
ccall((:xmlSchemaFreeParserCtxt, libxml2), Cvoid, (Xptr,), ctxt) | ||
obj = new(schema) | ||
finalizer(x -> Libc.free(x.ptr), obj) | ||
end | ||
end | ||
|
||
""" | ||
Create an XMLSchema from a file or url | ||
""" | ||
function XMLSchema(url::String) | ||
ctxt = ccall((:xmlSchemaNewParserCtxt, libxml2), Xptr, (Cstring,), url) | ||
ctxt != C_NULL || throw(XMLValidationError("Bad XML Schema at " * url)) | ||
return XMLSchema(ctxt) | ||
end | ||
|
||
""" | ||
Create an XMLSchema from an XMLDocument | ||
""" | ||
function XMLSchema(doc::XMLDocument) | ||
ctxt = ccall((:xmlSchemaNewDocParserCtxt, libxml2), Xptr, (Xptr,), doc.ptr) | ||
ctxt != C_NULL || throw(XMLValidationError("Bad XML Schema in Document")) | ||
return XMLSchema(ctxt) | ||
end | ||
|
||
""" | ||
Use an existing XMLschema to validate | ||
""" | ||
function _schema_valid_ctxt(f::Function, schema::XMLSchema) | ||
ctxt = ccall((:xmlSchemaNewValidCtxt, libxml2), Xptr, (Xptr,), schema.ptr) | ||
err = try | ||
f(ctxt) | ||
finally | ||
Libc.free(ctxt) | ||
end | ||
return err | ||
end | ||
|
||
""" | ||
Validate an XMLDocument with an XMLSchema | ||
Returns true if valid | ||
""" | ||
function validate(xml::XMLDocument, schema::XMLSchema) | ||
err = _schema_valid_ctxt(schema) do ctxt | ||
ccall((:xmlSchemaValidateDoc, libxml2), | ||
Cint, (Xptr, Xptr), ctxt, xml.ptr) | ||
end | ||
return err == 0 | ||
end | ||
|
||
""" | ||
Validate an XML file or url with an XMLSchema | ||
Returns true if valid | ||
""" | ||
function validate(url::String, schema::XMLSchema) | ||
err = _schema_valid_ctxt(schema) do ctxt | ||
ccall((:xmlSchemaValidateFile, libxml2), | ||
Cint, (Xptr, Cstring), ctxt, url) | ||
end | ||
return err == 0 | ||
end | ||
|
||
""" | ||
Validate an XMLElement of an XMLDocument with an XMLSchema | ||
Returns true if valid | ||
""" | ||
function validate(elem::XMLElement, schema::XMLSchema) | ||
err = _schema_valid_ctxt(schema) do ctxt | ||
ccall((:xmlSchemaValidateOneElement, libxml2), | ||
Cint, (Xptr, Xptr), ctxt, elem.node.ptr) | ||
end | ||
return err == 0 | ||
end | ||
wherrera10 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
""" | ||
Validate an XML file or url with an XSD file or url | ||
Returns true if valid | ||
""" | ||
function validate(url::String, schemafile::String) | ||
schema = XMLSchema(schemafile) | ||
return validate(url, schema) | ||
end |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
|
||
<shiporder orderid="889923" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:noNamespaceSchemaLocation="shiporder.xsd"> | ||
<orderperson>John Smith</orderperson> | ||
<shipto> | ||
<name>Ola Nordmann</name> | ||
<address>Langgt 23</address> | ||
<city>4000 Stavanger</city> | ||
<country>Norway</country> | ||
</shipto> | ||
<item> | ||
<titles>Empire Burlesque, Gold Ribbon</titles> | ||
<note>Special Edition</note> | ||
<quantity>1</quantity> | ||
<price>10.90</price> | ||
</item> | ||
<item> | ||
<title>Hide your heart</title> | ||
<quantity>1</quantity> | ||
<price>9.90</price> | ||
</item> | ||
</shiporder> |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
|
||
<shiporder orderid="889923" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:noNamespaceSchemaLocation="shiporder.xsd"> | ||
<orderperson>John Smith</orderperson> | ||
<shipto> | ||
<name>Ola Nordmann</name> | ||
<address>Langgt 23</address> | ||
<city>4000 Stavanger</city> | ||
<country>Norway</country> | ||
</shipto> | ||
<item> | ||
<title>Empire Burlesque</title> | ||
<note>Special Edition</note> | ||
<quantity>1</quantity> | ||
<price>10.90</price> | ||
</item> | ||
<item> | ||
<title>Hide your heart</title> | ||
<quantity>1</quantity> | ||
<price>9.90</price> | ||
</item> | ||
</shiporder> |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<?xml version="1.0" encoding="UTF-8" ?> | ||
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"> | ||
|
||
<xs:element name="shiporder"> | ||
<xs:complexType> | ||
<xs:sequence> | ||
<xs:element name="orderperson" type="xs:string"/> | ||
<xs:element name="shipto"> | ||
<xs:complexType> | ||
<xs:sequence> | ||
<xs:element name="name" type="xs:string"/> | ||
<xs:element name="address" type="xs:string"/> | ||
<xs:element name="city" type="xs:string"/> | ||
<xs:element name="country" type="xs:string"/> | ||
</xs:sequence> | ||
</xs:complexType> | ||
</xs:element> | ||
<xs:element name="item" maxOccurs="unbounded"> | ||
<xs:complexType> | ||
<xs:sequence> | ||
<xs:element name="title" type="xs:string"/> | ||
<xs:element name="note" type="xs:string" minOccurs="0"/> | ||
<xs:element name="quantity" type="xs:positiveInteger"/> | ||
<xs:element name="price" type="xs:decimal"/> | ||
</xs:sequence> | ||
</xs:complexType> | ||
</xs:element> | ||
</xs:sequence> | ||
<xs:attribute name="orderid" type="xs:string" use="required"/> | ||
</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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
@testset "XML Validation with XSD" begin | ||
|
||
@test validate("valid.xml", "valid.xsd") | ||
@test validate("invalid.xml", "valid.xsd") == false | ||
|
||
doc = parse_file("valid.xml") | ||
schema = XMLSchema("valid.xsd") | ||
|
||
@test validate("valid.xml", schema) | ||
@test validate("invalid.xml", schema) == false | ||
|
||
@test validate(doc, schema) | ||
|
||
@test validate(root(doc), schema) | ||
wherrera10 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
wherrera10 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
end |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.