XML/XPath: Difference between revisions
Content added Content deleted
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: | Line 2,771: | ||
| </inventory> |
| </inventory> |
||
scala> val firstItem = |
scala> val firstItem = xml \\ "item" take 1 |
||
⚫ | |||
| firstSection <- (xml \ "section").headOption |
|||
⚫ | |||
| firstItem <- (firstSection \ "item").headOption |
|||
⚫ | |||
| } yield firstItem |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
scala> |
scala> xml \\ "price" foreach println |
||
<price>14.50</price> |
|||
| section <- (xml \ "section") |
|||
<price>23.99</price> |
|||
| item <- (section \ "item") |
|||
<price>4.95</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 |
scala> val names = (xml \\ "name").toArray |
||
⚫ | |||
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 |
|||
⚫ | |||
=={{header|Sidef}}== |
=={{header|Sidef}}== |