- About Scala
- Documentation
- Code Examples
- Software
- Scala Developers
XML and namespace
Tue, 2012-02-14, 18:22
hi,
I would like to learn more about namespaced XML processing. It seems that it is not much supported in scala.xml._ (or I don't know how).
Let's say I have this little file test.xml:
<a xmlns="http://graphml.graphdrawing.org/xmlns" xmlns:x="urn:x">
<b>b1</b>
<x:b>b2</x:b>
<b>b3</b>
</a>
scala> val x = xml.XML.loadFile("test.xml")
x: scala.xml.Elem = ...
When I query all "b" elements it returns not just the "b" elemens, but also the "x:b" which it shouldn't:
scala> x \\ "b"
res9: scala.xml.NodeSeq = <b xmlns:x="urn:x" xmlns="http://graphml.graphdrawing.org/xmlns">b1</b><x:b xmlns:x="urn:x" xmlns="http://graphml.graphdrawing.org/xmlns">b2</x:b><b xmlns:x="urn:x" xmlns="http://graphml.graphdrawing.org/xmlns">b3</b>
On the other hand when I query "x:b" it returns nothing
scala> x \\ "x:b"
res8: scala.xml.NodeSeq =
I am sure there are already answers to that question in the internet. It would be nice if someone could point me to helpful resources and/or solve the mini problem from above.
Thanks,
Immanuel
I would like to learn more about namespaced XML processing. It seems that it is not much supported in scala.xml._ (or I don't know how).
Let's say I have this little file test.xml:
<a xmlns="http://graphml.graphdrawing.org/xmlns" xmlns:x="urn:x">
<b>b1</b>
<x:b>b2</x:b>
<b>b3</b>
</a>
scala> val x = xml.XML.loadFile("test.xml")
x: scala.xml.Elem = ...
When I query all "b" elements it returns not just the "b" elemens, but also the "x:b" which it shouldn't:
scala> x \\ "b"
res9: scala.xml.NodeSeq = <b xmlns:x="urn:x" xmlns="http://graphml.graphdrawing.org/xmlns">b1</b><x:b xmlns:x="urn:x" xmlns="http://graphml.graphdrawing.org/xmlns">b2</x:b><b xmlns:x="urn:x" xmlns="http://graphml.graphdrawing.org/xmlns">b3</b>
On the other hand when I query "x:b" it returns nothing
scala> x \\ "x:b"
res8: scala.xml.NodeSeq =
I am sure there are already answers to that question in the internet. It would be nice if someone could point me to helpful resources and/or solve the mini problem from above.
Thanks,
Immanuel
On Tue, Feb 14, 2012 at 6:22 PM, Immanuel Normann
wrote:
> hi,
> I would like to learn more about namespaced XML processing. It seems that it
> is not much supported in scala.xml._ (or I don't know how).
>
> Let's say I have this little file test.xml:
>
>
> b1
> b2
> b3
>
>
> scala> val x = xml.XML.loadFile("test.xml")
> x: scala.xml.Elem = ...
>
> When I query all "b" elements it returns not just the "b" elemens, but also
> the "x:b" which it shouldn't:
>
> scala> x \\ "b"
> res9: scala.xml.NodeSeq = xmlns="http://graphml.graphdrawing.org/xmlns">b1 xmlns="http://graphml.graphdrawing.org/xmlns">b2 xmlns="http://graphml.graphdrawing.org/xmlns">b3
>
> On the other hand when I query "x:b" it returns nothing
>
> scala> x \\ "x:b"
> res8: scala.xml.NodeSeq =
>
> I am sure there are already answers to that question in the internet. It
> would be nice if someone could point me to helpful resources and/or solve
> the mini problem from above.
>
> Thanks,
> Immanuel
>
Scales does the "right thing" by default:
val p = x.asScala
(p \* "b").size // 0
boolean( p \* "b" ) // false - XPath notion of boolean
val ns = Namespace("http://graphml.graphdrawing.org/xmlns")
boolean(p \* ns("b")) // true
(p \* ns("b")).size // 2
val nsx = Namespace("urn:x")
(p \* nsx("b")).size // 1
That said to query properly in Scala XML you only have to filter the nodeseq:
x \\ "b" filter ( z => z.namespace == "http://graphml.graphdrawing.org/xmlns")