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 7083245
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T07:09:16+00:00 2026-05-28T07:09:16+00:00

I’m trying to wrap my head around alternatives for global variables. Case in question

  • 0

I’m trying to wrap my head around alternatives for global variables.

Case in question is one where I need to find values in one XML and compare against another XML (or more). Since the XML JQuery is, itself, a function and the operations beneath that are functions inside of a function (ugh) I can’t get a value from 2 functions deep and use it globally.

So it’s presently not possible for me to get an XML value from 1 file and use it filter another XML file, and that’s where I need help.

I’ve been handed 3 XML files.

File 1 – categories.xml – contains a mapping of categories
ex…

<CAT>
      <OA1>True</OA1>
      <OA2>False</OA2>
      <OA3>True</OA3>
      <EP1>True</EP1>
      <EP2>False</EP2>
      <EP3>False</EP3>
</CAT>

File 2 = oa.xml – contains the values of each OA record
ex…

<OA>
      <Name>Name 1</Name>
      <City>City</City>
      <State>ST</State>
</OA>

and so on…

File 3 = EP.xml – contains the values of each EP record
Copy code

<EP>
      <object 1></object1>
      <object 2></object2>
      <object 3></object3>
</EP>

Now, what I thought I could do when I started was to allow the user to select a category, and based upon that selection return 2 tables containing the values that mapped to that category.

My problem is that when JQuery starts parsing XML it does it in a function (in all examples I’ve seen) so I have no idea how to set a variable inside of one function and use it inside of the next function used to open the 2nd file, or the 3rd.

Here is what I have now:
Copy code

<script>
      var catid = ""; // I thought this, being outside of the function would be a global varaible
      OA1 = ""; // I tried it with and without var in front
      var OAid = "";
      $(document).ready(function(){ //When opening an XML we do it in a function
        $.ajax({
            type: "GET",
            url: "xml/categories.xml",
            dataType: "xml",
            success: function(xml) {
                $(xml).find('Cat').each(function(){
                                                //Next 2 rows don't matter b/c I can't use their values outside 
                                                //of the function
                  var catid = $(this).find('Catid').text(); 
                  var OA1 = $(this).find('OA1').text();

                  $('<div class="page-wrap"></div>').html('<table><tr><td>' + catid +'</td><td>OA1 '+ OA1 +'</td></tr></table></div>').appendTo('#page-wrap');

                });
        }
       });
      });
//The only way I know how to open up the next XML, start all over again
      $(document).ready(function(){
        $.ajax({
            type: "GET",
            url: "xml/OA.xml",
            dataType: "xml",
            success: function(xml) {
                $(xml).find('OAData').each(function(){
                  var OAid = $(this).find('OAid').text();
                  $('<div class="page-wrap"></div>').html('<table><tr><td>OA ID is '+ OAid +'</td></tr></table></div>').appendTo('#page-wrap');

                });
            }
          });
         });
</script>
//Somebody shoot me

ANY ADVICE would be most appreciated – I haven’t been able to even consider the compare operation b/c of the variable issue.

Is there a way to compare 2 XML files and I’m just missing it, or can you recommend a solution using some sort of temporary location?

  • 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-28T07:09:17+00:00Added an answer on May 28, 2026 at 7:09 am

    So taking the suggestion of @Kevin B and enhancing it a bit, you can pass values to your different success handler functions easily if you factor those functions out into separate functions.

    <script>
          var catid = ""; // I thought this, being outside of the function would be a global varaible
          OA1 = ""; // I tried it with and without var in front
          var OAid = "";
          $(document).ready(function(){ //When opening an XML we do it in a function
            $.ajax({
                type: "GET",
                url: "xml/categories.xml",
                dataType: "xml",
                success: function(xml) {
                    $(xml).find('Cat').each(function(){
                                                    //Next 2 rows don't matter b/c I can't use their values outside 
                                                    //of the function
                      var catid = $(this).find('Catid').text(); 
                      var OA1 = $(this).find('OA1').text();
    
                      $('<div class="page-wrap"></div>').html('<table><tr><td>' + catid +'</td><td>OA1 '+ OA1 +'</td></tr></table></div>').appendTo('#page-wrap');
    
                    });
                  $.ajax({
                    type: "GET",
                    url: "xml/OA.xml",
                    dataType: "xml",
                    success: function(xml) { getCategoriesSuccess(xml, catid, OA1, OAid); }
                  });
                }
           });
          });
    
          function getCategoriesSuccess(xml, catid, OA1, OAid) {
            $(xml).find('Cat').each(function(){
                                            //Next 2 rows don't matter b/c I can't use their values outside 
                                            //of the function
              var catid = $(this).find('Catid').text(); 
              var OA1 = $(this).find('OA1').text();
    
              $('<div class="page-wrap"></div>').html('<table><tr><td>' + catid +'</td><td>OA1 '+ OA1 +'</td></tr></table></div>').appendTo('#page-wrap');
    
            });
            $.ajax({
                type: "GET",
                url: "xml/OA.xml",
                dataType: "xml",
                success: function(xml) { getOASuccess(xml, OAid); }
              });
             });        
          }
    
          function getOASuccess(xml, OAid){
            $(xml).find('OAData').each(function(){
              var OAid = $(this).find('OAid').text();
              $('<div class="page-wrap"></div>').html('<table><tr><td>OA ID is '+ OAid +'</td></tr></table></div>').appendTo('#page-wrap');
            });
          }
    </script>
    

    So inside your $(document).ready()‘s ajax call’s success handler, you make a second ajax call as @Kevin B suggested. You can pass additional data to this by wrapping a function call inside your success handler. And I’ll pass the data that a second nested function call (inside getCategoriesSuccess) needs in that first call so that it’s available for the second call. So that’s why I pass OAid in the first nested function call because it’s needed inside of getOASuccess.

    I’m sure there are other ways to do this, but this gives you a bit of flexiblity with your success handlers.

    I hope this helps. Let me know if there are additional questions and I’ll update my answer accordingly. Good luck!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In my XML file chapters tag has more chapter tag.i need to display chapters
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am reading a book about Javascript and jQuery and using one of the
I am trying to render a haml file in a javascript response like so:
Seemingly simple, but I cannot find anything relevant on the web. What is the
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I need to clean up various Word 'smart' characters in user input, including but

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.