2017-02-02 2 views
0

を使用している場合が見つかりませんノード:。XMLSlurper:私は検証と名前空間意識することなくXMLSlurperを作成するとXmlSlurper(、偽偽)

new XmlSlurper(false, false) 

私はその後(node.depthFirstを持つ任意のノードを見つけることができません)のfindAll()。

次のコードは、私の問題を示しています

def xml = '''<?xml version="1.0" encoding="UTF-8"?> 
<cus:Customizations xmlns:cus="http://www.bea.com/wli/config/customizations" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xt="http://www.bea.com/wli/config/xmltypes"> 
    <cus:customization xsi:type="cus:EnvValueActionsCustomizationType"> 
     <cus:description/> 
     <cus:actions> 
      <xt:replace> 
       <xt:envValueType>Service URI</xt:envValueType> 
       <xt:location>0</xt:location> 
       <xt:value xsi:type="xs:string" xmlns:xs="http://www.w3.org/2001/XMLSchema">http://myUrl.com</xt:value> 
      </xt:replace> 
     </cus:actions> 
    </cus:customization> 
</cus:Customizations>''' 

/* The following code finds no nodes. */ 
def envelope1 = new XmlSlurper(false, false).parseText(xml) 
def replaces1 = envelope1.depthFirst().findAll { node -> 
    (node.name() == "replace") 
} 
assert replaces1.size() == 0 

/* The following code finds one node (works as I expect). */ 
def envelope2 = new XmlSlurper().parseText(xml) 
def replaces2 = envelope2.depthFirst().findAll { node -> 
    (node.name() == "replace") 
} 
assert replaces2.size() == 1 

は、それが既知のバグですか、私は何かが足りないのですか?

+1

XMLネームスペースのサポートを提供しないようにパーサに依頼する場合、findAll内のロジックは '(node.name()==" xt:replace ")'であるべきではありません。そうです。 – dmahapatro

+0

はい、「xt:replace」を検索すると、そのノードが見つかります。私は "namespaceAware"が何を意味するのかをはっきりと理解していませんでした。私はそれが名前空間を無視することを意味すると仮定した。私が知る限り、コロンは名前空間の割り当てのために予約されているため、要素名にコロンを含めることはできません。しかし、ここでは "xt:"接頭辞は名前の一部として扱われます。 XmlSlurperの動作が混乱しているようです。また、一般的な名前空間接頭辞が変更された場合、プログラムは動作しなくなります。 – KarelHusa

+1

なぜ、 'def envelope1 = new XmlSlurper(false、true).parseText(xml)'を使用しないと、検証は無効になりますが、名前空間を認識しています... –

答えて

0

XmlSlurperのプロパティnamespaceAwareがfalseに設定されている場合、我々は要素を探したときに、私たちはすなわち、要素名の一部として、名前空間接頭辞を処理する必要があります。

(node.name() == "xt:replace") 

XmlSlurperのこの動作はありません私にとって正しいとは思われませんが、それは動作する方法です。ダブルコロン(:)は、XML要素名の一部にすることはできず、名前空間の使用のために予約されています。私の結論は、私は将来、trueに設定されたnamespaceAwareを使用するつもりです。

ありがとう@dhamapatro。

関連する問題