XQuery, Group By, Count

flashray

Erfahrenes Mitglied
Hallo,

wie gruppiert man mit XQuery Knoten nach bestimmten Kriterien (gleiche Knotenname, Namespace, Prefix) und zählt dabei das Vorkommnis um ein "Histogramm" als XML Ergebnis Dokument zu erstellen.

In SQL war das noch ganz leicht mit Group By und Count(). Hier finde ich jedoch leider keinen Ansatz wie man das mit verschachtelten FLWORs realisieren könnte.

Könnte vielleicht jemand einen Algorithmus in Worten beschreiben, wie man das lösen könnte?


Vg Erdal
 
Hallo,

könnte sich bitte jemand den Code anschauen und Lösungsvorschläge machen?

XQuery Anfrage
Code:
declare namespace wsprak="http://db.informatik.uni-mannheim.de/xmlprak"; 
	<env:Envelope xmlns:env='http://www.w3.org/2003/05/soap-envelope'> 
		<env:Header/>
		<env:Body>
			<wsprak:BodyHistogram>
				{
					for $x in distinct-values( for $y in (env:Envelope/env:Body/wsprak:histogram/descendant::*)
								return node-name($y))
					let $z := (env:Envelope/env:Body/wsprak:histogram/descendant::*)
					where node-name($z) = $x
					return 	<wsprak:Tag>
								<wsprak:LocalName>{local-name($z)}</wsprak:LocalName>
	      						<wsprak:Prefix>{in-scope-prefixes($z)}</wsprak:Prefix>
	      						<wsprak:Namespace>{namespace-uri($z)}</wsprak:Namespace>
	      						<wsprak:Count>{count($z)}</wsprak:Count>
	      					</wsprak:Tag>
				}
			</wsprak:BodyHistogram>
		</env:Body>
</env:Envelope>

Fehlermeldung
Code:
Error on line 10 of file:/media/hda5/EclipseWorkspace/Informatik/Versuch2/histogram.xq:
  XPTY0004: A sequence of more than one item is not allowed as the first argument of
  node-name() (<PLAY/>, <TITLE/>, ...) 
A sequence of more than one item is not allowed as the first argument of node-name() (<PLAY/>, <TITLE/>, ...)

Code:
      <env:Envelope>     
         <env:Header/>    
         <env:Body xmlns:xp="http://db.informatik.uni-mannheim.de/xmlprak">      
            <xp:histogram>
               <PLAY>
                  <TITLE>The Tragedy of Hamlet, Prince of Denmark</TITLE>
                  <FM>
                     <P>Text placed in the public domain by Moby Lexical Tools, 1992.</P>
                     <P>SGML markup by Jon Bosak, 1992-1994.</P>
                     <P>XML version by Jon Bosak, 1996-1998.</P>
                     <P>This work may be freely copied and distributed worldwide.</P>
                  </FM>
                  <PERSONAE>
                     <TITLE>Dramatis Personae</TITLE>
                     <PERSONA>CLAUDIUS, king of Denmark. </PERSONA>
                     <PERSONA>HAMLET, son to the late, and nephew to the present king.</PERSONA>
                     <PERSONA>POLONIUS, lord chamberlain. </PERSONA>
                     <PERSONA>HORATIO, friend to Hamlet.</PERSONA>
                     <PERSONA>LAERTES, son to Polonius.</PERSONA>
                     <PERSONA>LUCIANUS, nephew to the king.</PERSONA>
                     <PGROUP>
                        <PERSONA>VOLTIMAND</PERSONA>
                        <PERSONA>CORNELIUS</PERSONA>
                        <PERSONA>ROSENCRANTZ</PERSONA>
                        <PERSONA>GUILDENSTERN</PERSONA>
                        <PERSONA>OSRIC</PERSONA>
                        <GRPDESCR>courtiers.</GRPDESCR>
                     </PGROUP>
                     <PERSONA>A Gentleman</PERSONA>
                     <PERSONA>A Priest. </PERSONA>
                     <PGROUP>
                        <PERSONA>MARCELLUS</PERSONA>
                        <PERSONA>BERNARDO</PERSONA>
                        <GRPDESCR>officers.</GRPDESCR>
                     </PGROUP>
                     <PERSONA> % FRANCISCO, a soldier.</PERSONA>
                     <PERSONA>REYNALDO, servant to Polonius.</PERSONA>
                     <PERSONA>Players.</PERSONA>
                     <PERSONA>Two Clowns, grave-diggers.</PERSONA>
                     <PERSONA>FORTINBRAS, prince of Norway. </PERSONA>
                     <PERSONA>A Captain.</PERSONA>
                     <PERSONA>English Ambassadors. </PERSONA>
                     <PERSONA>GERTRUDE, queen of Denmark, and mother to Hamlet. </PERSONA>
                     <PERSONA>OPHELIA, daughter to Polonius.</PERSONA>
                     <PERSONA>Lords, Ladies, Officers, Soldiers, Sailors, Messengers, and other Attendants.</PERSONA>
                     <PERSONA>Ghost of Hamlet's Father. </PERSONA>
                  </PERSONAE>
                  <SCNDESCR>SCENE  Denmark.</SCNDESCR>
                  <PLAYSUBT>HAMLET</PLAYSUBT>
                  <ACT>
                     <TITLE>ACT I</TITLE>
                     <SCENE>
                        <TITLE>SCENE I.  Elsinore. A platform before the castle.</TITLE>
                        <STAGEDIR>FRANCISCO at his post. Enter to him BERNARDO</STAGEDIR>
                        <SPEECH>
...


Vg Erdal
 
Zuletzt bearbeitet:
Zurück