The blog.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

107 lines
4.2 KiB

  1. ---
  2. title: XML Schema Validation for the command line
  3. description: >
  4. XML Schema Validation for the command line
  5. created: !!timestamp '2015-05-07'
  6. time: 2:17 PM
  7. tags:
  8. - xml
  9. - schema
  10. ---
  11. It turns out that unless you use a full fledge XML editor, validating
  12. your XML document against a schema is difficult. Most tools require you
  13. to specify a single schema file. If you have an XML document that
  14. contains more than one name space this doesn't work too well as often,
  15. each name space is in a separate schema file.
  16. The XML document has xmlns attributes which use a URI as the identifier.
  17. These URIs are for identifing it, and not a URL, so not able to be used.
  18. In fact, different cases in the URIs specify different name spaces even
  19. in the "host" part, though that is not the case with URLs. In order for
  20. validators to find the schema, the attribute
  21. [xsi:schemaLocation](http://www.w3.org/TR/xmlschema-1/#schema-loc) is
  22. used to map the name space URIs to the URLs of the schema.
  23. The `xsi:schemaLocation` mapping is very simple. It is simply a white
  24. space delimited list of URI/URL pairs. None of the command line tools
  25. that I used uses this attribute to make the schema validation simple.
  26. This includes [xmllint](http://xmlsoft.org/xmllint.html) which uses
  27. the libxml2 library. I also tried to use the Java XML library
  28. Xerces, but was unable to get it to work. Xerces did not provide a
  29. simple command line utility, and I couldn't figure out the correct java
  30. command line to invoke the validator class.
  31. My coworker, [Patrick](http://fivetwentysix.com/), found the blog entry,
  32. [Nokogiri XML schema validation with multiple schema files](http://avinmathew.com/nokogiri-xml-schema-validation-with-multiple-schema-files/),
  33. which talks about using `xs:import` to have a single schema file support
  34. multiple name spaces. With this, we realized that we could finally get
  35. our XML document verified.
  36. As I know shell scripting well, I decided to write a script to automate
  37. creating a unified schema and validate a document. The tools don't cache
  38. the schema documents, requiring fetching the schema each time you want
  39. to validate the XML document. We did attempt to write the schema files
  40. to disk, and reuse those, *but* there are issues in that some schemas
  41. reference other resources in them. If the schema is not retrieved from
  42. the web, these internal resources are not retrieved also, causing errors
  43. when validating some XML documents.
  44. With a little bit of help from `xsltproc` to extract xsi:schemaLocation,
  45. it wasn't to hard to generate the schema document and provide it to
  46. xmllint.
  47. The code ([xmlval.sh](http://www.funkthat.com/~jmg/xmlval.sh)):
  48. ``` { .shell .showlines }
  49. #!/bin/sh -
  50. cat <<EOF |
  51. <?xml version="1.0"?>
  52. <xsl:stylesheet version="1.0"
  53. xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  54. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  55. >
  56. <xsl:output method="text"/>
  57. <xsl:template match="/">
  58. <xsl:value-of select="/*/@xsi:schemaLocation"/>
  59. </xsl:template>
  60. </xsl:stylesheet>
  61. EOF
  62. xsltproc - "$1" |
  63. sed -e 's/ */\
  64. /g' |
  65. sed -e '/^$/d' |
  66. (echo '<?xml version="1.0" encoding="UTF-8"?>'
  67. echo '<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:nospace="nospace" targetNamespace="http://www.example.com/nospace">'
  68. while :; do
  69. if ! read a; then
  70. break
  71. fi
  72. if ! read b; then
  73. break
  74. fi
  75. echo '<xs:import namespace="'"$a"'" schemaLocation="'"$b"'"/>'
  76. done
  77. echo '</xs:schema>') |
  78. xmllint --noout --schema - "$1"
  79. ```
  80. Though the script looks complicated, it is a straight forward pipeline:
  81. 1. Lines 3-16 provide the xslt document to `xsltproc` on line 17 to
  82. extract schema location attribute.
  83. 1. Lines 18-20 replace multiple spaces with new lines and deletes any
  84. blank lines. It should probably also handle tabs, but none of the
  85. documents that I have had tabs. After this, we now have the odd
  86. lines containing the URI of the name space, and the even lines
  87. contain the URL for the schema.
  88. 1. Lines 21 and 22 are the header for the new schema document.
  89. 1. Lines 23-31 pulls in these line pairs and create the necessary
  90. `xs:import` lines.
  91. 1. Line 32 provides the closing element for the schema document.
  92. 1. Line 33 gives the schema document to xmllint for validation.