Question Récupérer l'id d'un noeud xml

Plus d'informations
il y a 15 ans 6 mois #7831 par DUMOULIN
Bonjour,
J'ai un fichier xml sous cette forme :
[code:1]
- <oss>
- <os guid=\"{ZZZZZZ}\" enable=\"True\">
<Name>Windows Server 2003 AA</Name>
</os>
- <os guid=\"{YYYYYY}\" enable=\"True\">
<Name>Windows Server 2003 BB</Name>
</os>
- <os guid=\"{XXXXXX}\" enable=\"True\">
<Name>Windows Server 2003 CC</Name>
</os>
- <os guid=\"{WWWWWW}\" enable=\"True\">
<Name>Windows Server 2003 DD</Name>
</os>
</oss>
[/code:1]
J'aurais besoin de connaître les Ids des noeuds contenant les guid ZZZZZZ et XXXXXX pour récupérer ce qu'il y a dans le tag Name par exemple comme ça :
[code:1]
[xml]$listOS = get-content \"\\Serveur\OperatingSystems.xml\"
foreach ($Line in $listOS)
{
$DefList = $ListOS.oss.os[$IdXMLNode].Name
}
[/code:1]

Si vous avez une idée... Je suis preneur !

Merci de votre aide,

Harfango

Connexion ou Créer un compte pour participer à la conversation.

Plus d'informations
il y a 15 ans 6 mois #7834 par Laurent Dardenne
Tu peux utiliser Xpath :
[code:1]
#ps v2
[xml]$Datas=GC g:\temp\Test.xml
$Nodes=$Datas|
#Collision de nom avec PSCX
Microsoft.PowerShell.Utility\Select-Xml -XPath '//os[@guid = \"{ZZZZZZ}\" or @guid =\"{XXXXXX}\"]'
$Nodes|% {$_.Node.Name}
[/code:1]
Le problème est d'apprendre la syntaxe, comme avec les regex, mais en moins difficile.
Sinon il te faut parcourir chaque attribut de chaque noeud du document XML cible.

Voir aussi :
xml.developpez.com/faq/?page=2
cours.ebsi.umontreal.ca/SCI6132/H2010/matthem/intro-XPath.ppt
jerome.developpez.com/xmlxsl/xpath/
www.dil.univ-mrs.fr/~massat/ens/xml/4-xpath.html
msdn.microsoft.com/fr-fr/library/ms256471.aspx

Tutoriels PowerShell

Connexion ou Créer un compte pour participer à la conversation.

Plus d'informations
il y a 15 ans 6 mois #7835 par Richard Lazaro
Bonjour,

Je te conseille de regarder du côté du xPath pour faire de la recherche dans du xml, par exemple ta demande serait :

[code:1]
[xml]$listOS = gc '\\Serveur\OperatingSystems.xml'
$listOS.SelectNodes(\"//oss/os[@guid='{ZZZZZZ}' or @guid='{XXXXXX}']\"«»)[/code:1]

Cela retourne un objet System.Xml.XPathNodeList que tu parcours avec un simple foreach

[code:1]
$listOS.SelectNodes(\"//oss/os[@guid='{ZZZZZZ}' or @guid='{XXXXXX}']\"«») | %{$_.Name}
[/code:1]

Et donc tu as pour résultat :

[code:1]
Windows Server 2003 AA
Windows Server 2003 CC
[/code:1]

Bien Cordialement,
Richard Lazaro.<br><br>Message édité par: Richard Lazaro, à: 21/09/10 11:34

Think-MS : (Get-Life).Days | %{ Learn-More }

\\&quot;Problems cannot be solved by the same level of thinking that created them.\\&quot; - Albert Einstein

Connexion ou Créer un compte pour participer à la conversation.

Plus d'informations
il y a 15 ans 6 mois #7837 par DUMOULIN
Réponse de DUMOULIN sur le sujet Re:Récupérer l'id d'un noeud xml
Rebonjour et merci beaucoup pour cette aide précieuse !

L'objectif final étant de \&quot;remplir\&quot; les items d'une Combobox, ci-joint le script finalisé :

[code:1]
$CBoxOS.Items.clear()
[xml]$ListOSGroup = get-content \&quot;\\$IP\DeploymentShare$\Control\OperatingSystemGroups.xml\&quot;
$OSEnableList = $ListOSGroup.groups.group | Where-Object {$_.Name -eq \&quot;default\&quot;} | %{$_.Member}
[xml]$listOS = get-content \&quot;\\$IP\DeploymentShare$\Control\OperatingSystems.xml\&quot;
foreach ($Line in $OSEnableList)
{
$DefOSList = $listOS.SelectNodes(\&quot;//oss/os[@guid='$Line']\&quot;«») | %{$_.Name} | foreach {$_.SubString(0,$_.IndexOf(' in'))}
foreach ($ListComboOS in $DefOSList)
{
$CBoxOS.Items.Add($ListComboOS)|Out-Null
}
}
[/code:1]

Pour les fichiers xml, voici leur contenu (je ne suis pas arrivé à joindre les 2 fichiers :s )

OperatingSystemGroups.xml
[code:1]
&lt;groups&gt;
&lt;group guid=\&quot;{00000000-0000-0000-0000-000000000000}\&quot; enable=\&quot;True\&quot;&gt;
&lt;Name&gt;default&lt;/Name&gt;
&lt;Comments&gt;default folder (root)&lt;/Comments&gt;
&lt;Member&gt;{AAAAAA}&lt;/Member&gt;
&lt;Member&gt;{BBBBBB}&lt;/Member&gt;
&lt;Member&gt;{CCCCCC}&lt;/Member&gt;
&lt;Member&gt;{DDDDDD}&lt;/Member&gt;
&lt;Member&gt;{EEEEEE}&lt;/Member&gt;
&lt;Member&gt;{FFFFFF}&lt;/Member&gt;
&lt;Member&gt;{GGGGGG}&lt;/Member&gt;
&lt;/group&gt;
&lt;group guid=\&quot;{FFFFFFFF-FFFF-FFFF-FFFF-FFFFFFFFFFFF}\&quot; enable=\&quot;True\&quot;&gt;
&lt;Name&gt;hidden&lt;/Name&gt;
&lt;Comments&gt;This folder contains shared source items that have not been made explicitly visible in this deployment share.&lt;/Comments&gt;
&lt;Member&gt;{HHHHHH}&lt;/Member&gt;
&lt;Member&gt;{IIIIII}&lt;/Member&gt;
&lt;Member&gt;{JJJJJJ}&lt;/Member&gt;
&lt;Member&gt;{KKKKKK}&lt;/Member&gt;
&lt;/group&gt;
&lt;/groups&gt;
[/code:1]

OperatingSystems.xml (extrait) :
[code:1]
&lt;oss&gt;
&lt;os guid=\&quot;{AAAAAA}\&quot; enable=\&quot;True\&quot;&gt;
&lt;Name&gt;Windows Server 2003 Enterprise Edition SP2 source files in Windows Server 2003 Enterprise Edition SP2&lt;/Name&gt;
&lt;Description&gt;Windows Server 2003 Enterprise Edition SP2 source files&lt;/Description&gt;
&lt;/os&gt;
&lt;os guid=\&quot;{BBBBBB}\&quot; enable=\&quot;True\&quot;&gt;
&lt;Name&gt;Windows Server 2003 Standard Edition SP2 source files in Windows Server 2003 Standard Edition SP2&lt;/Name&gt;
&lt;Description&gt;Windows Server 2003 Standard Edition source files&lt;/Description&gt;
&lt;/os&gt;
&lt;os guid=\&quot;{CCCCCC}\&quot; enable=\&quot;True\&quot;&gt;
&lt;Name&gt;Windows Server 2008 R2 SERVERSTANDARD in Windows Server 2008 R2 x64 install.wim&lt;/Name&gt;
&lt;Description&gt;Windows Server 2008 R2 SERVERSTANDARD&lt;/Description&gt;
&lt;/os&gt;
&lt;os guid=\&quot;{HHHHHH}\&quot; enable=\&quot;True\&quot;&gt;
&lt;Name&gt;Windows Server 2008 R2 SERVERSTANDARDCORE in Windows Server 2008 R2 x64 install.wim&lt;/Name&gt;
&lt;Description&gt;Windows Server 2008 R2 SERVERSTANDARDCORE&lt;/Description&gt;
&lt;/os&gt;
&lt;os guid=\&quot;{DDDDDD}\&quot; enable=\&quot;True\&quot;&gt;
&lt;Name&gt;Windows Server 2008 R2 SERVERENTERPRISE in Windows Server 2008 R2 x64 install.wim&lt;/Name&gt;
&lt;Description&gt;Windows Server 2008 R2 SERVERENTERPRISE&lt;/Description&gt;
&lt;/os&gt;
&lt;os guid=\&quot;{IIIIII}\&quot; enable=\&quot;True\&quot;&gt;
&lt;Name&gt;Windows Server 2008 R2 SERVERENTERPRISECORE in Windows Server 2008 R2 x64 install.wim&lt;/Name&gt;
&lt;Description&gt;Windows Server 2008 R2 SERVERENTERPRISECORE&lt;/Description&gt;
&lt;/os&gt;
&lt;os guid=\&quot;{EEEEEE}\&quot; enable=\&quot;True\&quot;&gt;
&lt;Name&gt;Windows Server 2008 R2 SERVERDATACENTER in Windows Server 2008 R2 x64 install.wim&lt;/Name&gt;
&lt;Description&gt;Windows Server 2008 R2 SERVERDATACENTER&lt;/Description&gt;
&lt;/os&gt;
&lt;os guid=\&quot;{JJJJJJ}\&quot; enable=\&quot;True\&quot;&gt;
&lt;Name&gt;Windows Server 2008 R2 SERVERDATACENTERCORE in Windows Server 2008 R2 x64 install.wim&lt;/Name&gt;
&lt;Description&gt;Windows Server 2008 R2 SERVERDATACENTERCORE&lt;/Description&gt;
&lt;/os&gt;
&lt;os guid=\&quot;{FFFFFF}\&quot; enable=\&quot;True\&quot;&gt;
&lt;Name&gt;Windows Server 2008 R2 SERVERWEB in Windows Server 2008 R2 x64 install.wim&lt;/Name&gt;
&lt;Description&gt;Windows Server 2008 R2 SERVERWEB&lt;/Description&gt;
&lt;/os&gt;
&lt;os guid=\&quot;{KKKKKK}\&quot; enable=\&quot;True\&quot;&gt;
&lt;Name&gt;Windows Server 2008 R2 SERVERWEBCORE in Windows Server 2008 R2 x64 install.wim&lt;/Name&gt;
&lt;Description&gt;Windows Server 2008 R2 SERVERWEBCORE&lt;/Description&gt;
&lt;/os&gt;
&lt;os guid=\&quot;{GGGGGG}\&quot; enable=\&quot;True\&quot;&gt;
&lt;/os&gt;
&lt;/oss&gt;
[/code:1]

Merci encore pour l'aide et à bientôt, en espérant que ce petit bout de script pourra servir :-)

Harfango

Connexion ou Créer un compte pour participer à la conversation.

Temps de génération de la page : 0.042 secondes
Propulsé par Kunena