Solved

XJS: obtain value of attribute using SelectSingle from an xml

Posted on 2014-12-22
16
114 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
  • 8
  • 7
16 Comments
 
LVL 51

Expert Comment

by:HainKurt
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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 51

Expert Comment

by:HainKurt
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 51

Expert Comment

by:HainKurt
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 51

Expert Comment

by:HainKurt
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 51

Expert Comment

by:HainKurt
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 51

Expert Comment

by:HainKurt
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 51

Expert Comment

by:HainKurt
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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
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…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

808 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