[FredLUG] Re: XML and XQuery

Peter Larsen ego.alter at gmail.com
Sat Jun 27 15:12:31 EDT 2009


Ups - hit send too early ....

To continue - the condition doesn't limit the content of the meeting
node - it simply limit what meeting nodes to return, but they're
returned complete. To limit what's returned, the condition would have
to be expanded after the ] at the end, to just give the subelements
we're interested in.

The FOR loop now contains meeting elements that the person
participated in, and we return the meeting-date for each meeting.  The
resulting data looks like this:

<ns1:members xmlns:ns1="http://www.fredlug.com/members">
	<ns1:member>
		<ns1:name>Peter</ns1:name>
		<ns1:attendedMeeting>2009-06-27T09:00:00</ns1:attendedMeeting>
		<ns1:attendedMeeting>2001-12-31T12:00:00</ns1:attendedMeeting>
	</ns1:member>
	<ns1:member>
		<ns1:name>Jarred</ns1:name>
		<ns1:attendedMeeting>2009-06-27T09:00:00</ns1:attendedMeeting>
	</ns1:member>
	<ns1:member>
		<ns1:name>Bob</ns1:name>
		<ns1:attendedMeeting>2009-06-27T09:00:00</ns1:attendedMeeting>
	</ns1:member>
	<ns1:member>
		<ns1:name>Anthon</ns1:name>
		<ns1:attendedMeeting>2009-06-27T09:00:00</ns1:attendedMeeting>
	</ns1:member>
	<ns1:member>
		<ns1:name>Paul</ns1:name>
		<ns1:attendedMeeting>2001-12-31T12:00:00</ns1:attendedMeeting>
	</ns1:member>
</ns1:members>

And while the original data is in the data I uploaded, just to
complete the picture, here's what we started with:

<?xml version="1.0" encoding="UTF-8"?>
<tns:fredlug xmlns:tns="http://www.fredlug.com/meeting"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.fredlug.com/meeting myschema.xsd ">
	<tns:longName>tns:longName</tns:longName>
	<tns:founded>2001-01-01</tns:founded>
	<tns:meeting>
		<tns:meetingDate>2009-06-27T09:00:00</tns:meetingDate>
		<tns:location>CRRL</tns:location>
		<tns:attendie>
			<tns:name>Peter</tns:name>
			<tns:isSpeaker>true</tns:isSpeaker>
		</tns:attendie>
		<tns:attendie>
			<tns:name>Jarred</tns:name>
			<tns:isSpeaker>true</tns:isSpeaker>
		</tns:attendie>
		<tns:attendie>
			<tns:name>Bob</tns:name>
			<tns:isSpeaker>false</tns:isSpeaker>
		</tns:attendie>
		<tns:attendie>
			<tns:name>Anthon</tns:name>
			<tns:isSpeaker>false</tns:isSpeaker>
		</tns:attendie>
	</tns:meeting>
	<tns:meeting>
		<tns:meetingDate>2001-12-31T12:00:00</tns:meetingDate>
		<tns:location>CRRL</tns:location>
		<tns:attendie>
			<tns:name>Paul</tns:name>
			<tns:isSpeaker>true</tns:isSpeaker>
		</tns:attendie>
		<tns:attendie>
			<tns:name>Peter</tns:name>
			<tns:isSpeaker>false</tns:isSpeaker>
		</tns:attendie>
	</tns:meeting>
</tns:fredlug>

I hope this makes it a bit clearer. Getting a reverse list like this
is actually part of the advanced use of both XQuery and XSLT. So if
you have a bit of a problem with the concept, you're not alone. This
is actually quite harder in XSLT but part of the "cookbook" features
to know about to master XSLT.

Use this thread to ask questions if you have any. Sorry for all of
this "talking to myself" stuff :)


On Jun 27, 3:04 pm, Peter Larsen <ego.al... at gmail.com> wrote:
> I just realized I promised to show distinct values if I had a little
> bit of time - well consider it done :)
>
> Here's the XQuery that will return a list of attendees  and what
> meetings they attended (remember, the original data shows meetings and
> then who attendeed the meeting):
>
> [XQuery begin]
> (:: pragma bea:global-element-parameter parameter="$fredlug"
> element="ns0:fredlug" location="myschema.xsd" ::)
> (:: pragma bea:global-element-return element="ns1:members"
> location="outputschema.xsd" ::)
>
> declare namespace ns1 = "http://www.fredlug.com/members";
> declare namespace ns0 = "http://www.fredlug.com/meeting";
> declare namespace xf = "http://tempuri.org/xmltest/transform/";
>
> declare function xf:transform($fredlug as element(ns0:fredlug))
>     as element(ns1:members) {
>         let $attendies := $fredlug/ns0:meeting/ns0:attendie
>         return
>             <ns1:members>
>                {
>                  for $member in fn:distinct-values($attendies/
> ns0:name)
>                  return
>                    <ns1:member>
>                       <ns1:name>{$member}</ns1:name>
>                       {
>                          for $meeting in $fredlug/ns0:meeting
> [ns0:attendie/ns0:name/text()=$member]
>                          return
>                            <ns1:attendedMeeting>{data($meeting/
> ns0:meetingDate)}</ns1:attendedMeeting>
>                       }
>                    </ns1:member>
>                }
>             </ns1:members>
>
> };
>
> declare variable $fredlug as element(ns0:fredlug) external;
>
> xf:transform($fredlug)
> [XQuery end]
>
> A few notes here - notice there's two FOR statements. And you may also
> notice a little more in the XPath expression.  The code still starts
> by creating a list of attendee elements (yes, it's spelled badly -
> unfortunately code editors do not do spell checking). The root
> document is created, and the first FOR statement is created - it
> returns a unique list of names - one loop per name.
>
> For each name, the <ns0:name> element is created and the value of the
> name is inserted as text inside of it. Note, that before the $meeting
> variable contained xml nodes - it doesn't now - it just contains
> strings - in essence it's a string array.
>
> Once the name is rendered, another FOR loop is created. This one is a
> bit more interesting. The loop looks for meetings that the person in
> the outside loop attended. We query the XML for the meeting with the
> following XPath expression: $fredlug/ns0:meeting[ns0:attendie/ns0:name/
> text()=$member]. We talked at the meeting that XPath is a query
> language. This query starts by looking at the $fredlug variable, it
> will return meeting elements. The condition - enclosed in [ ] - is
> where any of the attendee elements under the meeting has a name
> element with the content of the name we're looking for. Notice, that
> the [ ] condition doesn't mean that the meeting element returned will
> be limited. It simply means, it will not return meetings that doesn't
> fulfill the condition - but if it does fulfill it, we're returned the
> FULL and COMPLETE
>
> On Jun 27, 2:26 pm, Peter Larsen <ego.al... at gmail.com> wrote:
>
> > After the interest at today's meeting, I've uploaded the files I
> > created at the meeting to our google group page:
>
> >http://groups.google.com/group/fredlug/web/xmltest.zip
>
> > It has the 5 files I created in there. The graphical layout I showed
> > all came from the Eclipse environment. The zip file is of course the
> > source.
>
> > If there's any questions please don't hesitate to ask here in the
> > group.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "FredLUG" group.
To post to this group, send email to fredlug at googlegroups.com
To unsubscribe from this group, send email to fredlug+unsubscribe at googlegroups.com
For more options, visit this group at http://groups.google.com/group/fredlug?hl=en
-~----------~----~----~----~------~----~------~--~---




More information about the Fredlug mailing list