Solved

XJS: obtain value of attribute using SelectSingle from an xml

Posted on 2014-12-22
16
117 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 51

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
MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

 
LVL 51

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 51

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 51

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 51

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 51

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 51

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone 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

Suggested Solutions

Title # Comments Views Activity
Debugging Html 8 31
PHP Web Development 6 21
setInterval() calls function twice in one interval 2 36
How do I post more than 1 item to php backend 24 29
SASS allows you to treat your CSS code in a more OOP way. Let's have a look on how you can structure your code in order for it to be easily maintained and reused.
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.
In this tutorial viewers will learn how to customize the background color and font color of highlighted text using the ::selection element in CSS Begin by defining the selected text as an element in CSS by typing "::selection": Style the ::selection…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

763 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