File: Validate.hs

package info (click to toggle)
haskell-hxt-relaxng 9.1.4-4
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 684 kB
  • ctags: 23
  • sloc: haskell: 12,339; makefile: 167; xml: 37
file content (34 lines) | stat: -rw-r--r-- 963 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
module Validate where

import Data.Maybe

import Text.XML.HXT.Core
import Text.XML.HXT.RelaxNG

loadSchema	:: String -> IO (Maybe XmlTree)
loadSchema schema =
    runX ( validateSchemaWithRelax schema ) >>= return . listToMaybe

validateWithSchema	:: XmlTree -> XmlTree -> Maybe String
validateWithSchema schema doc
    = listToMaybe $
      runLA
      ( validateRelax' schema
        >>>
        getErrorMsg
      ) doc

main1    :: String -> String -> IO ()
main1 sf df
    = do
      schema <- loadSchema sf >>= return . fromJust
      doc    <- runX ( readDocument [ withValidate no ] df ) >>= return . head
      case validateWithSchema schema doc of
        Just e  -> putStrLn $ "Document " ++ show df ++ " is not valid for schema " ++ show sf ++ ": " ++ e
        Nothing -> putStrLn $ "Document " ++ show df ++ " is valid for schema " ++ show sf

main :: IO ()
main
    = do
      main1 "valid1.rng" "valid1.xml"
      main1 "valid1.rng" "invalid1.xml"