Anonymous user
XML/XPath: Difference between revisions
m
Undo revision 292660 by Dcsobral (talk) broke formatting
(→{{header|Scala}}: Replace with code that follow XPath semantics and the problem description more closely) |
|||
Line 2,771:
| </inventory>
scala> val firstItem =
| firstSection <- (xml \ "section").headOption
firstItem: scala.xml.NodeSeq =▼
| firstItem <- (firstSection \ "item").headOption
NodeSeq(<item upc="123456789" stock="12">▼
| } yield firstItem
<name>Invisibility Cream</name>▼
<price>14.50</price>▼
<description>Makes you invisible</description>▼
▲ <name>Invisibility Cream</name>
</item>)▼
▲ <price>14.50</price>
▲ <description>Makes you invisible</description>
▲ </item>)
scala>
| section <- (xml \ "section")
| item <- (section \ "item")
| } yield scala.math.BigDecimal(price.text)
prices: List[scala.math.BigDecimal] = List(14.50, 23.99, 4.95, 3.56)
scala> val
salesTax: scala.math.BigDecimal = 2.3500
names: Array[scala.xml.Node] = Array(<name>Invisibility Cream</name>, <name>Levitation Salve</name>, <name>Blork and Freen Instameal</name>, <name>Grob winglets</name>)▼
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[
=={{header|Sidef}}==
|