Family Day update
This commit is contained in:
parent
aac6731f2c
commit
9ad63ea473
2442 changed files with 39761 additions and 8255 deletions
|
|
@ -1,7 +1,7 @@
|
|||
Perform the following three XPath queries on the XML Document below:
|
||||
* Retrieve the first "item" element
|
||||
* Perform an action on each "price" element (print it out)
|
||||
* Get an array of all the "name" elements
|
||||
* '''//item[1]''': Retrieve the first "item" element
|
||||
* '''//price/text()''': Perform an action on each "price" element (print it out)
|
||||
* '''//name''': Get an array of all the "name" elements
|
||||
|
||||
XML Document:
|
||||
<inventory title="OmniCorp Store #45x10^3">
|
||||
|
|
|
|||
|
|
@ -26,33 +26,19 @@ scala> val xml: scala.xml.Elem =
|
|||
| </section>
|
||||
| </inventory>
|
||||
|
||||
scala> val firstItem = for {
|
||||
| firstSection <- (xml \ "section").headOption
|
||||
| firstItem <- (firstSection \ "item").headOption
|
||||
| } yield firstItem
|
||||
firstItem: Option[scala.xml.Node] =
|
||||
Some(<item upc="123456789" stock="12">
|
||||
<name>Invisibility Cream</name>
|
||||
<price>14.50</price>
|
||||
<description>Makes you invisible</description>
|
||||
</item>)
|
||||
scala> val firstItem = xml \\ "item" take 1
|
||||
firstItem: scala.xml.NodeSeq =
|
||||
NodeSeq(<item upc="123456789" stock="12">
|
||||
<name>Invisibility Cream</name>
|
||||
<price>14.50</price>
|
||||
<description>Makes you invisible</description>
|
||||
</item>)
|
||||
|
||||
scala> val prices = for {
|
||||
| section <- (xml \ "section")
|
||||
| item <- (section \ "item")
|
||||
| price <- (item \ "price")
|
||||
| } yield scala.math.BigDecimal(price.text)
|
||||
prices: List[scala.math.BigDecimal] = List(14.50, 23.99, 4.95, 3.56)
|
||||
scala> xml \\ "price" map (_.text) foreach println
|
||||
14.50
|
||||
23.99
|
||||
4.95
|
||||
3.56
|
||||
|
||||
scala> val salesTax = prices.sum * 0.05
|
||||
salesTax: scala.math.BigDecimal = 2.3500
|
||||
|
||||
scala> println(salesTax.setScale(2, BigDecimal.RoundingMode.HALF_UP))
|
||||
2.35
|
||||
|
||||
scala> val names = for {
|
||||
| section <- (xml \ "section").toArray
|
||||
| item <- (section \ "item")
|
||||
| name <- (item \ "name")
|
||||
| } yield name.text
|
||||
names: Array[String] = Array(Invisibility Cream, Levitation Salve, Blork and Freen Instameal, Grob winglets)
|
||||
scala> val names = (xml \\ "name").toArray
|
||||
names: Array[scala.xml.Node] = Array(<name>Invisibility Cream</name>, <name>Levitation Salve</name>, <name>Blork and Freen Instameal</name>, <name>Grob winglets</name>)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue