?
Solved

XJS: obtain value of attribute using SelectSingle from an xml

Posted on 2014-12-22
16
Medium Priority
?
124 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 57

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
AWS Certified Solutions Architect - Associate

This course has been developed to provide you with the requisite knowledge to not only pass the AWS CSA certification exam but also gain the hands-on experience required to become a qualified AWS Solutions architect working in a real-world environment.

 
LVL 57

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 36

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 57

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 57

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 57

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 57

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 57

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Browsers only know CSS so your awesome SASS code needs to be translated into normal CSS. Here I'll try to explain what you should aim for in order to take full advantage of SASS.
Create a Windows 10 custom Image with custom task bar and custom start menu using XML for deployment.
In this tutorial viewers will learn how to style elements, such a divs, with a "drop shadow" effect using the CSS box-shadow property Start with a normal styled element, such as a div.: In the element's style, type the box shadow property: "box-shad…
In this tutorial viewers will learn how to style a corner ribbon overlay for an image using CSS Create a new class by typing ".Ribbon":  Define the class' "display:" as "inline-block": Define its "position:" as "relative": Define its "overflow:" as …
Suggested Courses

770 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