Jump to content

XML/XPath: Difference between revisions

→‎{{header|Scala}}: Replace with code that follow XPath semantics and the problem description more closely
m (→‎{{header|Nim}}: Use the correct template)
(→‎{{header|Scala}}: Replace with code that follow XPath semantics and the problem description more closely)
Line 2,771:
| </inventory>
 
scala> val firstItem = forxml {\\ "item" take 1
firstItem: Option[scala.xml.Node]NodeSeq =
| firstSection <- (xml \ "section").headOption
SomeNodeSeq(<item upc="123456789" stock="12">
| firstItem <- (firstSection \ "item").headOption
<name>Invisibility Cream</name>
| } yield firstItem
<price>14.50</price>
firstItem: Option[scala.xml.Node] =
<description>Makes you invisible</description>
Some(<item upc="123456789" stock="12">
</item>)
<name>Invisibility Cream</name>
<price>14.50</price>
<description>Makes you invisible</description>
</item>)
 
scala> valxml prices\\ ="price" forforeach {println
<price>14.50</price>
| section <- (xml \ "section")
<price>23.99</price>
| item <- (section \ "item")
| <price >4.95<- (item \ "/price")>
<price>3.56</price>
| } yield scala.math.BigDecimal(price.text)
prices: List[scala.math.BigDecimal] = List(14.50, 23.99, 4.95, 3.56)
 
scala> val salesTaxnames = prices.sum(xml *\\ 0"name").05toArray
names: Array[Stringscala.xml.Node] = Array(<name>Invisibility Cream</name>, <name>Levitation Salve</name>, <name>Blork and Freen Instameal</name>, <name>Grob winglets)</langname>)
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)</lang>
 
=={{header|Sidef}}==
Anonymous user
Cookies help us deliver our services. By using our services, you agree to our use of cookies.