def test_name_start(self): """Productions:: [4] NameStartChar ::= ":" | [A-Z] | "_" | [a-z] | [#xC0-#xD6] | [#xD8-#xF6] | [#xF8-#x2FF] | [#x370-#x37D] | [#x37F-#x1FFF] | [#x200C-#x200D] | [#x2070-#x218F] | [#x2C00-#x2FEF] | [#x3001-#xD7FF] | [#xF900-#xFDCF] | [#xFDF0-#xFFFD] | [#x10000-#xEFFFF] [5] NameChar ::= NameStartChar | "-" | "." | [0-9] | #xB7 | [#x0300-#x036F] | [#x203F-#x2040]""" n_namestartchars = 0 n_namechars = 0 for code in range3(0x10000): c = character(code) if structures.is_name_char(c): n_namechars += 1 if structures.is_name_start_char(c): n_namestartchars += 1 else: self.assertFalse(structures.is_name_start_char(c), "NameStart not a name char: %s" % c) self.assertTrue(n_namechars == 54129, "name char total %i" % n_namechars) self.assertTrue(n_namestartchars == 54002, "name start char total %i" % n_namestartchars)
def ValidateIdentifier(value, prefix='_'): """Decodes an identifier from a string:: <xsd:simpleType name="identifier.Type"> <xsd:restriction base="xsd:NCName"/> </xsd:simpleType> This function takes a string that is supposed to match the production for NCName in XML and forces it to comply by replacing illegal characters with '_', except the ':' which is replaced with a hyphen for compatibility with previous versions of the QTI migraiton script. If name starts with a valid name character but not a valid name start character, it is prefixed with '_' too, but the prefix string used can be overridden.""" if value: goodName = [] if not xml.is_name_start_char(value[0]): goodName.append(prefix) elif value[0] == ':': # Previous versions of the migrate script didn't catch this problem # as a result, we deviate from its broken behaviour of using '-' # by using the prefix too. goodName.append(prefix) for c in value: if c == ':': goodName.append('-') elif xml.is_name_char(c): goodName.append(c) else: goodName.append('_') return string.join(goodName, '') else: return prefix
def MakeValidName(name): """This function takes a string that is supposed to match the production for Name in XML and forces it to comply by replacing illegal characters with '_'. If name starts with a valid name character but not a valid name start character, it is prefixed with '_' too.""" if name: goodName = [] if not xml.is_name_start_char(name[0]): goodName.append(u'_') for c in name: if xml.is_name_char(c): goodName.append(c) else: goodName.append(u'_') return string.join(goodName, u'') else: return u'_'