lib/schematron-nokogiri.rb in schematron-nokogiri-0.0.1 vs lib/schematron-nokogiri.rb in schematron-nokogiri-0.0.2
- old
+ new
@@ -27,14 +27,14 @@
end
end
# Compile schematron into xsl that maps to svrl
- validator_doc = xforms.inject(schema_doc) {
+ @validator_doc = xforms.inject(schema_doc) {
|xml, xsl| xsl.transform xml
}
- @validator_xsl = Nokogiri::XSLT(validator_doc.to_s)
+ @validator_xsl = Nokogiri::XSLT(@validator_doc.to_s)
end
def validate(instance_doc)
# Validate the xml
@@ -43,16 +43,35 @@
# compile the errors and log any messages
rule_hits(results_doc, instance_doc, 'assert', '//svrl:failed-assert') +
rule_hits(results_doc, instance_doc, 'report', '//svrl:successful-report')
end
+ def prefixes
+ namespaces = {}
+
+ @validator_doc.namespaces.each {
+ |k, v| namespaces[k.gsub 'xmlns:', ''] = v
+ }
+ namespaces
+ end
+
# Look for reported or failed rules of a particular type in the instance doc
def rule_hits(results_doc, instance_doc, rule_type, xpath)
results = []
results_doc.root.xpath(xpath, NS_PREFIXES).each do |hit|
- context = instance_doc.root.xpath(hit['location'], NS_PREFIXES).first
+ context_tag = hit
+ context_path = nil
+ while context_path.nil?
+ context_tag = context_tag.previous_sibling
+ context_path = context_tag['context']
+ end
+
+ context = instance_doc.root.xpath(
+ context_path ? '/' + context_path : hit['location'],
+ NS_PREFIXES.merge(prefixes)
+ ).first
hit.xpath('svrl:text/text()', NS_PREFIXES).each do |message|
results << {
:rule_type => rule_type,
:type => node_type(context),