Click here to Skip to main content
15,886,110 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

how can I read and automated processing of XML nodes with a "-" in the name with PowerShell.


I found this in the help for select-xml:

XML
<?xml version="1.0" encoding="utf-8"?>
 <Book>
   <projects>
     <project name="Book1" date="2009-01-20">
       <book-editions>
          <edition language="English">En.Book1.com</edition>
          <edition language="German">Ge.Book1.Com</edition>
          <edition language="French">Fr.Book1.com</edition>
          <edition language="Polish">Pl.Book1.com</edition>
       </book-editions>
     </project>
   </projects>
 </Book>


and added "book-" in the nodes because I have the "-" in my real xml.

the help says that if I executed this command

$xml | select-xml -xpath "//edition"

I should get this output

Text            Node          Path
----            ----          ----
En.Book1.com    edition       InputStream
Ge.Book1.Com    edition       InputStream
Fr.Book1.com    edition       InputStream
Pl.Book1.com    edition       InputStream


but I just get this

Node                                    Path                                    Pattern
----                                    ----                                    -------
edition                                 InputStream                             //edition
edition                                 InputStream                             //edition
edition                                 InputStream                             //edition
edition                                 InputStream                             //edition


So I don't know if the problem is the "-". The real question should be: How can I search for a specific node ("edition") and display/get its contend?

Thanks for your answers.

Alex
Posted
Updated 2-Sep-12 0:39am
v3
Comments
Andreas Gieriet 1-Sep-12 15:00pm    
What did you try and where did you fail?
Cheers
Andi
PS: Please provide the script that causes problems and some sample data, and what you exactly mean be "automated processing". Or do you expect us to invent all of that?

I suppose, you don't need the path and the node name. So you can use one of these, supposing, that $books contains the xml:
$books | select-xml -xpath "//edition" | % {$_.Node."#text"}

$books.SelectNodes("//edition") | % { $_."#text"}

$books.book.projects.project."book-editions".edition | % {$_."#text"}

In the later one, you can see, how you could reference the element name containing the hyphen.
 
Share this answer
 
Similar to Zoltan's solution, but displays all three columns with headings, as required:


XML
$xml = @"
<?xml version="1.0" encoding="utf-8"?>
 <Book>
   <projects>
     <project name="Book1" date="2009-01-20">
       <book-editions>
          <edition language="English">En.Book1.com</edition>
          <edition language="German">Ge.Book1.Com</edition>
          <edition language="French">Fr.Book1.com</edition>
          <edition language="Polish">Pl.Book1.com</edition>
       </book-editions>
     </project>
   </projects>
 </Book>
"@

$xml | select-xml -xpath "//edition" | select-object @{n='Text' ; e={$_.Node."#text"}}, Node, Path
 
Share this answer
 
the below will fetch you the results

project."book-editions"
 
Share this answer
 
v2
Comments
Kats2512 8-Aug-17 9:11am    
you think after 5 years this guy would still be looking for an answer???
Member 13351143 14-Aug-18 6:00am    
LOL..

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900