Get all elements from a specific namespace in an XML document

I need to get an element of an XML file using a prefix. My XML file looks like this:

<dependency:COP xmi:id="5299" sofa="6" begin="291" end="294" Governor="4906" Dependent="4928" DependencyType="cop"/>
<dependency:POSS xmi:id="5306" sofa="6" begin="90" end="93" Governor="5313" Dependent="5322" DependencyType="poss"/>
<dependency:POSS xmi:id="5751" sofa="6" begin="133" end="136" Governor="5758" Dependent="5767" DependencyType="poss"/>
<dependency:POSS xmi:id="6385" sofa="6" begin="172" end="175" Governor="6361" Dependent="6352" DependencyType="poss"/>
<dependency:POSS xmi:id="6392" sofa="6" begin="203" end="206" Governor="6234" Dependent="6216" DependencyType="poss"/>
<dependency:POSS xmi:id="6399" sofa="6" begin="362" end="365" Governor="6406" Dependent="6415" DependencyType="poss"/>
<constituent:ADVP xmi:id="5808" sofa="6" begin="149" end="153" constituentType="ADVP" parent="5784" children="5819"/>
<constituent:ADVP xmi:id="4937" sofa="6" begin="295" end="301" constituentType="ADVP" parent="4894" children="4948"/>
<dependency:ADVCL xmi:id="6866" sofa="6" begin="31" end="35" Governor="5499" Dependent="5632" DependencyType="advcl"/>

      

I know there is a getElementsByTagName function, but that doesn't help me in this case. I want to get all items with prefix dependency.

Thanks in advance!

+3


source to share


2 answers


Ok I found a solution:

getElementsByTagNameNS (namespace, "*");



The dependency namespace is declared at the top of the XML file.

+1


source


You can use xPath.



Here's an example to find any element that has an attribute starting with 'poss': //*[starts-with(@DependencyType,'poss')]

0


source







All Articles