Aide pour script et xmlstarlet
Bonjour,
j'ai besoin d'extraire les données d'un fichier XML et de sauvegarder ces données dans un fichier CSV.
J'ai besoin d'extraire du fichier suivant (extrait de fichier):
Code:
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
| <project title="" version="0.5" folioSheetQuantity="0">
<element x="80" y="330" type="embed://import/bornes-cables/borne_simple.elmt" orientation="0" uuid="{697707f9-8748-4261-be66-76a5960819bb}">
<terminals>
<terminal number="_" x="0" y="-6" orientation="0" id="1" nameHidden="0" name="_"/>
<terminal number="_" x="0" y="6" orientation="2" id="2" nameHidden="0" name="_"/>
</terminals>
<inputs>
<input x="-1" text="X3.6" y="3"/>
</inputs>
<elementInformations>
<elementInformation show="1" name="label">X3.6</elementInformation>
</elementInformations>
</element>
<element x="730" y="330" type="embed://import/bornes-cables/borne_simple.elmt" orientation="0" uuid="{45da3bc2-fd5d-4b15-a5f9-a78483286769}">
<terminals>
<terminal number="_" x="0" y="-6" orientation="0" id="3" nameHidden="0" name="_"/>
<terminal number="_" x="0" y="6" orientation="2" id="4" nameHidden="0" name="_"/>
</terminals>
<inputs>
<input x="-1" text="X4.2" y="3"/>
</inputs>
<elementInformations>
<elementInformation show="1" name="label">X4.2</elementInformation>
</elementInformations>
</element>
<conductors>
<conductor terminal1="0" displaytext="1" terminal2="24" onetextperfolio="0" x="0" num="_" y="0" horizrotatetext="0" type="multi" numsize="6" vertirotatetext="270"/>
<conductor terminal1="26" displaytext="1" terminal2="19" onetextperfolio="0" x="0" num="123456789" y="0" horizrotatetext="0" type="multi" numsize="6" vertirotatetext="270"/>
<conductor terminal1="30" displaytext="1" terminal2="15" onetextperfolio="0" x="0" num="02468" y="0" horizrotatetext="0" type="multi" numsize="6" vertirotatetext="270"/>
</conductors> |
données:
1: dans la section /project/diagram/elements/element/elementInformations/elementInformations: name="label" (exemple: name="label">X4.2)
2: dans la section /project/diagram/elements/element: type (exemple: type="embed://import/bornes-cables/borne_simple.elmt")
3: dans la section /project/diagram/elements/element/terminals/terminal: les idx2 (exemple: id="3")
4: dans la section /project/diagram/elements/element/terminals/terminal: les 2 orientation (exemple 0 et 2)
je souhaite mettre les résultats en forme: donnée1;donnée2;donnée3;donnée4 (x2)
J'arrive à tout récupérer sauf le 2ème id et la 2ème orientation.
mon script:
Code:
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
| #!/bin/bash
#nécessite les paquets: zenity; xmlstarlet
{
projet=`zenity --title "Sélectionner le fichier xml" --file-selection --filename="./Bureau"`
if [ $? != 0 ]; then # si fermeture
exit 1
fi
[ $? -ne 0 ] && exit 2 # si annulation
}
{
dossier=`zenity --title "Sélectionner le dossier de destination" --file-selection --directory`
if [ $? != 0 ]; then # si fermeture
exit 1
fi
[ $? -ne 0 ] && exit 2 # si annulation
}
xmlstarlet sel -T -t -m /project/diagram/elements/element -s A:T:- "elementInformations" -v \
"concat(elementInformations/*[@name='label'], ';' ,@type, ';', terminals/terminal/@id, ';' , terminals/terminal/@orientation)" -n "$projet" > "$dossier/bornes.csv"
zenity --info --title="Fichier CSV créé" --height=100 --width=300 |
Merci pour votre aide.
Cordialement.