Hi all,I have a UDF in Java that does some XML processing. My parser works 
fine. I am now adding some code to this UDF that uses XPath. However, my XPath 
expression always returns null. I wrote another standalone program that has the 
same path expression and same document as its input, I get correct results. 
However, my Pig UDF with same XPath expression and same i/p it retunes a null 
result. Please find relevant code snippet attached. Has anyone used XPath in 
UDFs. Also I saw mention of XPath with PiggyBan, but my 0.11 code does not have 
that in its contrib area. Does anyone has any experience with using that?
public parseXml (String xmlRecords){/* I have a sequence file so xmlRecords isn 
basically the xml file to parse */
 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 
dbf.setNamespaceAware(true);  db = 
DocumentBuilderFactory.newInstance().newDocumentBuilder();InputSource is = new 
InputSource();is.setCharacterStream(new StringReader(xmlRecords));doc = 
db.parse(is);
// other codeā€¦.
} public String tokenIdToStringXPath (String tokenidstr)    {
 String querystr = "//lang:Token[@xml:id='" + tokenidstr + "']";
        Element tokenelem = null;        String resultingtokenstr = "Sameer";   
     StringBuilder resultstr = new StringBuilder();
        try            {

                XPathExpression expr = xPath.compile(querystr);                
Object result = expr.evaluate(doc, XPathConstants.NODESET);                
NodeList nodes = (NodeList) result;
                resultstr.append("Querystr: " + querystr);                
resultstr.append(" ::Nodes: " + nodes.getLength());
}
The XPath expression after evaluation never 
 //lang:Token[@xml:id='639'] ::Nodes: 0                                         
  

Reply via email to