Solved

XJS: obtain value of attribute using SelectSingle from an xml

Posted on 2014-12-22
16
110 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
 
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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
The viewer will receive an overview of the basics of CSS showing inline styles. In the head tags set up your style tags: (CODE) Reference the nav tag and set your properties.: (CODE) Set the reference for the UL element and styles for it to ensu…
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

744 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now