Project

Profile

Help

XQuery on XML with namespace declaration

Added by Anonymous about 20 years ago

Legacy ID: #2835898 Legacy Poster: Maria Muslea (codra13)

Hi, I have a simple XML document that looks like: <?xml version="1.0" encoding="utf-8"?> <geopoint xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://terraworld.isi.edu/Geocode/"> <lat>40.736825869507953</lat> <lon>-73.997625565246025</lon> <geoerror /> <errorbounds>0.001161</errorbounds> <errorboundsunit>Decimal Degrees</errorboundsunit> </geopoint> I would like to write a simple XQuery. Something like: let $d := doc(&quot;D:/maria/examples/e5.xml&quot;) return <m>{$d/geopoint}</m> If I remove the "xmlns" definitions from the XML it is working fine, but otherwise it isn't retrieving anything. I also tried to declare the namespace inside the XQuery like: declare namespace f="http://terraworld.isi.edu/Geocode" and then use f:geopoint to access the info, but it still doesn't work. I did serach on this forum for possible solutions, but I did not find anything. I would really appreciate it if anyone could help me with this. Thank you, Maria


Replies (7)

Please register to reply

RE: XQuery on XML with namespace declaration - Added by Anonymous about 20 years ago

Legacy ID: #2835976 Legacy Poster: Michael Kay (mhkay)

The following works just fine: declare namespace g = "http://terraworld.isi.edu/Geocode/"; let $d := doc("test.xml") return <m>{$d/g:geopoint}</m> Note that the argument to the doc() function must be a URI, not a filename. The above was from the command line, using a relative URI. If you want to do it from a program, its probably safer to use an absolute URI such as "file:///a/b/c/test.xml" Michael Kay

RE: XQuery on XML with namespace declaration - Added by Anonymous about 20 years ago

Legacy ID: #2835980 Legacy Poster: Michael Kay (mhkay)

The forum software seems to be adding spurious semicolons after string literals. Mike

RE: XQuery on XML with namespace declaration - Added by Anonymous about 20 years ago

Legacy ID: #2836065 Legacy Poster: Maria Muslea (codra13)

Hmmmm, this is exactly what my query looks like: String q733 = "declare namespace f=&quot;http://terraworld.isi.edu/Geocode\&quot;;let $d := doc(&quot;file:///D:/maria/examples/e5.xml&quot;) return <m>{$d/f:geopoint}</m>"; and it doesn't work. I get back: <?xml version="1.0" encoding="UTF-8"?> <m/> I just saw that there is a new version of Saxon. I am using saxon7.8. Is this maybe something that works only in the newer version? Thank you for your help, Maria

RE: XQuery on XML with namespace declaration - Added by Anonymous about 20 years ago

Legacy ID: #2836502 Legacy Poster: Michael Kay (mhkay)

I can't immediately think of anything that would stop this working in earlier releases, but 7.8 was a year ago, and memories fade - a lot has changed since then. In fact, I think 7.8 was one of the first releases of the XQuery processor in Saxon. You should definitely try to keep up with new Saxon releases if you want help with such problems. Michael Kay

RE: XQuery on XML with namespace declaration - Added by Anonymous about 20 years ago

Legacy ID: #2837657 Legacy Poster: Maria Muslea (codra13)

I just downloaded the latest release and used QueryAPIExamples.java in saxon8-1-1/samples/java to test my xquery. All I did was replace the xquery in exampleToSerializedSequence() with: final XQueryExpression exp = sqc.compileQuery( "declare namespace f=&quot;http://terraworld.isi.edu/Geocode\&quot;;let $d := doc(&quot;file:///D:/maria/examples/e5.xml&quot;) return <m>{$d/f:geopoint}</m>"); and it still doesn't work. The result that I get back is </m> (nothing). e5.xml contains the following: <?xml version="1.0" encoding="utf-8"?> <geopoint xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://terraworld.isi.edu/Geocode/"> <lat>40.736825869507953</lat> <lon>-73.997625565246025</lon> <geoerror /> <errorbounds>0.001161</errorbounds> <errorboundsunit>Decimal Degrees</errorboundsunit> </geopoint> I would really appreciate it if you could take a look at this. I am probably making a mistake somewhere , but I can't tell what it is. Thank you again, Maria

RE: XQuery on XML with namespace declaration - Added by Anonymous about 20 years ago

Legacy ID: #2837878 Legacy Poster: Michael Kay (mhkay)

The namespace in your source document has a trailing "/" which is missing from your query. Michael Kay

RE: XQuery on XML with namespace declaration - Added by Anonymous about 20 years ago

Legacy ID: #2838167 Legacy Poster: Maria Muslea (codra13)

Thank you so much. It is working now. I really appreciate your help with this. Maria

    (1-7/7)

    Please register to reply