Using XPath to get a certain field from XML (UPS)
First, I apologize for whats probably a newbie question. I've spent the last 2 days scrounging the web trying to find an answer that makes sense to me and I keep seeing references to XPath and querying an XML file, so I feel I'm CLOSE, I'm just not getting how to tell it WHICH field I wanna select and how to tell it what to call that value when I need to use it in a SQL statement. I have been able to write an XSL file to display this information through an ASP page but now I need to take the shipping charge, and write it to my database for that order (add it to the subtotal, etc - NOT a problem, once I have it as a variable I can use). So lets start with my XML file (returned from UPS):
===========================================
XML File
===========================================
<?xml version="1.0"?>
<RatingServiceSelectionResponse>
<Response>
<TransactionReference>
<CustomerContext>Rating and Service</CustomerContext>
<XpciVersion>1.0001</XpciVersion>
</TransactionReference>
<ResponseStatusCode>1</ResponseStatusCode>
<ResponseStatusDescription>Success</ResponseStatusDescription>
</Response>
<RatedShipment>
<Service>
<Code>03</Code>
</Service>
<BillingWeight>
<UnitOfMeasurement>
<Code>LBS</Code>
</UnitOfMeasurement>
<Weight>2.0</Weight>
</BillingWeight>
<TransportationCharges>
<CurrencyCode>USD</CurrencyCode>
<MonetaryValue>5.96</MonetaryValue>
</TransportationCharges>
<ServiceOptionsCharges>
<CurrencyCode>USD</CurrencyCode>
<MonetaryValue>0.00</MonetaryValue>
</ServiceOptionsCharges>
<TotalCharges>
<CurrencyCode>USD</CurrencyCode>
<MonetaryValue>5.96</MonetaryValue>
</TotalCharges>
<GuaranteedDaysToDelivery></GuaranteedDaysToDelivery>
<ScheduledDeliveryTime></ScheduledDeliveryTime>
<RatedPackage>
<TransportationCharges>
<CurrencyCode>USD</CurrencyCode>
<MonetaryValue>5.96</MonetaryValue>
</TransportationCharges>
<ServiceOptionsCharges>
<CurrencyCode>USD</CurrencyCode>
<MonetaryValue>1.00</MonetaryValue>
</ServiceOptionsCharges>
<TotalCharges>
<CurrencyCode>USD</CurrencyCode>
<MonetaryValue>6.96</MonetaryValue>
</TotalCharges>
<Weight>1.3</Weight>
<BillingWeight>
<UnitOfMeasurement>
<Code>LBS</Code>
</UnitOfMeasurement>
<Weight>2.0</Weight>
</BillingWeight>
</RatedPackage>
</RatedShipment>
</RatingServiceSelectionResponse>
===========================================
ASP Snippet (where I read in the XML and XSL files and display
===========================================
set xmlDoc=server.createObject("Msxml2.FreeThreadedDOM Document.4.0")
set xslDoc=server.createObject("Msxml2.FreeThreadedDOM Document.4.0")
xmldoc.async = false
xslDoc.async = false
xmlDoc.load "e:\inetpub\wwwroot\kitsforcrafts\ups\" & strFileName
if (xmlDoc.parseError.errorCode <> 0) then
Response.Write "XML error - " & "<br>"
Response.Write "Error Reason: " & xmlDoc.parseError.reason & "<br>"
Response.Write "Source: " & xmlDoc.parseError.srcText & "<br>"
Response.Write "Error Line: " & xmlDoc.parseError.line & "<br>"
Response.Write "Error Position: " & xmlDoc.parseError.linepos & "<br>"
end if
xslDoc.load "e:\inetpub\wwwroot\kitsforcrafts\shipping.xsl "
if (xslDoc.parseError.errorCode <> 0) then
Response.Write "XSL error - " & "<br>"
Response.Write "Error Reason: " & xslDoc.parseError.reason & "<br>"
Response.Write "Source: " & xslDoc.parseError.srcText & "<br>"
Response.Write "Error Line: " & xslDoc.parseError.line & "<br>"
Response.Write "Error Position: " & xslDoc.parseError.linepos & "<br>"
end if
response.write xmlDoc.transformNode(xslDoc)
===========================================
And yes, I know I should be using Server.MapPath but I was just trying to get it WORKING and so hardcoded the paths in (I will go back and change them). The loads DO work, I can display the results just fine. So after that response.write line above, I need to somehow get the field (RatingServiceSelectionResponse/RatedShipment/RatedPackage/TotalCharges/MonetaryValue - the 6.96) into a variable called curShipping that I can use in a SQL statement such as:
"UPDATE CUSTOMER SET SHIPPING_CHARGE = '" & FormatCurrency(curShipping) & "' WHERE ORDER_ID = 78127"
Or
GRAND_TOTAL = SUB_TOTAL + CurShipping
So, what I'm looking for is the code to put in after that line that gets the shipping charge stored in curShipping. XML is already read in as xmlDoc (assuming I can still use it as long as I put the code before the set xmlDoc = nothing line). I probably won't be using XML on a regular basis (at least without another year to figure stuff out), but unfortunately, a windows update a few weeks ago broke the current system we were using to get shipping charges from UPS and its been a real hassle not having that automatic shipping figured when someone checks out through our website (I've got one server, I haven't updated yet that we're running from, but its old and decrepit and I need to get the site moved to the NEW machine but can't until I get the shipping stuff re-written). Too much information I'm sure, but I'm just desperate at this point to get it WORKING.
|