954,517 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

XPATH for complex data structure

Please see attached XML file.

I am using the XPATH expression getValueXPath() in JavaScript to extract one value from an XML alert message - to control a robotic telescope.

I need to extract the value for Dec, a coordinate location in the sky that is a child of

The particular value I need to retrieve is green/bold in the below excerpt.

<WhereWhen>
<ObsDataLocation>
 <ObservatoryLocation xlink:type="simple" xlink:href="ivo://STClib/Observatories#GEOLUN/" id="GEOLUN"/>
<ObservationLocation>
 <AstroCoordSystem xlink:type="simple" xlink:href="ivo://STClib/CoordSys#UTC-FK5-GEO/" id="UTC-FK5-GEO"/>
<AstroCoords coord_system_id="UTC-FK5-GEO">
<Time unit="s">
<TimeInstant>
 <ISOTime>2012-01-19T04:04:30.21</ISOTime>
 </TimeInstant>
 </Time>
<Position2D unit="deg">
 <Name1>RA</Name1>
 <Name2>Dec</Name2>
<Value2>
 <C1>120.0271</C1>
 <strong><C2>-9.0739</C2></strong>
 </Value2>
 <Error2Radius>0.0500</Error2Radius>
 </Position2D>
 </AstroCoords>
 </ObservationLocation>
 </ObsDataLocation>
 <Description>The RA,Dec coordinates are of the type: source_object.</Description>
 </WhereWhen>


I have tried many variations on this expression, but have not been able to extract the value

dec = getValueXPath(XmlDoc, '/z:VOEvent/WhereWhen/ObsDataLocation/ObservationLocation/AstroCoords/Position2D/Dec');


What is the correct XPATH for this particular file and format?

Thank you in advance,
Tom Krajci

Attachments BAT_GRB_Pos_512035-500.xml (7.14KB)
Tom_Krajci
Newbie Poster
3 posts since Jan 2012
Reputation Points: 24
Solved Threads: 0
 

Looks like it should end with this /Position2D/Value2/C2 (Based on this)

<WhereWhen>
  <ObsDataLocation>
    <ObservationLocation>
      <AstroCoords coord_system_id="UTC-FK5-GEO">
        <Position2D unit="deg">
          <Value2>
            <C2>-9.0739</C2>
pritaeas
Posting Expert
Moderator
5,475 posts since Jul 2006
Reputation Points: 653
Solved Threads: 874
 

You're probably right, the XPATH should be:

/z:VOEvent/WhereWhen/ObsDataLocation/ObservationLocation/AstroCoords/Position2D/Value2/C2

But it appears that XML namespaces are an important factor here.

At the root of the XML file I attached in the first post, we have

<voe:VOEvent xmlns:voe="http://www.ivoa.net/xml/VOEvent/v1.1" version="1.1" role="observation" ivorn="ivo://nasa.gsfc.gcn/SWIFT#BAT_GRB_Pos_512035-500" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xlink="http://www.w3.org/1999/xlink" xsi:schemaLocation="http://www.ivoa.net/xml/VOEvent/v1.1 http://www.ivoa.net/xml/VOEvent/VOEvent-v1.1.xsd">


And the Java script deals with it this way:

XmlDoc.setProperty("SelectionNamespaces", "xmlns:z='http://www.ivoa.net/xml/VOEvent/v1.1'");


That works well for 'shallow' XPATH queries, but for my 'deep' query, I see that a new XML namespace is provided when we get down to <ObsDataLocation xmlns="http://www.ivoa.net/xml/STC/stc-v1.30.xsd">


What does the XPATH look like when we have one xmlns in the root, and another xmlns down lower?

Tom_Krajci
Newbie Poster
3 posts since Jan 2012
Reputation Points: 24
Solved Threads: 0
 

I think you should just add it like the other one, and apply it where needed (like the z: prefix in your example).

pritaeas
Posting Expert
Moderator
5,475 posts since Jul 2006
Reputation Points: 653
Solved Threads: 874
 

Thank you!

If anyone is interested, below are the dirty details I learned and implemented.

Original version of the filter script uses only one (the default value) xml namespace:

XmlDoc.setProperty("SelectionNamespaces", "xmlns:z='http://www.ivoa.net/xml/VOEvent/v1.1'");


That's fine for 'shallow' searches in the Swift GRB XML files, but the value for declination is 'deep' in the XML tree, and you need to identify another xml namespace...so now that line looks like:

XmlDoc.setProperty("SelectionNamespaces", "xmlns:z='http://www.ivoa.net/xml/VOEvent/v1.1' xmlns:y='http://www.ivoa.net/xml/STC/stc-v1.30.xsd'");


because if you look at this node: ...it lists a new namespace:

<ObsDataLocation xmlns="http://www.ivoa.net/xml/STC/stc-v1.30.xsd">

It's not an elegant solution, but I created a javascript function:

function getDecValue(XmlDoc)
{
    return getValueXPath(XmlDoc, '/z:VOEvent/WhereWhen/y:ObsDataLocation/y:ObservationLocation/y:AstroCoords/y:Position2D/y:Value2/y:C2');
}

note that the new namespace is called/referenced with y: ....and I had to add it for every child/level....down to the final node. Whew!

But now, I get this result with a test GRB message that's south of +20 declination:


22-Jan-2012 16:28:50.6: NEW MESSAGE (Observation): ivo://nasa.gsfc.gcn/SWIFT#BAT_GRB_Pos_2012-01-22T16:28:20_879862-0
22-Jan-2012 16:28:50.6: Filtering...
22-Jan-2012 16:28:50.7: ...rejected by filter with explanation:22-Jan-2012 16:28:50.7: A SWIFT/GRB BAT_GRB_Pos alert with declination 15.0001 degrees was received and discarded.

In other words it's now working the way I want it.

Thank you again!

Tom_Krajci
Newbie Poster
3 posts since Jan 2012
Reputation Points: 24
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You