Bonjour,
J'essaye de lire un XML, ça marche plutôt bien dans l'ensemble sauf pour l'attribut nommé :
dont j'essaie d'avoir la valeur "1234567890"
Code xml : Sélectionner tout - Visualiser dans une fenêtre à part <custom-attribute attribute-id="loyaltyNumber">1234567890</custom-attribute>
Voici le code de test :
Le résultat actuel est :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36 DECLARE @XML XML = '<customers> <customer customer-no="00000001"> <credentials> <login>test@email.com</login> </credentials> <profile> <preferred-locale>fr_BE</preferred-locale> <custom-attributes> <custom-attribute attribute-id="ServiceId">1</custom-attribute> <custom-attribute attribute-id="loyaltyNumber">1234567890</custom-attribute> </custom-attributes> </profile> <note/> </customer> <customer customer-no="00000002"> <credentials> <login>test2@email.com</login> </credentials> <profile> <preferred-locale>fr_FR</preferred-locale> <custom-attributes> <custom-attribute attribute-id="loyaltyNumber">1234567890</custom-attribute> </custom-attributes> </profile> <note/> </customer> </customers>' SELECT CustomerNo = Events.value('@customer-no', 'int'), --EventType = Events.value('@Type', 'varchar(20)'), CustomerLogin =Events.value('(credentials/login)[1]', 'varchar(60)'), CustomerLocal =Events.value('(profile/preferred-locale)[1]', 'varchar(60)'), EventKind =Events.value('(profile/custom-attributes/custom-attribute)[2]', 'varchar(60)') FROM @XML.nodes('/customers/customer') AS XTbl(Events)
Et c'est logique vu que les "custom-attribute" sont optionnels, on ne peut donc pas y accèder avec l'index.
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7 |------------------------------------------------------| |CustomerNo |CustomerLogin |CustomerLocal|EventKind | |------------------------------------------------------| |1 |test@email.com |fr_BE |1234567890 | |2 |test2@email.com |fr_FR |NULL | |------------------------------------------------------|
Je cherche donc un moyen de pouvoir y accéder par le nom : attribute-id="loyaltyNumber"
Merci à d'avance à toute les personnes qui essayeront de m'aider.
EDIT :
C'était tout bête :
ou
Code : Sélectionner tout - Visualiser dans une fenêtre à part EventKind =Events.value('(profile/custom-attributes/custom-attribute[@attribute-id="loyaltyNumber"])[1]', 'varchar(60)')
Code : Sélectionner tout - Visualiser dans une fenêtre à part EventKind = Events.query('(profile/custom-attributes/custom-attribute)[@attribute-id=''loyaltyNumber'']').value('/','varchar(60)')
Partager