Next / Previous / Contents / Shipman's homepage

35. Sox.pi(): Write a processing instruction

sox.py
# - - -   S o x . p i

    def pi(self, target, args):
        '''Write an XML processing instruction.
        '''
        #-- 1
        # [ if (target is a valid xml-name) and
        #   (target and args are unicode or can be coerced to
        #   unicode) ->
        #     uTarget  :=  target as unicode
        #     uArgs  :=  args as unicode
        #   else -> raise SoxError ]
        uTarget = self._nameCheck(target)
        uArgs = self._unicodify(args)

Aside from the constraint that both parts must be Unicode and the target must a valid XML name, the standard also stipulates that the arguments part may not contain the string “?>”.

sox.py
        #-- 2
        if u'?>' in uArgs:
            raise SoxError("PI arguments %r: May not contain "
                "'?>'." % uArgs)

        #-- 3
        self.outFile.write("<?%s %s?>" %
            (uTarget.encode('utf-8'), uArgs.encode('utf-8')))