Reputation: 41
I really need assistance in querying an XML file using xPath in Javacript where a certain tag = a certain data.
Lets say for instance:
<bookstore>
<b1:book>
<name>Hungry Teddy</name>
<author>Bozo</author</author>
<category>fiction</category>
</b1:book>
<b1:book>
<name>Funny Kangaroo</name>
<author>Bozo</author</author>
<category>non-fiction</category>
</b1:book>
<b1:book>
<name>How to JAVA</name>
<author>umm510</author>
<category>fiction</category>
</b1:book>
</bookstore>
How can I query this xml file using Javascript to only display data from tags WHERE category = fiction?????
Here is some current sample code which does not work:
<html>
<body>
<script type="text/javascript">
function loadXMLDoc(dname)
{
if (window.XMLHttpRequest)
{
xhttp=new XMLHttpRequest();
}
else
{
xhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xhttp.open("GET",dname,false);
xhttp.send("");
return xhttp.responseXML;
}
xml=loadXMLDoc("bookstore.xml");
var name = "/bookstore/b1:book/name";
var category = "/bookstore/b1:book/category";
if (typeof xml.evaluate !== 'undefined')
{
//Lets assume I have filled in all the required checks for URL's and namespaces for prefix b1
//Using nodes for name and nodes2 for category
var name=nodes.iterateNext();
var category=nodes2.iterateNext();
while(category.childNodes[0].nodeValue=="fiction")
{
document.write(name.childNodes[0].nodeValue);
}
}
else if (typeof xml.selectNodes !== 'undefined' && typeof xml.setProperty != 'undefined')
{
//IE Part which I'm not fussed about as I'll just convert from firefox implementation
}
</script>
</body>
</html>
Edited Extra:
<?xml version="1.0" encoding="UTF-8"?>
<!--Terminal Area Forecast (TAF) Transformation to WXXM Copyright 2010 La Trobe University - AirServices Australia project. All rights reserved.--><asa:AustralianTAFProduct xsi:schemaLocation="http://www.airservicesaustralia.com.au C:/wxxm/AustralianProductsSchemas/AustralianTAFproduct.xsd" gml:id="ID_46c691d5-aa88-4ca6-9879-1e507f065b32" xmlns:uuid="xalan://java.util.UUID" xmlns:gco="http://www.isotc211.org/2005/gco" xmlns:gmd="http://www.isotc211.org/2005/gmd" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:wx="http://www.eurocontrol.int/wx/1.1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:om="http://www.opengis.net/om/1.0/gml32" xmlns:fn="http://www.w3.org/2005/xpath-functions" xmlns:avwx="http://www.eurocontrol.int/avwx/1.1" xmlns:gml="http://www.opengis.net/gml/3.2" xmlns:asa="http://www.airservicesaustralia.com.au">
<avwx:rawText>TAF YMGB 180015Z 1802/1811
11010KT 9999 SCT025
RMK
T 30 32 29 28 Q 1011 1009 1008 1011
</avwx:rawText>
<!--Forecast Items-->
<!--WXXM Schema {avwx:aerodromeWxForecast} feature-->
<avwx:aerodromeWxForecast>
<wx:Forecast gml:id="ID_58c3f598-575c-45c5-8988-7a5146864292">
<om:samplingTime>
<gml:TimePeriod gml:id="ID_f3bdffee-a2c6-4d05-8097-e32943c1a5f8">
<gml:beginPosition>2010-12-18T 02:00:00Z</gml:beginPosition>
<gml:endPosition>2010-12-18T 11:00:00Z</gml:endPosition>
</gml:TimePeriod>
</om:samplingTime>
<om:procedure/>
<om:observedProperty xlink:href="http://www.eurocontrol.int/ont/avwx/1.1/wx.owl#avwx:AerodromeWxForecast"/>
<om:featureOfInterest xlink:href="#ID_d5dbad50-401d-442a-b74c-58a58758d6fc"/>
<om:parameter/>
<om:result>
<avwx:AerodromeWxForecast gml:id="ID_9449d9d2-601c-4187-8ff5-e25feaf815e4">
<wx:validTime>
<gml:TimePeriod gml:id="ID_72b7a40b-c57b-47d6-88af-94ffbe6aaab4">
<gml:beginPosition>2010-12-18T 02:00:00Z</gml:beginPosition>
<gml:endPosition>2010-12-18T 11:00:00Z</gml:endPosition>
</gml:TimePeriod>
</wx:validTime>
<avwx:windDirection uom="deg">110</avwx:windDirection>
<avwx:horizontalVisibility>
<avwx:HorizontalVisibility gml:id="ID_0738e74a-2b6e-4e7f-9e7b-2cf5456a18b3">
<avwx:minimumVisibility uom="km">10</avwx:minimumVisibility>
</avwx:HorizontalVisibility>
</avwx:horizontalVisibility>
<avwx:windSpeed uom="kt">10</avwx:windSpeed>
<avwx:cloudCondition>
<wx:CloudCondition gml:id="ID_69351345-8054-411e-9db5-87f365982775">
<wx:base uom="ft">2500</wx:base>
<wx:cloudAmount>SCATTERED</wx:cloudAmount>
</wx:CloudCondition>
</avwx:cloudCondition>
<!--End of Forecast Items-->
</avwx:AerodromeWxForecast>
</om:result>
</wx:Forecast>
</avwx:aerodromeWxForecast>
<!--Trend Condition Items-->
<!--In support of Australian Significant Changes and Variations as described in the Bureau of Meteorology Aeronautical Services Handbook InterTempoChanges GradualChanges SignificantChanges PoorVisibilityChanges-->
<!--Temperature and QNH Trend Forecasts-->
<!--In support of Australian Temperature and QNH Interval Forecasts as described in the Bureau of Meteorology Aeronautical Services Handbook-->
<!--Valid Period Start -->
<!--Valid Period Start plus three hours-->
<!--Valid Period Start plus six hours-->
<!--Valid Period Start plus nine hours-->
<!--This concept does not exist in WXXM however a mapping was be made to {aerodromeWxForecast} feature-->
<avwx:aerodromeWxForecast>
<wx:Forecast gml:id="ID_b128bee2-e4e6-4463-9739-ea84df97caeb">
<om:samplingTime>
<gml:TimePeriod gml:id="ID_8c240bbb-5e96-47f9-8be3-3ccc3bf143e6">
<gml:beginPosition>2010-12-18T 02:00:00Z</gml:beginPosition>
<gml:endPosition>2010-12-18T 05:00:00Z</gml:endPosition>
</gml:TimePeriod>
</om:samplingTime>
<om:procedure/>
<om:observedProperty xlink:href="http://www.eurocontrol.int/ont/avwx/1.1/wx.owl#avwx:TREND"/>
<om:featureOfInterest xlink:href="#ID_d5dbad50-401d-442a-b74c-58a58758d6fc"/>
<om:result>
<avwx:TREND gml:id="ID_bc50099f-45a6-4348-8294-964d20a3003c">
<avwx:forecast>
<wx:Forecast gml:id="ID_3cf3d5a8-cf3d-4d7f-90f9-f68f6afbfbbf">
<om:samplingTime/>
<om:procedure/>
<om:observedProperty/>
<om:featureOfInterest/>
<om:parameter/>
<om:result>
<wx:validTime>
<gml:TimePeriod gml:id="ID_f8b9fa36-783b-4c88-9e63-244d89cc7d1e">
<gml:beginPosition>2010-12-18T 02:00:00Z</gml:beginPosition>
<gml:endPosition>2010-12-18T 05:00:00Z</gml:endPosition>
</gml:TimePeriod>
</wx:validTime>
<avwx:windDirection uom="deg">160</avwx:windDirection>
<avwx:windSpeed uom="kt">30</avwx:windSpeed>
<avwx:airTemperature>30</avwx:airTemperature>
<avwx:qnh>1011</avwx:qnh>
<avwx:cloudCondition>
<wx:CloudCondition gml:id="ID_69351345-8054-411e-9db5-87f365982775">
<wx:base uom="ft">2500</wx:base>
<wx:cloudAmount>SCATTERED</wx:cloudAmount>
</wx:CloudCondition>
</avwx:cloudCondition>
</om:result>
</wx:Forecast>
</avwx:forecast>
</avwx:TREND>
</om:result>
</wx:Forecast>
</avwx:aerodromeWxForecast>
<avwx:aerodromeWxForecast>
<wx:Forecast gml:id="ID_19df8738-be93-4c1a-af9f-d818f6e058c6">
<om:samplingTime>
<gml:TimePeriod gml:id="ID_37f26807-9497-4fdd-8cf7-96fc800a3b48">
<gml:beginPosition>2010-12-18T 05:00:00Z</gml:beginPosition>
<gml:endPosition>2010-12-18T 08:00:00Z</gml:endPosition>
</gml:TimePeriod>
</om:samplingTime>
<om:procedure/>
<om:observedProperty xlink:href="http://www.eurocontrol.int/ont/avwx/1.1/wx.owl#avwx:TREND"/>
<om:featureOfInterest xlink:href="#ID_d5dbad50-401d-442a-b74c-58a58758d6fc"/>
<om:result>
<avwx:TREND gml:id="ID_85f3b9ea-21d1-4237-ac8b-4767bf33cc32">
<avwx:forecast>
<wx:Forecast gml:id="ID_968fdbcf-3129-48f0-8b0d-331dc2185bdf">
<om:samplingTime/>
<om:procedure/>
<om:observedProperty/>
<om:featureOfInterest/>
<om:parameter/>
<om:result>
<wx:validTime>
<gml:TimePeriod gml:id="ID_f211f02d-83fb-407b-b0c7-d06669fa148c">
<gml:beginPosition>2010-12-18T 05:00:00Z</gml:beginPosition>
<gml:endPosition>2010-12-18T 08:00:00Z</gml:endPosition>
</gml:TimePeriod>
</wx:validTime>
<avwx:airTemperature>32</avwx:airTemperature>
<avwx:qnh>1009</avwx:qnh>
<avwx:windDirection uom="deg">170</avwx:windDirection>
<avwx:windSpeed uom="kt">25</avwx:windSpeed>
<avwx:cloudCondition>
<wx:CloudCondition gml:id="ID_69351345-8054-411e-9db5-87f365982775">
<wx:base uom="ft">3100</wx:base>
<wx:cloudAmount>SCATTERED</wx:cloudAmount>
</wx:CloudCondition>
</avwx:cloudCondition>
</om:result>
</wx:Forecast>
</avwx:forecast>
</avwx:TREND>
</om:result>
</wx:Forecast>
</avwx:aerodromeWxForecast>
<avwx:aerodromeWxForecast>
<wx:Forecast gml:id="ID_67ea2cd9-5f3a-4837-994e-26fc216cac8b">
<om:samplingTime>
<gml:TimePeriod gml:id="ID_8d662056-a8e6-416c-a3e1-7765aef94a85">
<gml:beginPosition>2010-12-18T 08:00:00Z</gml:beginPosition>
<gml:endPosition>2010-12-18T 11:00:00Z</gml:endPosition>
</gml:TimePeriod>
</om:samplingTime>
<om:procedure/>
<om:observedProperty xlink:href="http://www.eurocontrol.int/ont/avwx/1.1/wx.owl#avwx:TREND"/>
<om:featureOfInterest xlink:href="#ID_d5dbad50-401d-442a-b74c-58a58758d6fc"/>
<om:result>
<avwx:TREND gml:id="ID_2386470b-d17b-4c38-9718-5cdb44075550">
<avwx:forecast>
<wx:Forecast gml:id="ID_44a09991-25d1-423d-a994-4b62c256272a">
<om:samplingTime/>
<om:procedure/>
<om:observedProperty/>
<om:featureOfInterest/>
<om:parameter/>
<om:result>
<wx:validTime>
<gml:TimePeriod gml:id="ID_f59bec03-a4f0-4493-b50a-48a03db7a1d2">
<gml:beginPosition>2010-12-18T 08:00:00Z</gml:beginPosition>
<gml:endPosition>2010-12-18T 11:00:00Z</gml:endPosition>
</gml:TimePeriod>
</wx:validTime>
<avwx:airTemperature>29</avwx:airTemperature>
<avwx:qnh>1008</avwx:qnh>
<avwx:windDirection uom="deg">180</avwx:windDirection>
<avwx:windSpeed uom="kt">41</avwx:windSpeed>
<avwx:cloudCondition>
<wx:CloudCondition gml:id="ID_69351345-8054-411e-9db5-87f365982775">
<wx:base uom="ft">2200</wx:base>
<wx:cloudAmount>SCATTERED</wx:cloudAmount>
</wx:CloudCondition>
</avwx:cloudCondition>
</om:result>
</wx:Forecast>
</avwx:forecast>
</avwx:TREND>
</om:result>
</wx:Forecast>
</avwx:aerodromeWxForecast>
<avwx:aerodromeWxForecast>
<wx:Forecast gml:id="ID_c0f73f45-53c9-4e0a-b053-030b641fb978">
<om:samplingTime>
<gml:TimePeriod gml:id="ID_ff2336d7-aec7-48dd-b21e-09f9fc18af2a">
<gml:beginPosition>2010-12-18T 11:00:00Z</gml:beginPosition>
<gml:endPosition>2010-12-18T 14:00:00Z</gml:endPosition>
</gml:TimePeriod>
</om:samplingTime>
<om:procedure/>
<om:observedProperty xlink:href="http://www.eurocontrol.int/ont/avwx/1.1/wx.owl#avwx:TREND"/>
<om:featureOfInterest xlink:href="#ID_d5dbad50-401d-442a-b74c-58a58758d6fc"/>
<om:result>
<avwx:TREND gml:id="ID_eb92fcee-ea8b-4d9e-af96-c60a8747df70">
<avwx:forecast>
<wx:Forecast gml:id="ID_c54eaedc-a082-4cb9-acca-2951dafd4855">
<om:samplingTime/>
<om:procedure/>
<om:observedProperty/>
<om:featureOfInterest/>
<om:parameter/>
<om:result>
<wx:validTime>
<gml:TimePeriod gml:id="ID_9828228e-8152-4c30-8107-7e1a86974a64">
<gml:beginPosition>2010-12-18T 11:00:00Z</gml:beginPosition>
<gml:endPosition>2010-12-18T 14:00:00Z</gml:endPosition>
</gml:TimePeriod>
</wx:validTime>
<avwx:airTemperature>28</avwx:airTemperature>
<avwx:qnh>1011</avwx:qnh>
<avwx:windDirection uom="deg">190</avwx:windDirection>
<avwx:windSpeed uom="kt">20</avwx:windSpeed>
<avwx:cloudCondition>
<wx:CloudCondition gml:id="ID_69351345-8054-411e-9db5-87f365982775">
<wx:base uom="ft">1500</wx:base>
<wx:cloudAmount>SCATTERED</wx:cloudAmount>
</wx:CloudCondition>
</avwx:cloudCondition>
</om:result>
</wx:Forecast>
</avwx:forecast>
</avwx:TREND>
</om:result>
</wx:Forecast>
</avwx:aerodromeWxForecast>
<!--End Temperature and QNH Trend Forecasts-->
<!--End of Trend Condition Items-->
<avwx:appliesTo>
<avwx:Aerodrome gml:id="ID_d5dbad50-401d-442a-b74c-58a58758d6fc">
<gml:name>MANSFIELD</gml:name>
<gml:location>
<gml:Point gml:id="ID_a85a0f15-deb9-4d6d-96d1-4818fafc3556">
<gml:coordinates>134.893530555556,-12.0944666666667</gml:coordinates>
</gml:Point>
</gml:location>
<avwx:icaoCode>YMGB</avwx:icaoCode>
<!--Integration feature with AIXM-->
</avwx:Aerodrome>
</avwx:appliesTo>
<avwx:issueTime>2010-12-18T00:15:00Z</avwx:issueTime>
<avwx:type>TAF</avwx:type>
<asa:AFTNheader>CBJ0338 180015
GG YBZZABEX
180015 YPDMYMYX</asa:AFTNheader>
<asa:stationID/>
<!--The Australian TAF product extension items-->
//NEWLY ADDED
<avwx:aerodromeWxForecast>
<wx:Forecast gml:id="ID_c0f73f45-53c9-4e0a-b053-030b641fb979">
<om:samplingTime>
<gml:TimePeriod gml:id="ID_ff2336d7-aec7-48dd-b21e-09f9fc18af2b">
<gml:beginPosition>2010-12-18T 11:00:00Z</gml:beginPosition>
<gml:endPosition>2010-12-18T 14:00:00Z</gml:endPosition>
</gml:TimePeriod>
</om:samplingTime>
<om:procedure/>
<om:observedProperty xlink:href="http://www.eurocontrol.int/ont/avwx/1.1/wx.owl#avwx:TREND"/>
<om:featureOfInterest xlink:href="#ID_d5dbad50-401d-442a-b74c-58a58758d6fd"/>
<om:result>
<avwx:TREND gml:id="ID_eb92fcee-ea8b-4d9e-af96-c60a8747df70">
<avwx:forecast>
<wx:Forecast gml:id="ID_c54eaedc-a082-4cb9-acca-2951dafd4856">
<om:samplingTime/>
<om:procedure/>
<om:observedProperty/>
<om:featureOfInterest/>
<om:parameter/>
<om:result>
<wx:validTime>
<gml:TimePeriod gml:id="ID_9828228e-8152-4c30-8107-7e1a86974a65">
<gml:beginPosition>2010-12-18T 11:00:00Z</gml:beginPosition>
<gml:endPosition>2010-12-18T 14:00:00Z</gml:endPosition>
</gml:TimePeriod>
</wx:validTime>
<avwx:airTemperature>28</avwx:airTemperature>
<avwx:qnh>1011</avwx:qnh>
<avwx:windDirection uom="deg">200</avwx:windDirection>
<avwx:windSpeed uom="kt">20</avwx:windSpeed>
<avwx:cloudCondition>
<wx:CloudCondition gml:id="ID_69351345-8054-411e-9db5-87f365982776">
<wx:base uom="ft">1500</wx:base>
<wx:cloudAmount>SCATTERED</wx:cloudAmount>
</wx:CloudCondition>
</avwx:cloudCondition>
</om:result>
</wx:Forecast>
</avwx:forecast>
</avwx:TREND>
</om:result>
</wx:Forecast>
</avwx:aerodromeWxForecast>
<!--End Temperature and QNH Trend Forecasts-->
<!--End of Trend Condition Items-->
<avwx:appliesTo>
<avwx:Aerodrome gml:id="ID_d5dbad50-401d-442a-b74c-58a58758d6fd">
<gml:name>BIRCHIP</gml:name>
<gml:location>
<gml:Point gml:id="ID_a85a0f15-deb9-4d6d-96d1-4818fafc3558">
<gml:coordinates>134.893530555556,-12.0944666666667</gml:coordinates>
</gml:Point>
</gml:location>
<avwx:icaoCode>YBIR</avwx:icaoCode>
<!--Integration feature with AIXM-->
</avwx:Aerodrome>
</avwx:appliesTo>
<avwx:issueTime>2010-12-18T00:15:00Z</avwx:issueTime>
<avwx:type>TAF</avwx:type>
<asa:AFTNheader>CBJ0338 180015
GG YBZZABEX
180015 YPDMYMYX</asa:AFTNheader>
<asa:stationID/>
</asa:AustralianTAFProduct>
How can I extract data from "/asa:AustralianTAFProduct//wx:Forecast/om:samplingTime/gml:TimePeriod/gml:beginPosition" only if "/asa:AustralianTAFProduct//gml:name" = MANSFIELD???? How do I enforce this condition? Note that there are 2 gml:name tags - One = MANSFIELD and the other = BIRCHIP
New XML data is under the comment //NEWLY ADDED
I have used timeBegin="/asa:AustralianTAFProduct[.//gml:name=\"BIRCHIP\"]//wx:Forecast/om:samplingTime/gml:TimePeriod/gml:beginPosition";
as xquib-team suggested, but this returns the time value for BOTH MANSFIELD and BIRCHIP :(
Any further suggestions???
gml:name is towards the end of the XML document, if you can't find it. I'm sorry I uploaded the large XML file, but it makes my explaining much easier
Any help would be greatly appreciated!!!!! Note: xqib-team's answer worked perfectly for the XML fragment before I renewed it! Excellent work guys! I sincerely appreciate it <3
Upvotes: 1
Views: 491
Reputation: 7279
Here is an attempt at your second question, without using IDs, assuming there is one name for the entire document. The following XPath
/asa:AustralianTAFProduct[.//gml:name="MANSFIELD"]//wx:Forecast/om:samplingTime/gml:TimePeriod/gml:beginPosition
will either return all nodes in:
/asa:AustralianTAFProduct//wx:Forecast/om:samplingTime/gml:TimePeriod/gml:beginPosition
if the following is true:
/asa:AustralianTAFProduct//gml:name="MANSFIELD"
or otherwise it will return an empty sequence.
Upvotes: 0
Reputation: 7279
The following XPath expressions selects all names corresponding to a "fiction" category:
/bookstore/b1:book[category="fiction"]/name
Note that the document above is not namespace-well-formed, because the prefix b1 is not bound to a namespace. This might be a reason for your code not to work. Here is how it could be fixed:
<bookstore xmlns:b1="http://www.example.com">
<b1:book>
<name>Hungry Teddy</name>
<author>Bozo</author>
<category>fiction</category>
</b1:book>
<b1:book>
<name>Funny Kangaroo</name>
<author>Bozo</author>
<category>non-fiction</category>
</b1:book>
<b1:book>
<name>How to JAVA</name>
<author>umm510</author>
<category>fiction</category>
</b1:book>
</bookstore>
Edit: There were also well-formedness errors in some author tags.
Upvotes: 1