xquery version "1.0"; (: XQuery script to format SPARQL Variable Results XML Format as xhtml Copyright © 2004 World Wide Web Consortium, (Massachusetts Institute of Technology, European Research Consortium for Informatics and Mathematics, Keio University). All Rights Reserved. This work is distributed under the W3C® Software License [1] in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. [1] http://www.w3.org/Consortium/Legal/2002/copyright-software-20021231 :) declare namespace res="http://www.w3.org/2001/sw/DataAccess/rf1/result"; declare namespace default="http://www.w3.org/1999/xhtml"; (: How to set serialization parameters? :) (: doctype-system = "-//W3C//DTD XHTML 1.0 Transitional//EN" :) (: doctype-public = "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> :) declare variable $variableNames { for $element in //res:sparql/res:head/res:variable return string($element/@name) }; document { SPARQL Variable Binding Results to XHTML table (XQuery)

SPARQL Variable Binding Results to XHTML table (XQuery)

{ for $name in $variableNames return } { for $result in //res:sparql/res:results/* return { for $name in $variableNames let $item := $result/*[name() = $name] return } }
{$name}
{ if ($item/@bnodeid) then (: blank node value :) text { "nodeID", string($item/@bnodeid) } else if ($item/@uri) then (: URI value :) text { "URI", string($item/@uri) } else if ($item/@datatype) then (: datatyped literal value :) text { $item/text(), "(datatype", string($item/@datatype), ")" } else if ($item/@xml:lang) then (: lang-string :) text { $item/text(), "@", string($item/@xml:lang) } else if ($item/@bound = 'false') then (: unbound variable - empty cell :) "[unbound]" else if ( exists($item/text()) ) then (: present and not empty :) $item/text() else if ( exists($item) ) then (: present and empty :) "[empty]" else (: unbound variable - empty cell :) "[unbound]" }
}