Solved

XJS: obtain value of attribute using SelectSingle from an xml

Posted on 2014-12-22
16
118 Views
Last Modified: 2015-01-01
I have the following xml

<Service>
      <Amount>22.99</Amount>
      <SvcPrice Total="2299">
      </SvcPrice>
      <Booking>
            <Code>SEAT</Code>
            <Meth>TES</Meth>
      </Booking>
</Service>


I want to obtain the value of SvcPrice/@Total where Booking/Code = 'SEAT' and store into a variable.
I was trying something as follows:

var test = node.SelectSingle("Service[Booking[Code='SEAT']]");
var test2 = node.ValueOfSelect("Service[Booking[Code='SEAT']]");

but no work.
0
Comment
Question by:badtz7229
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 8
  • 7
16 Comments
 
LVL 53

Expert Comment

by:Huseyin KAHRAMAN
ID: 40513954
try this:

var test = node.SelectSingle("//Service[Booking[Code='SEAT']]/SvcPrice");

then you need to get attribute of the returned node...
0
 

Author Comment

by:badtz7229
ID: 40513957
@HainKurt
i'm wanting to obtain SvcPrice/@Total
0
 

Author Comment

by:badtz7229
ID: 40513968
@HainKurt
ur suggestion unfortunately didn't work.
0
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
LVL 53

Expert Comment

by:Huseyin KAHRAMAN
ID: 40513971
is this returning something?

var test = node.SelectSingleNode("//Service[Booking[Code='SEAT']]/SvcPrice");

if yes, then debug and add it watch window, and get the attribute "Total"
0
 
LVL 35

Expert Comment

by:mccarl
ID: 40514306
Try this statement then...

var test = node.SelectSingle("//Service[Booking/Code='SEAT']/SvcPrice/@Total");

Open in new window

0
 
LVL 53

Expert Comment

by:Huseyin KAHRAMAN
ID: 40514326
ok, here it is (IE version):

<!DOCTYPE html>
<script>
var xmlDoc = new ActiveXObject("Msxml2.DOMDocument.3.0");
var currNode;
xmlDoc.async = false;
xmlDoc.loadXML('<Service><Amount>22.99</Amount><SvcPrice Total="2299"></SvcPrice><Booking><Code>SEAT</Code><Meth>TES</Meth></Booking></Service>');
currNode = xmlDoc.selectSingleNode("//Service[Booking[Code='SEAT']]/SvcPrice");
alert (currNode.attributes[0].nodeValue);
</script>

Open in new window

0
 

Author Comment

by:badtz7229
ID: 40515626
i added dummy data inside SvcPrice such as:


<Service>
      <Amount>22.99</Amount>
      <SvcPrice Total="2299">
            <Currency>USD</Currency>
            <Taxes>
                  <Tax Amount="195">
                        <Designator>US</Designator>
                  </Tax>
               </Taxes>
      </SvcPrice>
      <Booking>
            <Code>SEAT</Code>
            <Meth>TES</Meth>
      </Booking>
</Service>

and using @mccarl solution - it returned USDUS

using @HainKurt solution - it returned nothing.
0
 

Author Comment

by:badtz7229
ID: 40515631
maybe i have to use a getattribute function
0
 
LVL 53

Expert Comment

by:Huseyin KAHRAMAN
ID: 40515633
I used this

<!DOCTYPE html>
<script>
var xmlDoc = new ActiveXObject("Msxml2.DOMDocument.3.0");
var currNode;
xmlDoc.async = false;
xmlDoc.loadXML('<Service><Amount>22.99</Amount><SvcPrice Total="2299"></SvcPrice><Booking><Code>SEAT</Code><Meth>TES</Meth></Booking></Service>');
xmlDoc.loadXML('<Service><Amount>22.99</Amount><SvcPrice Total="2299"><Currency>USD</Currency><Taxes><Tax Amount="195"><Designator>US</Designator></Tax></Taxes></SvcPrice><Booking><Code>SEAT</Code><Meth>TES</Meth></Booking></Service>');
var test = xmlDoc.selectSingleNode("//Service[Booking[Code='SEAT']]/SvcPrice");
alert (test.attributes[0].nodeValue);
</script>

Open in new window


save it to html file and open with IE
it shows "2299" as expected...
0
 

Author Comment

by:badtz7229
ID: 40515635
or a combination of SelectSingle and GetAttribute ?
0
 
LVL 53

Expert Comment

by:Huseyin KAHRAMAN
ID: 40515636
i guess it will be better if you post your code and show us how you run  it (which browser)...
0
 

Author Comment

by:badtz7229
ID: 40515658
i am doing the following.

var test =  node.SelectSingle("//Service[Booking[Code='SEAT']]/SvcPrice");
var att = test.GetAttribute ("Total");

test returns USDUS
att - returns null
0
 
LVL 53

Expert Comment

by:Huseyin KAHRAMAN
ID: 40515662
i am doing the following.

we know what you are doing... I want the code before & after this... what xml objects do you use, how do you create it, how & where do you run this...
0
 
LVL 53

Expert Comment

by:Huseyin KAHRAMAN
ID: 40515665
you said these are returning NOTHING!

var test = xmlDoc.selectSingleNode("//Service[Booking[Code='SEAT']]/SvcPrice");
var att = test.attributes[0].nodeValue;
0
 

Accepted Solution

by:
badtz7229 earned 0 total points
ID: 40516601
correct. those 2 expression did not return anything.
i went the long route and did the following instead and it worked for me:

var testnode  = ValueOfSelect(node, "Booking/Code");
if ( testnode  =='SEAT')
  var test1 = node.SelectSingle("SvcPrice").GetAttribute("Total");

i was hoping to do it all 1 one line using a filter but i give up. this will have to do.

thanks so much for all your help. i may be able to use something similar to ur suggestions in the future for other scenarios.
0
 

Author Closing Comment

by:badtz7229
ID: 40526450
see comment in my solution.
0

Featured Post

Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

So you have coded your own WordPress plugin and now you want to allow users to upload images to a folder in the plugin folder rather than the default media location? Follow along and this article will show you how to do just that!
Many times as a report developer I've been asked to display normalized data such as three rows with values Jack, Joe, and Bob as a single comma-separated string such as 'Jack, Joe, Bob', and vice versa.  Here's how to do it. 
In this Micro Tutorial viewers will learn how to create a CSS image sprite (In a later tutorial, viewers will learn how to use CSS and HTML to create a navigation menu using this sprite) Open a new Photoshop document with a width of (Icon width)x(N…
In this tutorial viewers will learn how to style transparent/translucent elements using alpha transparency in CSS Start with a normal styled element, such as a div.: Define its "background-color" property as "rgba (255, 255, 255, .5): The numbers in…

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question