Jump to content

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)
m (Undo revision 292660 by Dcsobral (talk) broke formatting)
Line 2,771:
| </inventory>
 
scala> val firstItem = xmlfor \\ "item" take 1{
| firstSection <- (xml \ "section").headOption
firstItem: scala.xml.NodeSeq =
| firstItem <- (firstSection \ "item").headOption
NodeSeq(<item upc="123456789" stock="12">
| } yield firstItem
<name>Invisibility Cream</name>
firstItem: Option[scala.xml.NodeSeqNode] =
<price>14.50</price>
NodeSeqSome(<item upc="123456789" stock="12">
<description>Makes you invisible</description>
<name>Invisibility Cream</name>
</item>)
<price>14.50</price>
<description>Makes you invisible</description>
</item>)
 
scala> xmlval \\prices "price"= foreachfor println{
| section <- (xml \ "section")
<price>14.50</price>
| item <- (section \ "item")
<price>23.99</price>
< | price>4.95 </- (item \ "price>")
| } yield scala.math.BigDecimal(price.text)
<price>3.56</price>
prices: List[scala.math.BigDecimal] = List(14.50, 23.99, 4.95, 3.56)
 
scala> val namessalesTax = (xmlprices.sum \\* "name")0.toArray05
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[scala.xml.NodeString] = Array(<name>Invisibility Cream</name>, <name>Levitation Salve</name>, <name>Blork and Freen Instameal</name>, <name>Grob winglets)</namelang>)
 
=={{header|Sidef}}==
Anonymous user
Cookies help us deliver our services. By using our services, you agree to our use of cookies.