changeset 4:e70e06b19b1a

~ hide Saxon behind Scala XML
author Alexandre Bertails <bertails@w3.org>
date Wed, 24 Feb 2010 16:45:30 -0500
parents 1133234bce9a
children 6c3105ad43ee
files src/main/scala/Main.scala
diffstat 1 files changed, 14 insertions(+), 6 deletions(-) [+]
line wrap: on
line diff
--- a/src/main/scala/Main.scala	Wed Feb 24 15:24:25 2010 -0500
+++ b/src/main/scala/Main.scala	Wed Feb 24 16:45:30 2010 -0500
@@ -5,6 +5,7 @@
 import net.sf.saxon.s9api._
 import javax.xml.transform.stream.StreamSource
 import scala.collection.JavaConversions.JIteratorWrapper
+import scala.xml._
 
 object Document {
 
@@ -92,16 +93,23 @@
   }
 
   /**
-   * evaluates an xpath expression against a document
-   * TODO
+   * tranform a Saxon XdmItem into a Scala Elem
    */
-  def evaluateSingle(doc:XdmNode, xpath:String):Option[XdmItem] = Option(getXPathSelector(doc, xpath).evaluateSingle())
+  def XdmItem2Elem(item:XdmItem):Elem = XML.load(new StringReader(item.toString))
 
   /**
    * evaluates an xpath expression against a document
    * TODO
    */
-  def evaluate(doc:XdmNode, xpath:String):Iterator[XdmItem] = JIteratorWrapper(getXPathSelector(doc, xpath).iterator())
+  def evaluateSingle(doc:XdmNode, xpath:String):Option[Elem] =
+    Option(getXPathSelector(doc, xpath).evaluateSingle()) flatMap (item => Some(XdmItem2Elem(item)))
+
+  /**
+   * evaluates an xpath expression against a document
+   * TODO
+   */
+  def evaluate(doc:XdmNode, xpath:String):Iterator[Elem] =
+    JIteratorWrapper(getXPathSelector(doc, xpath).iterator()) map (XdmItem2Elem(_))
 
 }
 
@@ -109,8 +117,8 @@
 
   private val doc:XdmNode = Document.convertUrlToXdmNode(url)
 
-  def evaluateSingle(xpath:String):Option[XdmItem] = Document.evaluateSingle(doc, xpath)
+  def evaluateSingle(xpath:String):Option[Elem] = Document.evaluateSingle(doc, xpath)
 
-  def evaluate(xpath:String):Iterator[XdmItem] = Document.evaluate(doc, xpath)
+  def evaluate(xpath:String):Iterator[Elem] = Document.evaluate(doc, xpath)
 
 }
Set up and maintained by W3C Systems Team, please report bugs to sysreq@w3.org.

W3C would like to thank Microsoft who donated the server that allows us to run this service.