3434from .Nodes import StreamStartNode
3535from xml .sax .saxutils import escape as xml_sax_escape
3636
37+
3738class UnexpectedElementException (Exception ):
3839 def __init__ (self , msg ):
3940 super (UnexpectedElementException , self ).__init__ (msg )
4041
4142
43+ def to_xml_string (s ):
44+ #s = xml_sax_escape(s, {'"': '"'})
45+ s = s .encode ("ascii" , "xmlcharrefreplace" ).decode ('ascii' )
46+ return s
47+
48+
4249def _make_template_xml_view (root_node , cache = None ):
4350 """
4451 Given a RootNode, parse only the template/children
@@ -66,15 +73,15 @@ def rec(node, acc):
6673 for child in node .children ():
6774 if isinstance (child , AttributeNode ):
6875 acc .append (" " )
69- acc .append (child .attribute_name ().string ())
76+ acc .append (to_xml_string ( child .attribute_name ().string () ))
7077 acc .append ("=\" " )
7178 rec (child .attribute_value (), acc )
7279 acc .append ("\" " )
7380 acc .append (">" )
7481 for child in node .children ():
7582 rec (child , acc )
7683 acc .append ("</" )
77- acc .append (node .tag_name ())
84+ acc .append (to_xml_string ( node .tag_name () ))
7885 acc .append (">\n " )
7986 elif isinstance (node , CloseStartElementNode ):
8087 pass # intended
@@ -88,14 +95,14 @@ def rec(node, acc):
8895 pass # intended
8996 elif isinstance (node , CDataSectionNode ):
9097 acc .append ("<![CDATA[" )
91- acc .append (node .cdata ())
98+ acc .append (to_xml_string ( node .cdata () ))
9299 acc .append ("]]>" )
93100 elif isinstance (node , EntityReferenceNode ):
94- acc .append (node .entity_reference ())
101+ acc .append (to_xml_string ( node .entity_reference () ))
95102 elif isinstance (node , ProcessingInstructionTargetNode ):
96- acc .append (node .processing_instruction_target ())
103+ acc .append (to_xml_string ( node .processing_instruction_target () ))
97104 elif isinstance (node , ProcessingInstructionDataNode ):
98- acc .append (node .string ())
105+ acc .append (to_xml_string ( node .string () ))
99106 elif isinstance (node , TemplateInstanceNode ):
100107 raise UnexpectedElementException ("TemplateInstanceNode" )
101108 elif isinstance (node , NormalSubstitutionNode ):
@@ -127,11 +134,6 @@ def rec(node, acc):
127134 return "" .join (acc )
128135
129136
130- def to_xml_string (s ):
131- s = xml_sax_escape (s , {'"' : '"' })
132- s = s .encode ("ascii" , "xmlcharrefreplace" ).decode ('ascii' )
133- return s
134-
135137
136138def _build_record_xml (record , cache = None ):
137139 """
@@ -150,7 +152,8 @@ def rec(root_node):
150152 subs_strs = []
151153 for sub in root_node .fast_substitutions ():
152154 if isinstance (sub , str ):
153- subs_strs .append (to_xml_string (sub ))
155+ #subs_strs.append(to_xml_string(sub))
156+ subs_strs .append (sub )
154157 elif isinstance (sub , RootNode ):
155158 subs_strs .append (rec (sub ))
156159 elif sub is None :
0 commit comments