• Post Reply Bookmark Topic Watch Topic
  • New Topic

Generating Dynamic XPath from a XML

 
khadar valli
Greenhorn
Posts: 25
Java MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

Could you please let me know if anyone had come across this scenario of generating dyanamic Xpath from XML. My actual requirement is to display one XML to user. Based on user's selection, i have to generate the XPath.

Please respond ASAP.

Thanks in advance.

Regards,
Khadar.
 
Stephan van Hulst
Bartender
Posts: 6583
84
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's not possible, in general. Can you give an example XML snippet and what XPath you expect it to generate when a user selects a specific element.
 
khadar valli
Greenhorn
Posts: 25
Java MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Stephan,

Our's is TIBCO related project. In which the input data is in XML Format which is different for each and every application based on its source and target. The XML input will be having some keys/Identifiers.

I want to develop one such a tool which can generate a dynamic XPath from the XML based on the Users selection.

Regards,
Khadar.
 
khadar valli
Greenhorn
Posts: 25
Java MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

Can anyone help me on this.

Thanks in advance.

Regards,
Khadar.
 
Dave Tolls
Ranch Hand
Posts: 2276
23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You still need to give an example of what you expect as input, user selection, and output.

If the user selects an element in the XML then what XPath do you want?
There will be a potentially huge number of valid XPaths to represent that element in that XML.
 
g tsuji
Ranch Hand
Posts: 697
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@khadar valli
It can be done relatively easily to construct your own function for the purpose if the library/framework you are working with does not provide you a helper/utility function for the purpose. It can be done with classic java code, it can be done calling a well-isolated xslt template, ... many ways to achieve the same thing.

It is obvious that the "xpath of a node" is forming an equivalent class, if you understand enough mathematics, of huge number of members. In the sense people usually ask, the "representitive member" in the equivalent class is the "absolute xpath" starting from the document root to the node in question using only position index for each intermediate step. The way to construct it is first [1] raise the node in question to its (unique) parent which would be an Element more specifically; [2] then determine the parent's namespace, its prefix in pertinent, and local name; [3] using again xpath to count the preceding-sibling of elements to determine the position index. At the end of this step, you have all the data on-hand to construct one particle of the chain. Then recursively, from the parent Element, you do again the above steps until to encounter the case where the parent return null which signifies you reach the root Element and the process is complete.

If you want to save you that task of constructing a user function, you can sometimes in a position to use some library/framework where such helper/utility function is coded for you. Here are some suggestions. If you use jdom2, it has a utlity class org.jdom2.xpath.XPathHelper which exposes static functions called getAbsolutePath(). But, if you use xerces/xalan, it would be asking much to convert many code lines to jdom2.

If you use, as said xerces/xalan stack, you can specifically download a wsdl4j library and use its utility class com.ibm.wsdl.util.xml.XPathUtils in the wsdl4j.jar where a static function getXPathExprFromNode() is exposed. This time you can directly pass the org.w3c.dom.Node to it and no extra work is needed for conversion or adaptation. This seems suit you better and it is highly probable the library you are working with complies with org.w3c.dom.XXX api's.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!