2012-05-08 2 views
1

Pythonコードとして出力するPython &で解析しているXMLファイルがあります。PythonでXMLを解析する際に特殊文字を保持していますか?

一部のXMLにはReg Ex値と文字列が含まれています。この値は画面上にダイアログとして表示されるため、維持する必要がある特殊文字がいくつかあります。コードは次のとおりですが、どのようにこれを行うことができますか?

XMLはこのように少し見えます。

<variable id="passportnumber" value="" type="String"> 
    <validate> 
     <regularExpression fieldID="passportnumber" errorID="3007162"><![CDATA[^[a-zA-Z+:?<>;*()%="!0-9./',&\s-]{1,35}$]]></regularExpression> 
    </validate> 
</variable> 

ダイアログの場合。

<if> 
    <condition><![CDATA[$taxcode$ == $previousemergencytaxcode$ and $previousemergencytaxcode$ != $emergencytaxcode$]]></condition> 
    <then> 
     <dialog id="taxCodeOutdatedDialog" text="Are you sure this is the correct tax 
     code? &#10; &#10;The emergency code for the tax year 2011-12 was 
     '$previousemergencytaxcode$'. &#10;The emergency code for the tax 
     year 2012-13 is '$emergencytaxcode$'. &#10; &#10;Proceed?" type="YES|NO|CANCEL" /> 
    </then> 
</if> 

フルPythonスクリプトはhereで、具体的には、これらの二つである解析します。

def parse_regularExpression(self, elem): 
    self.out('') 
    self.out("item_regularExpression(fieldID='{0}', value='{1}')".format(elem.attrib['fieldID'],elem.text)) 

def parse_dialog(self, elem): 
    self.out('') 
    self.out("item_dialog(id='{0}', text='{1}', type='{2}')".format(elem.attrib['id'], elem.attrib['text'],elem.attrib['type'])) 

ラインフィード(&#10;)は、私が対処する方法がわからないんだ主なものです。それは、たとえそれが三重引用符であっても、改行としてそれを出力しているようです。テキスト値を次のように出力します。

item_dialog(id='taxCodeOutdatedDialog', text='Are you sure this is the correct tax code? 

The emergency code for the tax year 2011-12 was '$previousemergencytaxcode$'. 
The emergency code for the tax year 2012-13 is '$emergencytaxcode$'. 

Proceed?', type='YES|NO|CANCEL') 

答えて

1

これはあなたの言うとおりです。 XMLには、改行と思われる&#10が含まれていると思います。次に、その文字列を印刷しています。

改行を改行したい場合は、読んだ後で出力する前に改行してください。 (それをXMLで変更しようとするよりもむしろ)。

あなたのコードはこのような何かを探していることになります:あなたの問題は、基本的にスクリプトインジェクションの問題/脆弱性であるよう

def parse_dialog(self, elem): 
    self.out('') 
    self.out("item_dialog(id='{0}', text='{1}', type='{2}')".format(
     escape_string(elem.attrib['id']), 
     escape_string(elem.attrib['text']), 
     escape_string(elem.attrib['type']))) 

def escape_string(s): 
    ... 

これはあまりにもはるかに堅牢です。

+0

はい!時には私はあまりにも多くの考えを与えたときに明白なことを指摘する必要があります!ありがとう。ちょうど今すぐ値をサブする正規表現を把握する必要があります:) –

関連する問題