Showing posts with label xpath. Show all posts
Showing posts with label xpath. Show all posts

Friday, April 24, 2020

Fetching Unique XML Tags based on Values

Listing down the unique values of a tag in entire XML
Below is the raw information in the form of an XML

<?xml version="1.0" encoding="UTF-8"?>
<STATES>
<STATE>
<NAME>TELANGANA</NAME>
<REGION>SOUTH</REGION>
<CAPITAL>HYDERABAD</CAPITAL>
</STATE>
<STATE>
<NAME>TAMILNADU</NAME>
<REGION>SOUTH</REGION>
<CAPITAL>CHENNAI</CAPITAL>
</STATE>
<STATE>
<NAME>KARNATAKA</NAME>
<REGION>SOUTH</REGION>
<CAPITAL>BENGALURU</CAPITAL>
</STATE>
<STATE>
<NAME>KERALA</NAME>
<REGION>SOUTH</REGION>
<CAPITAL>TRIVANDRUM</CAPITAL>
</STATE>
<STATE>
<NAME>MAHARASHTRA</NAME>
<REGION>WEST</REGION>
<CAPITAL>MUMBAI</CAPITAL>
</STATE>
<STATE>
<NAME>TELANGANA</NAME>
<REGION>SOUTH</REGION>
<CAPITAL>HYDERABAD</CAPITAL>
</STATE>
<STATE>
<NAME>TAMILNADU</NAME>
<REGION>SOUTH</REGION>
<CAPITAL>CHENNAI</CAPITAL>
</STATE>
</STATES>

I want to get only the unique state names from the above information. (TELANGANA,TAMILNADU,KARNATAKA,KERALA,MAHARASHTRA)


//STATE/NAME[not(. = ../following-sibling::STATE/NAME)]

Credits : 
Padmapriya Iyengar (padmapriya.iyengar@gmail.com)

Thursday, January 29, 2015

XPath for the node with maximum value for a particular element : XPath1.0

Here is the list of bond characters played in bond movies and their earnings from movies,

<JamesBondCharacters>
<JamesBond>
<Name>Sean Connery</Name>
<Earnings>40000</Earnings>
<Currency>GBP</Currency>
</JamesBond>
<JamesBond>
<Name>Roger Moore</Name>
<Earnings>55000</Earnings>
<Currency>GBP</Currency>
</JamesBond>
<JamesBond>
<Name>George Lazenby</Name>
<Earnings>20000</Earnings>
<Currency>GBP</Currency>
</JamesBond>
<JamesBond>
<Name>Timothy Dalton</Name>
<Earnings>25000</Earnings>
<Currency>GBP</Currency>
</JamesBond>
<JamesBond>
<Name>Pierce Brosnan</Name>
<Earnings>70000</Earnings>
<Currency>GBP</Currency>
</JamesBond>
<JamesBond>
<Name>Danial Craig</Name>
<Earnings>30000</Earnings>
<Currency>GBP</Currency>
</JamesBond>
</JamesBondCharacters>

From this list I want to get the Bond details who earned the highest and least during his time. This example is just taken for fun to understand the XPath. 

To get the highest earned bond details use the below XPath

//JamesBond[not(./Earnings<//JamesBond/Earnings)]

To get the least earned bond details use the below XPath

//JamesBond[not(./Earnings>//JamesBond/Earnings)]

Note : The XPath which we used here is valid in XPath1.0. Alternatively you can check the same here in the below link

http://www.online-toolz.com/tools/xpath-editor.php

Credits :
GIB Learnings from Anitha and Padma Priya Iyengar

Wednesday, July 16, 2014

Listing down unique tags in entire XML

I have a requirement here, I would try to keep it in very simple terms,

I have a raw information in the form of XML:

<MyFruits>
<Apple>23</Apple>
<Mango>12</Mango>
<Orange>10</Orange>
<Apple>19</Apple>
</MyFruits>

I want to get only unique fruits among them. (Apple,Mango and Orange)

Can anyone write an XPath to retrieve this?
Status : Archived

You can give your answers as comments

Hint : 
Had the XML be like below

<MyFruits>
<Apple>23</Apple>
<Apple>19</Apple>
<Mango>12</Mango>
<Orange>10</Orange>
</MyFruits>

This XPath would have worked 

//MyFruits/*[not(name(.)=name(following-sibling::*))]


Friday, December 20, 2013

Java : XPathReader with CDATA

String xpath = "//IDOC[position()>=1 and position()<=3]";
XPath xpathObj = XPath.getXPathInstance(xpath);
XPathReader xpathReaderObj = XPathReader.getXPathReader(xpathObj, null,oDoc, fileName);
xpathReaderObj.open();
int resultNode = xpathReaderObj.loadNextNode();

Explanation of the above scenario :
In Order to read the data only chunkwise you need to go with xpathReader to have better performance as this thing will be reading on chunk basis.

Problem : But the problem with this function is, It won't load the CDATA elements if your XML have any. Instead the element nodes will be empty.

Tuesday, December 17, 2013

XPath : Select Unique in XML

XPath.getMatchingNodes("descendant-or-self::HMT_PROCESS_CONFIG[not(PROCESS_NAME=preceding::HMT_PROCESS_CONFIG/PROCESS_NAME)]/PROCESS_NAME", null, processData);