XPath support with XML

171 views
Skip to first unread message

FarmerChet

unread,
Mar 30, 2011, 10:15:15 AM3/30/11
to gosu-lang
We are using Gosu in house now.

Does Gosu have support for XPath queries within an XML document?

We would like to have a document loaded and be able to use xpath to
access different parts of the document data, such as: '/
elementName[@attrib = someValue]/childElement'

If possible, some of these queries would be put in blocks.

Thanks for any details you can provide.

(By the way, the Gosu documentation is some of the best I have ever
used. Gosu is the first language I have used in 25 years where I just
read the main docs one time and was able to start working. No books
needed, no training needed, and everything seems to work exactly as
documented. Great job all!)

Carson Gross

unread,
Mar 30, 2011, 12:26:55 PM3/30/11
to gosu...@googlegroups.com
Chet,

How are you using XML in gosu?

I'm not sure if the strongly typed XML library supports XPath or not, but the untyped XML library (SimpleXmlNode) offers something I think it more intuitive than XPath(although a bit more code), there is a 'Descendents' property on every node, which is an Iterable of all children of the given node :

uses gw.xml.simple.SimpleXmlNode

var xml = SimpleXmlNode.parse( "<foo><bar doh='blah'/><bar doh='flah'/><bar doh='blah'/></foo>" )

print( xml.Descendents.where( \ n -> n.Name == "bar" and n.Attributes["doh"] == 'blah'  ).Count )

Using this method, in conjunction with the enhancement methods such as where() and map(), you can usually get at what you want.

There is also a 'Children' property with only the direct children in it.

So an imperfect rendering of your XPath expression might be:


  root.Children.where( \ n -> n.Attributes["attrib"] == "someValue" ).map( \ n -> n.Children.firstWhere( \ c -> c.Name == "childElement" ) )

Obviously this is not as terse as XPath, but I believe it is more flexible and is certainly more "gosu-like".

I'll let DGreen chime in on the strongly typed XML side of things.

Also, I should note, you can use whatever java library you like in Gosu, so if you'd like to use Xerces or some other library that supports XPath, that should be no problem.

Thanks for giving Gosu a try!

Cheers,
Carson


--
You received this message because you are subscribed to the Google Groups "gosu-lang" group.
To post to this group, send email to gosu...@googlegroups.com.
To unsubscribe from this group, send email to gosu-lang+...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/gosu-lang?hl=en.


D. Green-Lank

unread,
Mar 30, 2011, 11:50:39 PM3/30/11
to gosu-lang
The easiest way to navigate an XML document is by using an XSD dropped
into a Gosu source path.

Let's say we have the following XSD:

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="root">
<xs:complexType>
<xs:sequence>
<xs:element name="child" minOccurs="0" maxOccurs="unbounded">
<xs:complexType>
<xs:attribute name="key" type="xs:int"/>
<xs:attribute name="value" type="xs:string"/>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>

We'll call it demo.xsd and drop it into a Gosu source root. Lets say
we have the following instance document, demo.xml:

<root>
<child key="1" value="one"/>
<child key="2" value="two"/>
<child key="3" value="three"/>
<child key="4" value="four"/>
<child key="5" value="five"/>
</root>

The following Gosu code can be used to find the "value" attribute of
the "child" element where key="3":

var xml = demo.Root.parse( new java.io.File( "platform/xml/gsrc/
demo.xml" ) )
print( xml.Child.firstWhere( \ r -> r.Key == 3 ).Value )

when run, prints:

three

And if you don't care to use an XSD at all, you can achieve a similar
result with the following:

var xml = gw.xml.XmlElement.parse( new java.io.File( "platform/xml/
gsrc/demo.xml" ) )
print( xml.getChildren( "child" ).firstWhere( \ r -
>r.getAttributeValue( "key" ) == "3" ).getAttributeValue( "value" ) )

We don't have any XPath support whatsoever at this time, and while it
might be added in the future, the idea was that Gosu is powerful
enough using blocks to find what you like.

David
Reply all
Reply to author
Forward
0 new messages