开发者

Xpath with wildcards returning partial node name

开发者 https://www.devze.com 2023-03-12 13:52 出处:网络
I am new to Xpath and am trying to parse a UML model saved in XML format. I have some nodes in the form :-

I am new to Xpath and am trying to parse a UML model saved in XML format. I have some nodes in the form :-

<ModelProfile:create xmi:id='_16_0_1_187c046a_1307545286478_527165_409' base_Element='_16_0_1_187c046a_1307545286478_986851_398'/>

<ModelProfile:get xmi:id='_16_0_1_187c046a_1307545286478_698482_412' base_Element='_16_0_1_187c046a_1307545286478_639036_401'/>

I have the base_Element value and know that the nodes begin with ModelProfile: , but I need to get what the value after the ModelProfile: string is - i.e. the word "get" or "create" in the above 2 examples.

How can I 开发者_如何学Godo this in Xpath (in Java)?

Thanks

Sarah


Assuming you have registered the ModelProfile namespace URI, you can use:

"//ModelProfile:*[@base_Element='" + yourBaseElementValue + "']"

to get all nodes with that particular base_Element attribute value.

From there on you can use the nodeName property of the returned nodes.


You will need to identify the elements by where they appear in the document e.g. get all elements that are children of the "umlModel" element. After that it is simple to pull out the local name (the "get" or "create" bit).

The "ModelProfile" part is the namespace prefix which will be bound to a URI. You will need to understand how namespaces work before you will get XPath to work as you expect. The w3c schools intro seems reasonable: http://www.w3schools.com/xml/xml_namespaces.asp

0

精彩评论

暂无评论...
验证码 换一张
取 消