Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6332865
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T18:21:36+00:00 2026-05-24T18:21:36+00:00

I would like to have Java generate a ‘0’ in case an XPath expression

  • 0

I would like to have Java generate a ‘0’ in case an XPath expression evaluates to ‘false’.

I have this Java code:

//Read the input XML document
private SAXBuilder parser = new SAXBuilder();
    private Document characters;
    private XPath pProbs;
    private List<Attribute> probs;
    private Double[] dprobs;
    private String pathToSourceXml;
    private String content1, content2, content3, content4, content5;

    characters = parser.build(pathToSourceXml);
    pProbs = XPath.newInstance("/n-grams-sorted/n-gram[contains(.,"+content1+") or contains(.,"+content2+") or contains(.,"+content3+") or contains(.,"+content4+") or contains(.,"+content5+")]/@probability");
    probs = (List<Attribute>) pProbs.selectNodes(characters);
    ...

//Return all the values of the @probability attibrutes
public Double[] getProbs(String pathToSourceXml) {
    this.pathToSourceXml = pathToSourceXml;

    List<Double> theDoubles = new ArrayList();
    dprobs = new Double[5];
    for (int i=0; i<probs.size(); i++) {
        theDoubles.add(Double.parseDouble(probs.get(i).getValue()));
        dprobs[i] = theDoubles.get(i);
    }
    return dprobs;
}

The problem here is that this code:

pProbs = XPath.newInstance("/n-grams-sorted/n-gram[contains(.,"+content1+") or contains(.,"+content2+") or contains(.,"+content3+") or contains(.,"+content4+") or contains(.,"+content5+")]/@probability");

only returns 4 elements because ‘content1’ returns ‘false’. There are no n-gram nodes whose content contains the string ‘$ $ $’. However, the rest of the content nodes evaluates to ‘true’.

If one of these contains() expressions evaluates to ‘false’, I must draw a ‘0’ in my Xaml code. A ‘0’ must appear on the screen, such as:

'# # #' = 0.0015
'? ? ?' = 0.0047
'k i d' = 0.0012
'$ $ $' = 0

I can’t fetch this ‘0’. I don’t know how to say: “return a zero in case that there are no nodes that contain ‘$ $ $’ as content.

Any ideas on how to do this?

Thank you.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T18:21:36+00:00Added an answer on May 24, 2026 at 6:21 pm

    Here is the answer:

    public class XPathCharacters {
        private SAXBuilder parser = new SAXBuilder();
        private Document characters;
        private XPath pProbs, pContent1, pContent2, pContent3, pContent4, pContent5;
        private List<Attribute> probs1, probs2, probs3, probs4, probs5;
        private List<List<Attribute>> probs;
        private List<String> noNodes;
        private Double[] dprobs;
        private String pathToSourceXml;
        private String content1, content2, content3, content4, content5;
        private int noNode;
    
        public XPathCharacters(){
    
        }
    
        public XPathCharacters(String path, String content1,String content2,String content3,String content4,String content5){
            setPathToSourceXml(path);
            this.content1 = content1;
            this.content2 = content2;
            this.content3 = content3;
            this.content4 = content4;
            this.content5 = content5;
            noNode = 0;
            initiate();
        }
    
        public void initiate() {
            try {
            //Evaluate each XPath expression seperately 
                characters = parser.build(pathToSourceXml);
                pContent1 = XPath.newInstance("/n-grams-sorted/n-gram[contains(.,"+content1+")]/@probability");
                pContent2 = XPath.newInstance("/n-grams-sorted/n-gram[contains(.,"+content2+")]/@probability");
                pContent3 = XPath.newInstance("/n-grams-sorted/n-gram[contains(.,"+content3+")]/@probability");
                pContent4 = XPath.newInstance("/n-grams-sorted/n-gram[contains(.,"+content4+")]/@probability");
                pContent5 = XPath.newInstance("/n-grams-sorted/n-gram[contains(.,"+content5+")]/@probability");
    
                //Convert the result of the above XPath expressions to nodes
                probs1 = (List<Attribute>) pContent1.selectNodes(characters);
                probs2 = (List<Attribute>) pContent2.selectNodes(characters);
                probs3 = (List<Attribute>) pContent3.selectNodes(characters);
                probs4 = (List<Attribute>) pContent4.selectNodes(characters);
                probs5 = (List<Attribute>) pContent5.selectNodes(characters);
    
            } catch (JDOMException jdome) {
                System.out.println("Error at XPathInformationgain.initiate(): JDOMException: " + jdome.getMessage());
            } catch (IOException ioe) {
                System.out.println("Error at XPathInformationgain.initiate(): IOException: " + ioe.getMessage());
            }
        }
    
        private void setPathToSourceXml(String path) {
            this.pathToSourceXml = path;
        }
    
        public Double[] getProbs(String pathToSourceXml) {
            this.pathToSourceXml = pathToSourceXml;
    
            probs = new ArrayList();
            probs.add(probs1);
            probs.add(probs2);
            probs.add(probs3);
            probs.add(probs4);
            probs.add(probs5);
    
            List<Double> theDoubles = new ArrayList();
            dprobs = new Double[5];
            noNodes = new ArrayList();
            int j=0;
            for (int i=0; i<5; i++) {
                if (probs.get(i).size() > 0) {
                    System.out.println("i: "+i);
                    System.out.println("j: "+j);
                   //Add the value of all these nodes to an Array so you can work with them
                   theDoubles.add(Double.parseDouble(probs.get(i).get(0).getValue()));
                    dprobs[j] = theDoubles.get(j);
                }
                else {
                    //If one of the values happens to be a zero-length array, add 0.0 to the array of values
                    theDoubles.add(0.0);
                }
            }
            return dprobs;
        }
    
        public List<String> getNoNodes() {
            return noNodes;
        }
    }
    

    (This code is not working for what I want to accomplish outside of this question, but it works for this question because I have a way of returning a ‘0’ in java, when the XPath expression yields zero-length).

    The solution is this part really:

            else {
                theDoubles.add(0.0);
            }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a java.hprof.txt file (automatically generated after an OutOfMemoryError) which I would like
I would like to have a Java component which has a resize icon on
I have an application where I would like to have mixed Java and Scala
I have a Java application and I would like to make it extensible. To
I have an Java applications which I would like to run with different values
I would like to have a Maven goal trigger the execution of a java
I have a single user java program that I would like to have store
I am developing a Java desktop application and would like to have an external
I currently have a small Java program which I would like to run both
I have downloaded Eclipse Java EE version (3.5) and I would like to use

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.