<?xml version="1.0" encoding="UTF-8"?>
<table xmlns="http://query.yahooapis.com/v1/schema/table.xsd">
    <meta>
        <author>Balaji Narayanan</author>
        <author>Tom Hughes-Croucher</author>
        <description>Placemaker</description>
        <sampleQuery>select * from {table} where documentContent = "They followed him to deepest Africa and found him there, in Timbuktu" and documentType="text/plain" anmd appid = ""</sampleQuery>
        <documentationURL>http://developer.yahoo.com/geo/placemaker/</documentationURL>
    </meta>
    <bindings>

        <select itemPath="" produces="XML">
            <urls>
                <url>http://wherein.yahooapis.com/v1/document</url>
            </urls>
            <inputs>
                <key id="documentURL" type="xs:string" required="true" paramType="variable"/>
                <key id="documentType" type="xs:string" required="true" paramType="variable"/>
                <key id="appid" type="xs:string" required="true" paramType="variable"/>
        		<key id="documentTitle" type="xs:string" paramType="variable"/>
        		<key id="inputLanguage" type="xs:string" paramType="variable" />
        		<key id="autoDisambiguate" type="xs:string" paramType="variable" />
        		<key id="focusWoeId" type="xs:string" paramType="variable" />
            </inputs>
            <execute>
            <![CDATA[
            
            content = "documentURL=" + documentURL + "&documentType=" + documentType + "&appid=" + appid;
    	    if (documentTitle) { content += "&documentTitle=" + documentTitle; }
    	    if (inputLanguage) { content += "&inputLanguage=" + inputLanguage; }
    	    if (autoDisambiguate) { content += "&autoDisambiguate=" + autoDisambiguate; }
    	    if (focusWoeId) { content += "&focusWoeId=" + focusWoeId; }
            
            postResponse = request.accept("application/xml").contentType("application/x-www-form-urlencoded").post(content).response;    
            
            if (postResponse != null) {

                default xml namespace = "http://wherein.yahooapis.com/v1/schema";
                
                matches = Array();
                
                for each (var place in postResponse.document.placeDetails.place) {
                    if (matches[place.woeId]) {
                        matches[place.woeId].place = place;
                    } else {
                        matches[place.woeId] = Array();
                        matches[place.woeId].place = place;
                    }
                }
                for each (var reference in postResponse.document.referenceList.reference) {
                    if (matches[reference.woeIds]) {
                        matches[reference.woeIds].reference = reference;
                    } else {
                        matches[reference.woeIds] = Array();
                        matches[reference.woeIds].reference = reference;
                    }
                }
                default xml namespace = "";
            
                var output = <matches></matches>;

                for each (var match in matches) {
                    var matchXML = <match></match>;
                    matchXML.node += match.place;
                    matchXML.node += match.reference;
                
                    output.node += matchXML;
                }

                response.object = output;
            
            }

            ]]>
            </execute>
        </select> 
        <select itemPath="" produces="XML">

            <urls>
                <url>http://wherein.yahooapis.com/v1/document</url>
            </urls>
            <inputs>
                <key id="documentContent" type="xs:string" required="true" paramType="variable"/>
                <key id="documentType" type="xs:string" required="true" paramType="variable"/>
                <key id="appid" type="xs:string" required="true" paramType="variable"/>
        		<key id="documentTitle" type="xs:string" paramType="variable"/>
        		<key id="inputLanguage" type="xs:string" paramType="variable" />
        		<key id="autoDisambiguate" type="xs:string" paramType="variable" />
        		<key id="focusWoeId" type="xs:string" paramType="variable" />
	    </inputs>
            <execute>
            <![CDATA[
            
            content = "documentContent=" + documentContent + "&documentType=" + documentType + "&appid=" + appid;
            if (documentTitle) { content += "&documentTitle=" + documentTitle; }
    	    if (inputLanguage) { content += "&inputLanguage=" + inputLanguage; }
    	    if (autoDisambiguate) { content += "&autoDisambiguate=" + autoDisambiguate; }
    	    if (focusWoeId) { content += "&focusWoeId=" + focusWoeId; }
            
            postResponse = request.accept("application/xml").contentType("application/x-www-form-urlencoded").post(content).response;    
            
            if (postResponse != null) {

                default xml namespace = "http://wherein.yahooapis.com/v1/schema";
                
                matches = Array();
                
                for each (var place in postResponse.document.placeDetails.place) {
                    if (matches[place.woeId]) {
                        matches[place.woeId].place = place;
                    } else {
                        matches[place.woeId] = Array();
                        matches[place.woeId].place = place;
                    }
                }
                for each (var reference in postResponse.document.referenceList.reference) {
                    if (matches[reference.woeIds]) {
                        matches[reference.woeIds].reference = reference;
                    } else {
                        matches[reference.woeIds] = Array();
                        matches[reference.woeIds].reference = reference;
                    }
                }
                default xml namespace = "";
            
                var output = <matches></matches>;

                for each (var match in matches) {
                    var matchXML = <match></match>;
                    matchXML.node += match.place;
                    matchXML.node += match.reference;
                
                    output.node += matchXML;
                }

                response.object = output;
            
            }
            ]]>
            </execute>
        </select> 
    </bindings>

</table>
