Précédent   Forum du club des développeurs et IT Pro > Autres langages > XML/XSL et SOAP > XQUERY/SGBD
XQUERY/SGBD Le langage XQUERY et tout ce qui a trait aux relations XML et bases de données : BDD XML native, intégration/extraction de XML, XML dans BDD relationnelle... Avant de poster -> FAQ XML, Sources XML
Partagez cette discussion sur d'autres réseaux sociaux : Viadeo Twitter Google Facebook Digg Delicious MySpace Yahoo
Réponse
 
Outils de la discussion
Publicité
'
Vieux 22/08/2011, 17h48   #1
tigunn
Membre chevronné
 
Avatar de tigunn
 
Homme
Développeur de bug
Inscription : janvier 2003
Messages : 565
Détails du profil
Informations personnelles :
Sexe : Homme
Âge : 32
Localisation : France

Informations professionnelles :
Activité : Développeur de bug

Informations forums :
Inscription : janvier 2003
Messages : 565
Points : 607
Points : 607
Par défaut recuperer une partie de chaine de caractères

Salut à tou(te)s,

Je voudrais récupérer par une requête xquery une partie seulement de la valeur d'un attribut dans un fichier xml présenté ainsi:
Code :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<item type="fragment" n="1">
					<cit dbp:hand="encre">
						<quote>bla<choice>
								<orig><c>&amp;</c></orig>
								<reg>et</reg>
							</choice> bablabla <choice>
								<orig><c>&amp;</c></orig>
								<reg>et</reg>
							</choice>bla <lb/>bla. »</quote>
						<bibl xml:id="b2862-3"><lb/><choice>
								<abbr>P.</abbr>
								<expan>P<ex>ère</ex></expan>
							</choice> Féfé <choice>
								<abbr>progr.</abbr>
								<expan><reg>P</reg>an<ex>ès</ex></expan>
							</choice> par le <choice>
								<abbr>chr</abbr>
								<expan>chr<ex>blabla</ex></expan>
							</choice> (<abbr>t</abbr><supplied>.</supplied> 2. <supplied reason="omission"><abbr>p.</abbr></supplied> 91)</bibl>
					</cit>
				</item>
Sauf qu'il y a plusieurs <item> dans le fichier.
La valeur que je voudrais retourner est en gras. Mais je souhaiterais traiter celle-ci et ne conserver que "2862".
Je pensais donc utiliser la fonction substring() mais je ne comprend pas comment elle fonctionne, même avec les tutos .
Voici ma requete, qui hors des lignes concernant substring() fonctionne très bien.
Code :
1
2
3
4
5
6
7
8
9
10
11
12
13
declare namespace dbp="http://dossiers-travail/ns/1.0";
			for $doc in collection("/db/bdd")/*:TEI
			for $fragment in $doc//*:div[@*:type="fragment"]|$doc//*:item[@*:type="fragment"]
			let $bibl3 := data($fragment/descendant-or-self::*:bibl[ends-with(@*:id, "-3")]/@*:id)
			let $id_b3 := substring($bibl3, 2, 4)
			let $no_fragment := data($fragment/@n)
				return <record>
							<fragment>
								<bibl3>{$bibl3}</bibl3>
								<id_b3>{$id_b3}</id_b3>
								<no_fragment>{$no_fragment}</no_fragment>
							</fragment>
						</record>
Et le message d'erreur (retourné par Exist):
Citation:
An exception occurred during query execution: The actual cardinality for parameter 1 does not match the cardinality declared in the function's signature: substring($source as xs:string?, $starting-at as xs:double, $length as xs:double) xs:string?. Expected cardinality: zero or one, got 2


org.xmldb.api.base.XMLDBException: The actual cardinality for parameter 1 does not match the cardinality declared in the function's signature: substring($source as xs:string?, $starting-at as xs:double, $length as xs:double) xs:string?. Expected cardinality: zero or one, got 2.
at org.exist.xmldb.RemoteXPathQueryService.throwException(RemoteXPathQueryService.java:179)
at org.exist.xmldb.RemoteXPathQueryService.query(RemoteXPathQueryService.java:102)
at org.exist.xmldb.RemoteXPathQueryService.query(RemoteXPathQueryService.java:71)
at org.exist.xmldb.RemoteXPathQueryService.execute(RemoteXPathQueryService.java:411)
at org.exist.client.QueryDialog$QueryThread.run(QueryDialog.java:473)
Caused by: org.exist.xquery.XPathException: The actual cardinality for parameter 1 does not match the cardinality declared in the function's signature: substring($source as xs:string?, $starting-at as xs:double, $length as xs:double) xs:string?. Expected cardinality: zero or one, got 2.
at org.exist.xmldb.RemoteXPathQueryService.throwException(RemoteXPathQueryService.java:178)
... 4 more
Caused by: org.exist.xquery.XPathException: The actual cardinality for parameter 1 does not match the cardinality declared in the function's signature: substring($source as xs:string?, $starting-at as xs:double, $length as xs:double) xs:string?. Expected cardinality: zero or one, got 2.
at org.exist.xmldb.RemoteXPathQueryService.throwException(RemoteXPathQueryService.java:178)
at org.exist.xmldb.RemoteXPathQueryService.query(RemoteXPathQueryService.java:102)
at org.exist.xmldb.RemoteXPathQueryService.query(RemoteXPathQueryService.java:71)
at org.exist.xmldb.RemoteXPathQueryService.execute(RemoteXPathQueryService.java:411)
at org.exist.client.QueryDialog$QueryThread.run(QueryDialog.java:473)
Merci de votre aide
__________________
"le rhume distillation - L’estomac est la cucurbite, le cerveau le chapiteau, & le nez le réfrigérant par lequel s’écoule goutte à goutte le produit de la distillation."
de Avicenne

Le monde se divise en deux: ceux qui utilisent le tag et les autres.

Un problème? venez en découdre!
tigunn est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 22/08/2011, 23h04   #2
thelvin
Modérateur
 
Inscription : septembre 2004
Messages : 7 078
Détails du profil
Informations forums :
Inscription : septembre 2004
Messages : 7 078
Points : 10 300
Points : 10 300
Envoyer un message via Skype™ à thelvin
Le message d'erreur est pourtant clair : ta variable $bibl3 contient plus d'une chaîne, alors que la fonction substring() n'accepte de travailler que sur une ou zéro chaîne (zéro aussi ? Ah bon...)

Il faut croire que dans ton document, il existe plus d'un élément <bibl> situé dans un xpath compatible, qui contienne un attribut id de namespace quelconque, dont la valeur termine par '-3'.
Il va donc falloir raffiner la recherche.
thelvin est actuellement connecté   Envoyer un message privé Réponse avec citation 00
Vieux 23/08/2011, 10h08   #3
tigunn
Membre chevronné
 
Avatar de tigunn
 
Homme
Développeur de bug
Inscription : janvier 2003
Messages : 565
Détails du profil
Informations personnelles :
Sexe : Homme
Âge : 32
Localisation : France

Informations professionnelles :
Activité : Développeur de bug

Informations forums :
Inscription : janvier 2003
Messages : 565
Points : 607
Points : 607
Je comprend ma requête ainsi:
Pour tous les documents, de la collection ...
Pour chaque fragment dans ces documents
on récupère $bibl3, donc la valeur de l'attribut id de bibl (<bibl xml:id="2864-3">)
et la valeur de l'attribut n de notre element fragment

En résultat, on obtiens qqchose du genre:
Code :
1
2
3
4
5
6
<record><fragment><bibl3>b2325-3</bibl3><no_fragment>1</no_fragment></fragment></record>
<record><fragment><bibl3>b2322-3</bibl3><no_fragment>2</no_fragment></fragment></record>
<record><fragment><bibl3>b2324-3</bibl3><no_fragment>3</no_fragment></fragment></record>
<record><fragment><bibl3>b2326-3</bibl3><no_fragment>4</no_fragment></fragment></record>
<record><fragment><bibl3>b2778-3</bibl3><no_fragment>1</no_fragment></fragment></record>
<record><fragment><bibl3>b2777-3</bibl3><no_fragment>2</no_fragment></fragment></record>....
Si j'enlève le substring, ce qui est tout à fait correct.
Pour chaque fragment, il n'y a qu'un seul élément <bibl>.
Comprend pas ...
__________________
"le rhume distillation - L’estomac est la cucurbite, le cerveau le chapiteau, & le nez le réfrigérant par lequel s’écoule goutte à goutte le produit de la distillation."
de Avicenne

Le monde se divise en deux: ceux qui utilisent le tag et les autres.

Un problème? venez en découdre!
tigunn est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 31/08/2011, 11h39   #4
tigunn
Membre chevronné
 
Avatar de tigunn
 
Homme
Développeur de bug
Inscription : janvier 2003
Messages : 565
Détails du profil
Informations personnelles :
Sexe : Homme
Âge : 32
Localisation : France

Informations professionnelles :
Activité : Développeur de bug

Informations forums :
Inscription : janvier 2003
Messages : 565
Points : 607
Points : 607
Finalement, je fais le traitement en php; c'est bien plus simple.
__________________
"le rhume distillation - L’estomac est la cucurbite, le cerveau le chapiteau, & le nez le réfrigérant par lequel s’écoule goutte à goutte le produit de la distillation."
de Avicenne

Le monde se divise en deux: ceux qui utilisent le tag et les autres.

Un problème? venez en découdre!
tigunn est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 31/08/2011, 13h14   #5
thelvin
Modérateur
 
Inscription : septembre 2004
Messages : 7 078
Détails du profil
Informations forums :
Inscription : septembre 2004
Messages : 7 078
Points : 10 300
Points : 10 300
Envoyer un message via Skype™ à thelvin
Entre-temps, j'avais pas mal cherché (par curiosité) ce qui pouvait bien faire qu'il se plaigne de la cardinalité. J'ai fait chou blanc total. Peut-être un bug, mais c'est difficile à prouver.
thelvin est actuellement connecté   Envoyer un message privé Réponse avec citation 00
Réponse Cette discussion est résolue.
Outils de la discussion

Navigation rapide


Fuseau horaire GMT +2. Il est actuellement 02h02.


 
 
 
 
Partenaires

Hébergement Web