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.
 
 
 
 

111 lines
4.8 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 identifying 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. <code>[xsi:schemaLocation](https://www.w3.org/TR/xmlschema-1/#schema-loc)</code> 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](https://web.archive.org/web/20210415145100/http://xmlsoft.org/xmllint.html)<label for="sn-xmlintarchive"
  27. class="margin-toggle sidenote-number"></label>
  28. <input type="checkbox" id="sn-xmlintarchive" class="margin-toggle"/><span class="sidenote">Via WayBack Machine as original link it http only.</span>
  29. which uses the libxml2 library. I also tried to use the Java XML library
  30. Xerces, but was unable to get it to work. Xerces did not provide a
  31. simple command line utility, and I couldn't figure out the correct java
  32. command line to invoke the validator class.
  33. My coworker, [Patrick](https://web.archive.org/web/20151012162546/http://fivetwentysix.com/)<label for="sn-526archive" class="margin-toggle sidenote-number"></label>
  34. <input type="checkbox" id="sn-526archive" class="margin-toggle"/>
  35. <span class="sidenote">Via WayBack Machine as original link is now defunct.</span>, found the blog entry,
  36. [Nokogiri XML schema validation with multiple schema files](https://avinmathew.com/nokogiri-xml-schema-validation-with-multiple-schema-files/),
  37. which talks about using `xs:import` to have a single schema file support
  38. multiple name spaces. With this, we realized that we could finally get
  39. our XML document verified.
  40. As I know shell scripting well, I decided to write a script to automate
  41. creating a unified schema and validate a document. The tools don't cache
  42. the schema documents, requiring fetching the schema each time you want
  43. to validate the XML document. We did attempt to write the schema files
  44. to disk, and reuse those, *but* there are issues in that some schemas
  45. reference other resources in them. If the schema is not retrieved from
  46. the web, these internal resources are not retrieved also, causing errors
  47. when validating some XML documents.
  48. With a little bit of help from `xsltproc` to extract xsi:schemaLocation,
  49. it wasn't to hard to generate the schema document and provide it to
  50. xmllint.
  51. The code ([xmlval.sh](https://www.funkthat.com/~jmg/xmlval.sh)):
  52. ``` { .shell .showlines }
  53. #!/bin/sh -
  54. cat <<EOF |
  55. <?xml version="1.0"?>
  56. <xsl:stylesheet version="1.0"
  57. xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  58. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  59. >
  60. <xsl:output method="text"/>
  61. <xsl:template match="/">
  62. <xsl:value-of select="/*/@xsi:schemaLocation"/>
  63. </xsl:template>
  64. </xsl:stylesheet>
  65. EOF
  66. xsltproc - "$1" |
  67. sed -e 's/ */\
  68. /g' |
  69. sed -e '/^$/d' |
  70. (echo '<?xml version="1.0" encoding="UTF-8"?>'
  71. echo '<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:nospace="nospace" targetNamespace="http://www.example.com/nospace">'
  72. while :; do
  73. if ! read a; then
  74. break
  75. fi
  76. if ! read b; then
  77. break
  78. fi
  79. echo '<xs:import namespace="'"$a"'" schemaLocation="'"$b"'"/>'
  80. done
  81. echo '</xs:schema>') |
  82. xmllint --noout --schema - "$1"
  83. ```
  84. Though the script looks complicated, it is a straight forward pipeline:
  85. 1. Lines 3-16 provide the xslt document to `xsltproc` on line 17 to
  86. extract schema location attribute.
  87. 1. Lines 18-20 replace multiple spaces with new lines and deletes any
  88. blank lines. It should probably also handle tabs, but none of the
  89. documents that I have had tabs. After this, we now have the odd
  90. lines containing the URI of the name space, and the even lines
  91. contain the URL for the schema.
  92. 1. Lines 21 and 22 are the header for the new schema document.
  93. 1. Lines 23-31 pulls in these line pairs and create the necessary
  94. `xs:import` lines.
  95. 1. Line 32 provides the closing element for the schema document.
  96. 1. Line 33 gives the schema document to xmllint for validation.