#!/usr/bin/python

import unittest
import rng
import copy
#from Ft.Lib.pDomlette import PyExpatReader
from xml.sax import make_parser, parseString, parse
from Ft.Xml import InputSource
from Ft.Xml.Domlette import NonvalidatingReader


"""
The contents of this file are subject to the Mozilla Public License  
Version 1.1 (the "License"); you may not use this file except in  
compliance with the License. 
You may obtain a copy of the License at http://www.mozilla.org/MPL/ 
Software distributed under the License is distributed on an "AS IS"  
basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the  
License for the specific language governing rights and limitations under  
the License. 

The Original Code is available at http://downloads.xmlschemata.org/python/xvif/

The Initial Developer of the Original Code is Eric van der Vlist. Portions  
created by Eric van der Vlist are Copyright (C) 2002. All Rights Reserved. 

Contributor(s): 
"""


class TestRelaxNg(unittest.TestCase):
	
	def schemaParser(self, schemaString, valid=1, display=0):
		parser = make_parser()
		schema = rng.RngParser()
		parser.setContentHandler(schema)
		parser.setFeature("http://xml.org/sax/features/namespaces", 1)
		factory = InputSource.DefaultFactory
		isrc=factory.fromString(schemaString, "dummy")
		exception = 0
		display=1
		if display:
			print "-------------------------"
			print schemaString
		msg=""
		try:
			parser.parse(isrc.stream)
			if display:
				print schema
		except (rng.RngSchemaInvalidException, rng.RngSchemaInvalidRecursionException), msg:
			exception=1
		if valid:
			self.failUnless(exception==0,
				'the schema %s should be valid (%s)' % (schemaString, msg))
		else:
			self.failUnless(exception==1,
				'the schema %s should be invalid' % schemaString)
		return schema

	def validateInstance(self, schema, instanceString, valid):
		reader = NonvalidatingReader
		doc = reader.parseString(instanceString, "dummy")
		s = copy.deepcopy(schema.grammar)
		deriv = s.deriv(doc.firstChild)
		if valid:
			self.failUnless(deriv.nullable(),
				'Should be valid (%s, %s)' % (instanceString, deriv))
		else:
			self.failUnless(not deriv.nullable(),
				'Should not be valid (%s, %s)' % (instanceString, deriv))
		return deriv

		
	def test1element(self):
		schema = self.schemaParser("""
		<element xmlns='http://relaxng.org/ns/structure/1.0' name='foo' 
			xmlns:if='http://namespaces.xmlschemata.org/xvif/iframe'>
			<data type='token'>
				<if:transform type='http://www.w3.org/TR/1999/REC-xpath-19991116' apply='normalize-space()'/>
			</data>
		</element>""")
		self.validateInstance(schema, "<foo>bar</foo>", 1)
		self.validateInstance(schema, "<foo><bar>bar</bar></foo>", 1)
		self.validateInstance(schema, "<foo><bar></bar></foo>", 1)
		self.validateInstance(schema, "<foo></foo>", 0)
		

	def test2element(self):
		schema = self.schemaParser("<element xmlns='http://relaxng.org/ns/structure/1.0' name='foo' xmlns:if='http://namespaces.xmlschemata.org/xvif/iframe'><data type='token'><if:transform type='http://www.w3.org/TR/1999/REC-xpath-19991116' apply='normalize-space()'/><except><value>foo</value></except></data></element>")
		self.validateInstance(schema, "<foo>bar</foo>", 1)
		self.validateInstance(schema, "<foo><bar>bar</bar></foo>", 1)
		self.validateInstance(schema, "<foo><bar></bar></foo>", 1)
		self.validateInstance(schema, "<foo></foo>", 1)
		self.validateInstance(schema, "<foo>foo</foo>", 0)
		self.validateInstance(schema, "<foo><bar>foo</bar></foo>", 0)
		self.validateInstance(schema, "<foo> <bar> foo </bar> </foo>", 0)
		self.validateInstance(schema, "<foo> f<bar>o</bar>o </foo>", 0)
		
	def test3element(self):
		schema = self.schemaParser("<element xmlns='http://relaxng.org/ns/structure/1.0' name='foo' xmlns:if='http://namespaces.xmlschemata.org/xvif/iframe'><data type='string'><if:transform type='http://www.w3.org/TR/1999/REC-xpath-19991116' apply='normalize-space()'/><except><value>foo</value></except></data></element>")
		self.validateInstance(schema, "<foo>bar</foo>", 1)
		self.validateInstance(schema, "<foo><bar>bar</bar></foo>", 1)
		self.validateInstance(schema, "<foo><bar></bar></foo>", 1)
		self.validateInstance(schema, "<foo></foo>", 1)
		self.validateInstance(schema, "<foo>foo</foo>", 0)
		self.validateInstance(schema, "<foo><bar>foo</bar></foo>", 0)
		self.validateInstance(schema, "<foo> <bar> foo </bar> </foo>", 0)
		self.validateInstance(schema, "<foo> f<bar>o</bar>o </foo>", 0)

	def test4element(self):
		schema = self.schemaParser("<element xmlns='http://relaxng.org/ns/structure/1.0' name='foo' xmlns:if='http://namespaces.xmlschemata.org/xvif/iframe'><list><if:transform type='http://www.w3.org/TR/1999/REC-xpath-19991116' apply='translate(., " + '",", " "' + ")'/><zeroOrMore><choice><value>x</value><value>y</value></choice></zeroOrMore></list></element>")
		self.validateInstance(schema, "<foo>x y</foo>", 1)
		self.validateInstance(schema, "<foo>xy</foo>", 0)
		self.validateInstance(schema, "<foo>x, y</foo>", 1)
		self.validateInstance(schema, "<foo><bar>x,</bar> y</foo>", 1)
		self.validateInstance(schema, "<foo><bar>x,</bar> x y</foo>", 1)

	def test5element(self):
		schema = self.schemaParser("""
		<element xmlns='http://relaxng.org/ns/structure/1.0' name='foo' 
			xmlns:if='http://namespaces.xmlschemata.org/xvif/iframe'>
			<list>
				<if:transform type='http://www.w3.org/TR/1999/REC-xpath-19991116' 
					apply='translate(concat(., name()), "," , " ")'/>
					<zeroOrMore>
						<choice>
							<value>x</value>
							<value>y</value>
						</choice>
					</zeroOrMore>
				</list>
			</element>""")
		self.validateInstance(schema, "<foo>x y</foo>", 1)
		self.validateInstance(schema, "<foo>xy</foo>", 0)
		self.validateInstance(schema, "<foo>x, y</foo>", 1)
		self.validateInstance(schema, "<foo><bar>x,</bar> x y</foo>", 0)
		self.validateInstance(schema, "<foo><bar>x,</bar> y</foo>", 0)
	
	def test6element(self):
		schema = self.schemaParser("""<?xml version="1.0" encoding="utf-8"?>
		<element xmlns="http://relaxng.org/ns/structure/1.0" name="foo"
			xmlns:if='http://namespaces.xmlschemata.org/xvif/iframe'>
  		<choice>
  			<if:transform type='http://www.w3.org/TR/1999/REC-xpath-19991116' 
					apply='concat(year,"-", month, "-", day)'/>
     			<value>2002-06-07</value>
     			<value>2002-06-09</value>
  		</choice>
		</element>""")
		self.validateInstance(schema, "<foo>x y</foo>", 0)
		self.validateInstance(schema, 
			"<foo><date><year>2002</year><month>06</month><day>07</day></date></foo>", 1)
		self.validateInstance(schema, 
			"<foo><date><year>2002</year><month>06</month><day>08</day></date></foo>", 0)

	def test7element(self):
		schema = self.schemaParser("""<?xml version="1.0" encoding="utf-8"?>
		<element xmlns="http://relaxng.org/ns/structure/1.0" name="foo"
			xmlns:if='http://namespaces.xmlschemata.org/xvif/iframe'>
  		<choice xmlns:b="bar">
  			<if:transform type='http://www.w3.org/TR/1999/REC-xpath-19991116' 
					apply='concat(b:year,"-", b:month, "-", b:day)'/>
     			<value>2002-06-07</value>
     			<value>2002-06-09</value>
  		</choice>
		</element>""")
		self.validateInstance(schema, "<foo>x y</foo>", 0)
		self.validateInstance(schema, 
			"<foo><date xmlns='bar'><year>2002</year><month>06</month><day>07</day></date></foo>", 1)
		self.validateInstance(schema, 
			"<foo><date><year>2002</year><month>06</month><day>07</day></date></foo>", 0)

	def test8element(self):
		schema = self.schemaParser("""<?xml version="1.0" encoding="utf-8"?>
		<element xmlns="http://relaxng.org/ns/structure/1.0" name="date"
			xmlns:if='http://namespaces.xmlschemata.org/xvif/iframe' xmlns:b="bar">
 			<if:transform return="inner" type='http://www.w3.org/TR/1999/REC-xpath-19991116' 
				apply='concat(b:year,"-", b:month, "-", b:day)'/>
  		<choice>
     			<value>2002-06-07</value>
     			<value>2002-06-09</value>
  		</choice>
		</element>""")
		self.validateInstance(schema, "<date>x y</date>", 0)
		self.validateInstance(schema, 
			"<date xmlns:b='bar'><b:year>2002</b:year><b:month>06</b:month><b:day>07</b:day></date>", 1)
		self.validateInstance(schema, 
			"<date xmlns:b='bar1'><b:year>2002</b:year><b:month>06</b:month><b:day>07</b:day></date>", 0)
		self.validateInstance(schema, 
			"<date xmlns:b='bar'><b:year>2001</b:year><b:month>06</b:month><b:day>07</b:day></date>", 0)
		self.validateInstance(schema, 
			"<date><year>2002</year><month>06</month><day>07</day></date>", 0)

	def test9element(self):
		schema = self.schemaParser("""<?xml version="1.0" encoding="utf-8"?>
		<element xmlns="http://relaxng.org/ns/structure/1.0" name="foo"
			xmlns:if='http://namespaces.xmlschemata.org/xvif/iframe' xmlns:b="bar">
 			<if:transform return="inner" type='http://www.w3.org/TR/1999/REC-xpath-19991116' 
				apply='b:*'/>
			<optional>
		 		<element name="b:foo"><empty/></element>
				<element name="b:bar"><empty/></element>
			</optional>
		</element>""")
		self.validateInstance(schema, "<foo>x y</foo>", 1)
		self.validateInstance(schema, 
			"<foo xmlns:b='bar'></foo>", 1)
		self.validateInstance(schema, 
			"<foo xmlns:b='bar'><bar/></foo>", 1)
		self.validateInstance(schema, 
			"<foo xmlns:b='bar'>jsqdgjhg</foo>", 1)
		self.validateInstance(schema, 
			"<foo xmlns:b='bar'>jsq<b:foo/>dg<b:bar/>jhg</foo>", 1)
		self.validateInstance(schema, 
			"<foo xmlns:b='bar'>jsq<b:foo/>dg<b:bar/><foo/>jhg</foo>", 1)
		self.validateInstance(schema, 
			"<foo xmlns:b='bar'>jsq<b:foo/>dgjhg</foo>", 0)
		self.validateInstance(schema, 
			"<foo xmlns:b='bar'>jsq<b:foo/>dg<b:bar/><foo/>jh<b:x/>g</foo>", 0)

	def test10element(self):
		schema = self.schemaParser("""<?xml version="1.0" encoding="utf-8"?>
		<element xmlns="http://relaxng.org/ns/structure/1.0" name="foo"
			xmlns:if='http://namespaces.xmlschemata.org/xvif/iframe' xmlns:b="bar">
 			<if:transform return="inner" type='http://www.w3.org/TR/1999/REC-xpath-19991116' 
				apply='b:*'/>
			<element name="b:foo"><empty/></element>
		</element>""")
		self.validateInstance(schema, "<foo>x y</foo>", 0)
		self.validateInstance(schema, 
			"<foo xmlns:b='bar'></foo>", 0)
		self.validateInstance(schema, 
			"<foo xmlns:b='bar'><bar/></foo>", 0)
		self.validateInstance(schema, 
			"<foo xmlns:b='bar'>jsqdgjhg</foo>", 0)
		self.validateInstance(schema, 
			"<foo xmlns:b='bar'>jsq<b:foo/>dg<bar/>jhg</foo>", 1)
		self.validateInstance(schema, 
			"<foo xmlns:b='bar'>jsq<b:foo/>dghg</foo>", 1)
		self.validateInstance(schema, 
			"<foo xmlns:b='bar'>jsq<b:foo/>d<b:foo/>gjhg</foo>", 0)
		self.validateInstance(schema, 
			"<foo xmlns:b='bar'>jsq<b:foo/>dg<b:bar/><foo/>jh<b:x/>g</foo>", 0)

	def test11element(self):
		schema = self.schemaParser("""<?xml version="1.0" encoding="utf-8"?>
		<element xmlns="http://relaxng.org/ns/structure/1.0" name="foo"
			xmlns:if='http://namespaces.xmlschemata.org/xvif/iframe' xmlns:b="bar">
 			<if:transform return="inner" type='http://www.w3.org/TR/1999/REC-xpath-19991116' 
				apply='b:*|@*'/>
			<attribute name="foo"/>
			<element name="b:foo"><empty/></element>
		</element>""")
		self.validateInstance(schema, "<foo>x y</foo>", 0)
		self.validateInstance(schema, 
			"<foo xmlns:b='bar'></foo>", 0)
		self.validateInstance(schema, 
			"<foo xmlns:b='bar'><bar/></foo>", 0)
		self.validateInstance(schema, 
			"<foo xmlns:b='bar'>jsqdgjhg</foo>", 0)
		self.validateInstance(schema, 
			"<foo xmlns:b='bar' foo=''>jsq<b:foo/>dg<bar/>jhg</foo>", 1)
		self.validateInstance(schema, 
			"<foo xmlns:b='bar' foo=''>jsq<b:foo/>dghg</foo>", 1)
		self.validateInstance(schema, 
			"<foo xmlns:b='bar'>jsq<b:foo/>dghg</foo>", 0)
		self.validateInstance(schema, 
			"<foo xmlns:b='bar'>jsq<b:foo/>d<b:foo/>gjhg</foo>", 0)
		self.validateInstance(schema, 
			"<foo xmlns:b='bar'>jsq<b:foo/>dg<b:bar/><foo/>jh<b:x/>g</foo>", 0)


	def test12element(self):
		schema = self.schemaParser("""<?xml version="1.0" encoding="utf-8"?>
		<element xmlns="http://relaxng.org/ns/structure/1.0" name="foo"
			xmlns:if='http://namespaces.xmlschemata.org/xvif/iframe' xmlns:b="bar">
 			<if:transform return="inner" type='http://www.w3.org/TR/1999/REC-xpath-19991116' 
				apply='b:*|@*[namespace-uri()=""]|text()'/>
			<attribute name="foo"/>
			<element name="b:foo"><empty/></element>
		</element>""")
		self.validateInstance(schema, "<foo>x y</foo>", 0)
		self.validateInstance(schema, 
			"<foo xmlns:b='bar'></foo>", 0)
		self.validateInstance(schema, 
			"<foo xmlns:b='bar'><bar/></foo>", 0)
		self.validateInstance(schema, 
			"<foo xmlns:b='bar'>jsqdgjhg</foo>", 0)
		self.validateInstance(schema, 
			"<foo xmlns:b='bar' foo=''>jsq<b:foo/>dg<bar/>jhg</foo>", 0)
		self.validateInstance(schema, 
			"<foo xmlns:b='bar' foo=''><b:foo/></foo>", 1)
		self.validateInstance(schema, 
			"<foo xmlns:b='bar' foo='' b:bar=''><b:foo/></foo>", 1)
		self.validateInstance(schema, 
			"<foo xmlns:b='bar' foo=''>jsq<b:foo/>dghg</foo>", 0)
		self.validateInstance(schema, 
			"<foo xmlns:b='bar'>jsq<b:foo/>dghg</foo>", 0)
		self.validateInstance(schema, 
			"<foo xmlns:b='bar'>jsq<b:foo/>d<b:foo/>gjhg</foo>", 0)
		self.validateInstance(schema, 
			"<foo xmlns:b='bar'>jsq<b:foo/>dg<b:bar/><foo/>jh<b:x/>g</foo>", 0)


	def test13element(self):
		schema = self.schemaParser("""<?xml version="1.0" encoding="utf-8"?>
		<element xmlns="http://relaxng.org/ns/structure/1.0" name="date"
			xmlns:if='http://namespaces.xmlschemata.org/xvif/iframe'>
  		<choice>
 				<if:transform type='http://namespaces.xmlschemata.org/xvif/regexp' 
					apply='s/ /-/'/>
     			<value>2002-06-07</value>
     			<value>2002-06-09</value>
  		</choice>
		</element>""")
		self.validateInstance(schema, "<date>x y</date>", 0)
		self.validateInstance(schema, "<date>2002-06-07</date>", 1)
		self.validateInstance(schema, "<date>2002 06-07</date>", 1)
		self.validateInstance(schema, "<date>2002 06 07</date>", 1)
		self.validateInstance(schema, "<date>2002-06 07</date>", 1)

	def test14element(self):
		schema = self.schemaParser("""<?xml version="1.0" encoding="utf-8"?>
		<element xmlns="http://relaxng.org/ns/structure/1.0" name="date"
			xmlns:if='http://namespaces.xmlschemata.org/xvif/iframe'>
  		<choice>
 				<if:transform type='http://namespaces.xmlschemata.org/xvif/regexp' 
					apply='s/(\d\d)-(\d\d)-(\d\d\d\d)/\\3-\\2-\\1/'/>
     			<value>2002-06-07</value>
     			<value>2002-06-09</value>
  		</choice>
		</element>""")
		self.validateInstance(schema, "<date>x y</date>", 0)
		self.validateInstance(schema, "<date>07-06-2002</date>", 1)
		self.validateInstance(schema, "<date>09-06-2002</date>", 1)
		self.validateInstance(schema, "<date>08-06-2002</date>", 0)
		self.validateInstance(schema, "<date>2002-06-09</date>", 1)


	def test15element(self):
		schema = self.schemaParser("""<?xml version="1.0" encoding="utf-8"?>
		<element xmlns="http://relaxng.org/ns/structure/1.0" name="date"
			xmlns:if='http://namespaces.xmlschemata.org/xvif/iframe'>
  		<choice>
 				<if:transform type='http://namespaces.xmlschemata.org/xvif/regexp' 
					apply='s/(\d\d)\/(\d\d)\/(\d\d\d\d)/\\3-\\2-\\1/'/>
     			<value>2002-06-07</value>
     			<value>2002-06-09</value>
  		</choice>
		</element>""")
		self.validateInstance(schema, "<date>x y</date>", 0)
		self.validateInstance(schema, "<date>07/06/2002</date>", 1)
		self.validateInstance(schema, "<date>09/06/2002</date>", 1)
		self.validateInstance(schema, "<date>09-06-2002</date>", 0)
		self.validateInstance(schema, "<date>08/06/2002</date>", 0)
		self.validateInstance(schema, "<date>2002-06-09</date>", 1)


	def test16element(self):
		schema = self.schemaParser("""<element name="date" xmlns="http://relaxng.org/ns/structure/1.0" xmlns:rng="http://relaxng.org/ns/structure/1.0">
  <if:transform type="http://www.w3.org/1999/XSL/Transform" return="outer"
		xmlns:if='http://namespaces.xmlschemata.org/xvif/iframe'>
    <if:apply>
     <xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
      <xsl:template match="date">
			 <xsl:copy>
        <year>
         <xsl:value-of select="substring-before(.,'-')"/>
        </year>
        <month>
         <xsl:value-of select="substring-before(substring-after(., '-'),'-')"/>
        </month>
        <day>
         <xsl:value-of select="substring-after(substring-after(., '-'),'-')"/>
        </day>
			 </xsl:copy>
      </xsl:template>
     </xsl:transform>
    </if:apply>
  </if:transform>
   <element name="rng:year">
     <text/>
   </element>
   <element name="rng:month">
    <text/>
   </element>
   <element name="rng:day">
    <text/>
   </element>
</element>""")
		self.validateInstance(schema, "<date>2002-06-09</date>", 1)

	def test17element(self):
		schema = self.schemaParser("""<element name="date" xmlns="http://relaxng.org/ns/structure/1.0" xmlns:rng="http://relaxng.org/ns/structure/1.0">
  <if:transform type="http://www.w3.org/1999/XSL/Transform" return="inner"
		xmlns:if='http://namespaces.xmlschemata.org/xvif/iframe'>
    <if:apply>
     <xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
      <xsl:template match="date">
        <year>
         <xsl:value-of select="substring-before(.,'-')"/>
        </year>
        <month>
         <xsl:value-of select="substring-before(substring-after(., '-'),'-')"/>
        </month>
        <day>
         <xsl:value-of select="substring-after(substring-after(., '-'),'-')"/>
        </day>
      </xsl:template>
     </xsl:transform>
    </if:apply>
  </if:transform>
   <element name="rng:year">
     <text/>
   </element>
   <element name="rng:month">
    <text/>
   </element>
   <element name="rng:day">
    <text/>
   </element>
</element>""")
		self.validateInstance(schema, "<date>2002-06-09</date>", 1)


	def test18element(self):
		schema = self.schemaParser("""<element name="date" xmlns="http://relaxng.org/ns/structure/1.0">
  <if:transform type="http://www.w3.org/1999/XSL/Transform" return="outer"
		xmlns:if='http://namespaces.xmlschemata.org/xvif/iframe'>
    <if:apply>
     <xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0" xmlns=''>
      <xsl:template match="date">
			 <xsl:copy>
        <year>
         <xsl:value-of select="substring-before(.,'-')"/>
        </year>
        <month>
         <xsl:value-of select="substring-before(substring-after(., '-'),'-')"/>
        </month>
        <day>
         <xsl:value-of select="substring-after(substring-after(., '-'),'-')"/>
        </day>
			 </xsl:copy>
      </xsl:template>
     </xsl:transform>
    </if:apply>
  </if:transform>
   <element name="year">
     <text/>
   </element>
   <element name="month">
    <text/>
   </element>
   <element name="day">
    <text/>
   </element>
</element>""")
		self.validateInstance(schema, "<date>2002-06-09</date>", 1)


	def test19element(self):
		schema = self.schemaParser("""<rng:element name="date" xmlns:rng="http://relaxng.org/ns/structure/1.0">
  <if:transform type="http://www.w3.org/1999/XSL/Transform" return="inner"
		xmlns:if='http://namespaces.xmlschemata.org/xvif/iframe'>
    <if:apply>
     <xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
      <xsl:template match="date[contains(., '-') and contains(substring-after(., '-'), '-')]">
       <year>
        <xsl:value-of select="substring-before(.,'-')"/>
       </year>
       <month>
        <xsl:value-of select="substring-before(substring-after(., '-'),'-')"/>
       </month>
       <day>
        <xsl:value-of select="substring-after(substring-after(., '-'),'-')"/>
       </day>
      </xsl:template>
     </xsl:transform>
    </if:apply>
  </if:transform>
   <rng:element name="year">
     <rng:text/>
   </rng:element>
   <rng:element name="month">
		<rng:choice>
			<rng:value>01</rng:value>
			<rng:value>02</rng:value>
			<rng:value>03</rng:value>
			<rng:value>04</rng:value>
			<rng:value>05</rng:value>
			<rng:value>06</rng:value>
			<rng:value>07</rng:value>
			<rng:value>08</rng:value>
			<rng:value>09</rng:value>
			<rng:value>10</rng:value>
			<rng:value>11</rng:value>
			<rng:value>12</rng:value>
		</rng:choice>
   </rng:element>
   <rng:element name="day">
    <rng:text/>
   </rng:element>
</rng:element>""")
		self.validateInstance(schema, "<date>2002-06-09</date>", 1)
		self.validateInstance(schema, "<date>--</date>", 0)
		self.validateInstance(schema, "<date>-06-</date>", 1)
		self.validateInstance(schema, "<date>20020609</date>", 0)
		self.validateInstance(schema, "<date>2002-0609</date>", 0)


	def test20(self):
		schema = self.schemaParser("""
	<element name="date" xmlns="http://relaxng.org/ns/structure/1.0">
  <if:transform type="http://simonstl.com/ns/fragments/"
    xmlns:if='http://namespaces.xmlschemata.org/xvif/iframe'>
    <if:apply>
      <fragmentRules xmlns="http://simonstl.com/ns/fragments/">
        <fragmentRule pattern="-" split="true" skipFirst="false">
          <applyTo>
            <element localName="date"/>
          </applyTo>
          <produce>
            <element localName="year"/>
            <element localName="month"/>
            <element localName="day"/>
          </produce>
        </fragmentRule>
      </fragmentRules>
    </if:apply>
  </if:transform>
   <element name="year">
     <text/>
   </element>
   <element name="month">
    <text/>
   </element>
   <element name="day">
    <text/>
   </element>
</element>""")
		self.validateInstance(schema, "<date>2002-06-09</date>", 1)
		self.validateInstance(schema, "<date>--</date>", 1)
		self.validateInstance(schema, "<date>200206-09</date>", 0)

	def test21(self):
		schema = self.schemaParser("""
	<element name="root" xmlns="http://relaxng.org/ns/structure/1.0">
  <if:validate type="http://www.w3.org/TR/1999/REC-xpath-19991116"
    xmlns:if='http://namespaces.xmlschemata.org/xvif/iframe'
		apply="foo/@id = bar/@idref"/>
   <element name="foo">
	 	<attribute name="id"/>
   </element>
   <element name="bar">
	 	<attribute name="idref"/>
   </element>
</element>""")
		self.validateInstance(schema, "<root><foo id='1'/><bar idref='1'/></root>", 1)
		self.validateInstance(schema, "<root><foo id='1'/><bar/></root>", 0)
		self.validateInstance(schema, "<root><foo id='1'/><bar idref='2'/></root>", 0)
		self.validateInstance(schema, "<root><foo/><bar idref='2'/></root>", 0)

	def test22(self):
		schema = self.schemaParser("""
	<element name="root" xmlns="http://relaxng.org/ns/structure/1.0">
	<text>
 	  <if:validate type='http://namespaces.xmlschemata.org/xvif/regexp' 
    xmlns:if='http://namespaces.xmlschemata.org/xvif/iframe'
		apply="\d\d\d\d-\d\d-\d\d"/>
	</text>
</element>""")
		self.validateInstance(schema, "<root>2002-04-12</root>", 1)
		self.validateInstance(schema, "<root>2002-0-12</root>", 0)
		self.validateInstance(schema, "<root>12-04-2000</root>", 0)

	def test23(self):
		schema = self.schemaParser("""
	<element name="date" xmlns="http://relaxng.org/ns/structure/1.0">
	<if:pipe xmlns:if='http://namespaces.xmlschemata.org/xvif/iframe'>
   <if:transform type="http://simonstl.com/ns/fragments/" xmlns:if='http://namespaces.xmlschemata.org/xvif/iframe' >
    <if:apply>
      <fragmentRules xmlns="http://simonstl.com/ns/fragments/">
        <fragmentRule pattern="(\d\d\d\d)-(\d\d)-(\d\d)" >
          <applyTo>
            <element localName="date"/>
          </applyTo>
          <produce>
            <element localName="year"/>
            <element localName="month"/>
            <element localName="day"/>
          </produce>
        </fragmentRule>
      </fragmentRules>
    </if:apply>
   </if:transform>
   <if:validate type="http://www.w3.org/TR/1999/REC-xpath-19991116"
		apply="month > 0 and month &lt; 13"/>
	 </if:pipe> 
   <element name="year">
     <text/>
   </element>
   <element name="month">
    <text/>
   </element>
   <element name="day">
    <text/>
   </element>
</element>""")
		self.validateInstance(schema, "<date>2002-06-09</date>", 1)
		self.validateInstance(schema, "<date>2002-14-09</date>", 0)
		self.validateInstance(schema, "<date>--</date>", 0)
		self.validateInstance(schema, "<date>200206-09</date>", 0)
	
	def test24(self):
		schema = self.schemaParser("""<?xml version="1.0" encoding="utf-8"?>
<element xmlns="http://relaxng.org/ns/structure/1.0" name="foo">
 <list>
  <if:transform xmlns:if='http://namespaces.xmlschemata.org/xvif/iframe'
   type='http://www.w3.org/TR/1999/REC-xpath-19991116' 
   apply='translate(., ",", " ")'/>
  <oneOrMore>
   <choice>
    <value>foo</value>
    <value>bar</value>
   </choice>
  </oneOrMore>
 </list>
</element>
	""")
		self.validateInstance(schema, "<foo><whatever>foo, bar <x>f</x>oo  foo</whatever></foo>", 1)
		self.validateInstance(schema, "<foo>foo, <whatever>bar <x>f</x>oo  foo</whatever></foo>", 1)
		self.validateInstance(schema, "<foo>foo, b<whatever>ar <x>f</x>oo  foo</whatever></foo>", 0)
		self.validateInstance(schema, "<foo>foo bar foo</foo>", 1)
		self.validateInstance(schema, "<foo>foo, bar, foo</foo>", 1)

	def test25(self):
		schema = self.schemaParser("""<?xml version="1.0" encoding="utf-8"?>
<element xmlns="http://relaxng.org/ns/structure/1.0" name="foo">
 <list>
  <if:pipe xmlns:if='http://namespaces.xmlschemata.org/xvif/iframe'>
   <if:validate 
    type='http://www.w3.org/TR/1999/REC-xpath-19991116' 
    apply='not(*)'/>
   <if:transform 
    type='http://www.w3.org/TR/1999/REC-xpath-19991116' 
    apply='translate(., ",", " ")'/>
  </if:pipe>
  <oneOrMore>
   <choice>
    <value>foo</value>
    <value>bar</value>
   </choice>
  </oneOrMore>
 </list>
</element>
	""")
		self.validateInstance(schema, "<foo><whatever>foo, bar <x>f</x>oo  foo</whatever></foo>", 0)
		self.validateInstance(schema, "<foo>foo, <whatever>bar <x>f</x>oo  foo</whatever></foo>", 0)
		self.validateInstance(schema, "<foo>foo, b<whatever>ar <x>f</x>oo  foo</whatever></foo>", 0)
		self.validateInstance(schema, "<foo>foo bar foo</foo>", 1)
		self.validateInstance(schema, "<foo>foo, bar, foo</foo>", 1)

	def test26(self):
		schema = self.schemaParser("""<?xml version="1.0" encoding="utf-8"?>
<element xmlns="http://relaxng.org/ns/structure/1.0" name="foo">
 <list>
  <if:transform xmlns:if='http://namespaces.xmlschemata.org/xvif/iframe'
    type='http://namespaces.xmlschemata.org/xvif/regexp' 
    apply='s/,/ /'/>
  <oneOrMore>
   <choice>
    <value>foo</value>
    <value>bar</value>
   </choice>
  </oneOrMore>
 </list>
</element>
	""")
		self.validateInstance(schema, "<foo><whatever>foo, bar <x>f</x>oo  foo</whatever></foo>", 0)
		self.validateInstance(schema, "<foo>foo, <whatever>bar <x>f</x>oo  foo</whatever></foo>", 0)
		self.validateInstance(schema, "<foo>foo, b<whatever>ar <x>f</x>oo  foo</whatever></foo>", 0)
		self.validateInstance(schema, "<foo>foo bar foo</foo>", 1)
		self.validateInstance(schema, "<foo>foo, bar, foo</foo>", 1)
	
	def test27(self):
		schema = self.schemaParser("""<?xml version="1.0" encoding="utf-8"?>
<element xmlns="http://relaxng.org/ns/structure/1.0" name="foo">
 <if:transform type="http://simonstl.com/ns/fragments/"
  xmlns:if='http://namespaces.xmlschemata.org/xvif/iframe'>
  <if:apply>
   <fragmentRules xmlns="http://simonstl.com/ns/fragments/">
    <fragmentRule pattern="," split="true" skipFirst="false" repeat="true">
     <applyTo>
      <element localName="foo"/>
     </applyTo>
     <produce>
      <element localName="item"/>
     </produce>
    </fragmentRule>
   </fragmentRules>
  </if:apply>
 </if:transform>
 <oneOrMore>
  <element name="item">
   <choice>
    <value>foo</value>
    <value>bar</value>
   </choice>
  </element>
 </oneOrMore>
</element>
	""")
		self.validateInstance(schema, "<foo><whatever>foo, bar <x>f</x>oo  foo</whatever></foo>", 0)
		self.validateInstance(schema, "<foo>foo, <whatever>bar <x>f</x>oo  foo</whatever></foo>", 0)
		self.validateInstance(schema, "<foo>foo, b<whatever>ar <x>f</x>oo  foo</whatever></foo>", 0)
		self.validateInstance(schema, "<foo>foo bar foo</foo>", 0)
		self.validateInstance(schema, "<foo>foo, bar, foo</foo>", 1)
		self.validateInstance(schema, "<foo><item>foo</item><item>bar</item><item>foo</item></foo>", 0)

	def test28(self):
		schema = self.schemaParser("""<?xml version="1.0" encoding="utf-8"?>
<element xmlns="http://relaxng.org/ns/structure/1.0" name="foo">
 <if:transform type="http://simonstl.com/ns/fragments/"
  xmlns:if='http://namespaces.xmlschemata.org/xvif/iframe'>
  <if:apply>
   <fragmentRules xmlns="http://simonstl.com/ns/fragments/">
    <fragmentRule pattern="," split="true" repeat="true">
     <applyTo>
      <attribute localName="bar"/>
     </applyTo>
     <produce>
      <element localName="item"/>
     </produce>
    </fragmentRule>
   </fragmentRules>
  </if:apply>
 </if:transform>
 <oneOrMore>
  <element name="item">
   <choice>
    <value>foo</value>
    <value>bar</value>
   </choice>
  </element>
 </oneOrMore>
</element>
	""")
		self.validateInstance(schema, "<foo bar='foo, bar foo  foo'/>", 0)
		self.validateInstance(schema, "<foo bar='foo, bar, foo,  foo'/>", 1)
		self.validateInstance(schema, "<foo bar='foor, bar, foo,  foo'/>", 0)
		self.validateInstance(schema, "<foo><item>foo</item><item>bar</item><item>foo</item></foo>", 1)

	def test29(self):
		schema = self.schemaParser("""<?xml version="1.0" encoding="utf-8"?>
<element xmlns="http://relaxng.org/ns/structure/1.0" name="foo">
 <if:pipe xmlns:if='http://namespaces.xmlschemata.org/xvif/iframe'>
  <if:validate type='http://www.w3.org/TR/1999/REC-xpath-19991116' 
    apply='not(item)'/>
  <if:transform type="http://simonstl.com/ns/fragments/">
   <if:apply>
    <fragmentRules xmlns="http://simonstl.com/ns/fragments/">
     <fragmentRule pattern="," split="true" repeat="true">
      <applyTo>
       <attribute localName="bar"/>
      </applyTo>
      <produce>
       <element localName="item"/>
      </produce>
     </fragmentRule>
    </fragmentRules>
   </if:apply>
  </if:transform>
 </if:pipe>
 <oneOrMore>
  <element name="item">
   <choice>
    <value>foo</value>
    <value>bar</value>
   </choice>
  </element>
 </oneOrMore>
</element>
	""")
		self.validateInstance(schema, "<foo bar='foo, bar foo  foo'/>", 0)
		self.validateInstance(schema, "<foo bar='foo, bar, foo,  foo'/>", 1)
		self.validateInstance(schema, "<foo bar='foor, bar, foo,  foo'/>", 0)
		self.validateInstance(schema, "<foo><item>foo</item><item>bar</item><item>foo</item></foo>", 0)

	def test30(self):
		schema = self.schemaParser("""<?xml version="1.0" encoding="utf-8"?>
<element xmlns="http://relaxng.org/ns/structure/1.0" name="foo">
 <if:transform type="http://simonstl.com/ns/fragments/"
  xmlns:if='http://namespaces.xmlschemata.org/xvif/iframe'>
  <if:apply>
   <fragmentRules xmlns="http://simonstl.com/ns/fragments/">
    <fragmentRule pattern="," split="true" repeat="true" break="true">
     <applyTo>
      <attribute localName="bar"/>
     </applyTo>
     <produce>
      <element localName="item"/>
     </produce>
    </fragmentRule>
    <fragmentRule pattern="(.*)">
     <applyTo>
      <element localName="item"/>
     </applyTo>
     <produce>
      <element localName="forbidden"/>
     </produce>
    </fragmentRule>
   </fragmentRules>
  </if:apply>
 </if:transform>
 <oneOrMore>
  <element name="item">
   <choice>
    <value>foo</value>
    <value>bar</value>
   </choice>
  </element>
 </oneOrMore>
</element>
	""")
		self.validateInstance(schema, "<foo bar='foo, bar foo  foo'/>", 0)
		self.validateInstance(schema, "<foo bar='foo, bar, foo,  foo'/>", 1)
		self.validateInstance(schema, "<foo bar='foor, bar, foo,  foo'/>", 0)
		self.validateInstance(schema, "<foo><item>foo</item><item>bar</item><item>foo</item></foo>", 0)


	def test31(self):
		schema = self.schemaParser("""<?xml version="1.0" encoding="iso-8859-1"?>
<element xmlns="http://relaxng.org/ns/structure/1.0" name="foo">
 <choice>
  <if:pipe xmlns:if='http://namespaces.xmlschemata.org/xvif/iframe'>
   <if:transform type='http://namespaces.xmlschemata.org/xvif/regexp'
    apply="s/(\d+) janvier (\d+)/\\2-01-\\1/"/>
   <if:transform type='http://namespaces.xmlschemata.org/xvif/regexp'
    apply="s/(\d+) février (\d+)/\\2-02-\\1/"/>
   <if:transform type='http://namespaces.xmlschemata.org/xvif/regexp'
    apply="s/(\d+) mars (\d+)/\\2-03-\\1/"/>
   <if:transform type='http://namespaces.xmlschemata.org/xvif/regexp'
    apply="s/(\d+) avril (\d+)/\\2-04-\\1/"/>
   <if:transform type='http://namespaces.xmlschemata.org/xvif/regexp'
    apply="s/(\d+) mai (\d+)/\\2-05-\\1/"/>
   <if:transform type='http://namespaces.xmlschemata.org/xvif/regexp'
    apply="s/(\d+) juin (\d+)/\\2-06-\\1/"/>
   <if:transform type='http://namespaces.xmlschemata.org/xvif/regexp'
    apply="s/(\d+) juillet (\d+)/\\2-07-\\1/"/>
   <if:transform type='http://namespaces.xmlschemata.org/xvif/regexp'
    apply="s/(\d+) août (\d+)/\\2-08-\\1/"/>
   <if:transform type='http://namespaces.xmlschemata.org/xvif/regexp'
    apply="s/(\d+) septembre (\d+)/\\2-09-\\1/"/>
   <if:transform type='http://namespaces.xmlschemata.org/xvif/regexp'
    apply="s/(\d+) octobre (\d+)/\\2-10-\\1/"/>
   <if:transform type='http://namespaces.xmlschemata.org/xvif/regexp'
    apply="s/(\d+) novembre (\d+)/\\2-11-\\1/"/>
   <if:transform type='http://namespaces.xmlschemata.org/xvif/regexp'
    apply="s/(\d+) décembre (\d+)/\\2-12-\\1/"/>
  </if:pipe>
  <value>2002-02-03</value>
  <value>1970-01-01</value>
 </choice>
</element>
""")
		self.validateInstance(schema, "<?xml version='1.0' encoding='iso-8859-1'?><foo>04 février 2002</foo>", 0)
		self.validateInstance(schema, "<?xml version='1.0' encoding='iso-8859-1'?><foo>03 février 2002</foo>", 1)
		self.validateInstance(schema, "<?xml version='1.0' encoding='iso-8859-1'?><foo>01 janvier 1970</foo>", 1)
		self.validateInstance(schema, "<?xml version='1.0' encoding='iso-8859-1'?><foo>2002-02-03</foo>", 1)


	def test32(self):
		schema = self.schemaParser("""<?xml version="1.0" encoding="iso-8859-1"?>
<element xmlns="http://relaxng.org/ns/structure/1.0" name="foo">
  <if:transform type='http://www.w3.org/1999/XSL/Transform'
     xmlns:if='http://namespaces.xmlschemata.org/xvif/iframe'>
   <if:apply xmlns="">
    <xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
      xmlns:vdv="http://eric.van-der-vlist.com/tmpns" version="1.0">
     <vdv:dates>
      <month name="janvier"/>
      <month name="février"/>
      <month name="mars"/>
      <month name="avril"/>
      <month name="mai"/>
      <month name="juin"/>
      <month name="juillet"/>
      <month name="août"/>
      <month name="septembre"/>
      <month name="octobre"/>
      <month name="novembre"/>
      <month name="décembre"/>
     </vdv:dates>
     <xsl:template match="*|@*">
      <xsl:copy>
       <xsl:apply-templates select="@*|*|text()"/>
      </xsl:copy>
     </xsl:template>
     <xsl:template match="foo/text()">
      <!-- year -->
      <xsl:value-of select="format-number(substring-after(substring-after(., ' '),' '), '0000')"/>
      <xsl:text>-</xsl:text>
      <!-- month -->
      <xsl:apply-templates select="document('')/xsl:transform/vdv:dates/month[@name=substring-before(substring-after(current(), ' '),' ')]" mode="month"/>
      <xsl:text>-</xsl:text>
      <!-- day -->
      <xsl:value-of select="format-number(substring-before(.,' '), '00')"/>
     </xsl:template>
     <xsl:template match="month" mode="month">
      <xsl:value-of select="format-number(count(preceding-sibling::month)+1, '00')"/>
     </xsl:template>
    </xsl:transform>
   </if:apply>
  </if:transform>
 <choice>
  <value>2002-02-03</value>
  <value>1970-01-01</value>
 </choice>
</element>
		""")
		self.validateInstance(schema, "<?xml version='1.0' encoding='iso-8859-1'?><foo>04 juin 2002</foo>", 0)
		self.validateInstance(schema, "<?xml version='1.0' encoding='iso-8859-1'?><foo>03 février 2002</foo>", 1)
		self.validateInstance(schema, "<?xml version='1.0' encoding='iso-8859-1'?><foo>01 janvier 1970</foo>", 1)
		self.validateInstance(schema, "<?xml version='1.0' encoding='iso-8859-1'?><foo>01 janvier 1971</foo>", 0)
		self.validateInstance(schema, "<foo></foo>", 0)
		#self.validateInstance(schema, "<foo>foo</foo>", 0)




if __name__ == "__main__":
	unittest.main()


