none
select-xml with namespace RRS feed

  • Question

  • Hello,

    I am having difficulty selecting xmldata when it contains a namespace and can't seem to get the syntax correctly.

    The xml data is

    <?xml version="1.0" encoding="ISO-8859-1"?>
    <nc:rpc-reply message-id="1" xmlns="http://www.cisco.com/nxos:1.0:nfcli" xmlns:nc="urn:ietf:params:xml:ns:netconf:base:1.0">
    <nc:data>
    <show>
    <version>
    <__XML__OPT_Cmd_sysmgr_show_version___readonly__>
    <__readonly__>
    <header_str>Cisco Nexus Operating System (NX-OS) Software TAC support: http://www.cisco.com/tac Documents: http://www.cisco.com/en/US/products/ps9372/tsd_products_support_series_home.html Copyright (c) 2002-2013, Cisco Systems, Inc. All rights reserved. The copyrights to certain works contained herein are owned by other third parties and are used and distributed under license. Some parts of this software are covered under the GNU Public License. A copy of the license is available at http://www.gnu.org/licenses/gpl.html. </header_str>
    <bios_ver_str>3.6.0</bios_ver_str>
    <loader_ver_str>N/A</loader_ver_str>
    <kickstart_ver_str>5.2(1)N1(4)</kickstart_ver_str>
    <sys_ver_str>5.2(1)N1(4)</sys_ver_str>
    <power_seq_ver_str>Module 1: version v1.0</power_seq_ver_str>
    <power_seq_ver_str> Module 2: version v1.0</power_seq_ver_str>
    <power_seq_ver_str> Module 3: version v5.0</power_seq_ver_str>
    <ucontroller_ver_str>v1.2.0.1</ucontroller_ver_str>
    <power_seq_ver_str>Module 1: v1.0.0.0</power_seq_ver_str>
    <bios_cmpl_time>05/09/2012</bios_cmpl_time>
    <kick_file_name>bootflash:///n5000-uk9-kickstart.5.2.1.N1.4.bin</kick_file_name>
    <kick_cmpl_time> 3/19/2013 3:00:00</kick_cmpl_time>
    <kick_tmstmp>03/19/2013 06:12:59</kick_tmstmp>
    <isan_file_name>bootflash:///n5000-uk9.5.2.1.N1.4.bin</isan_file_name>
    <isan_cmpl_time> 3/19/2013 3:00:00</isan_cmpl_time>
    <isan_tmstmp>03/19/2013 08:10:47</isan_tmstmp>
    <chassis_id>Nexus5548 Chassis</chassis_id>
    <module_id>O2 32X10GE/Modular Universal Platform Supervisor</module_id>
    <cpu_name>Intel(R) Xeon(R) CPU </cpu_name>
    <memory>8263848</memory>
    <mem_type>kB</mem_type>
    <proc_board_id>FOC16361YBT</proc_board_id>
    <host_name>VB200-5548A</host_name>
    <bootflash_size>2007040</bootflash_size>
    <kern_uptm_days>51</kern_uptm_days>
    <kern_uptm_hrs>10</kern_uptm_hrs>
    <kern_uptm_mins>15</kern_uptm_mins>
    <kern_uptm_secs>45</kern_uptm_secs>
    <rr_reason>Unknown</rr_reason>
    <rr_sys_ver>5.2(1)N1(4)</rr_sys_ver>
    <rr_service/>
    </__readonly__>
    </__XML__OPT_Cmd_sysmgr_show_version___readonly__>
    </version>
    </show>
    </nc:data>
    </nc:rpc-reply>

    The output will vary slightly on the __XML line and the part near the top.   This example I'm running the command show version.   If I were to run the show inventory command it would pretty much look the same.   The namespace is the same every time as well.

    Either my xpath is incorrect or name space.

    What I am trying to do is get the data between </__readonly__>

    Any help would be appreciated.

    For namespace I put

    $namespace=@{xmlns="http://www.cisco.com/nxos:1.0:nfcli";"xmlns:nc"="urn:ietf:params:xml:ns:netconf:base:1.0"}

    and XMLpath I put

    $XPath="//__readonly__"

    I get the following error message when I do that

    Select-Xml : Prefix "xmlns" is reserved for use by XML.

    At line:1 char:1

    + Select-Xml -Xml $XML -Namespace $NameSpace -XPath $XPath

    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

        + CategoryInfo          : NotSpecified: (:) [Select-Xml], ArgumentException

        + FullyQualifiedErrorId : System.ArgumentException,Microsoft.PowerShell.Commands.SelectXmlCommand

    I tried removing the xmlns part, but that didn't work.   it just produces no results

    Thank you


    Walter

    Wednesday, February 10, 2016 5:52 PM

Answers

  • We can do this:

    $namespaces=@{ ns="http://www.cisco.com/nxos:1.0:nfcli";nc="urn:ietf:params:xml:ns:netconf:base:1.0"}
    
    Get-Content file.xml |
          Select-Xml -XPath '//ns:__readonly__/.' -Namespace $namespaces |
          select -expand Node
    


    \_(ツ)_/


    • Edited by jrv Wednesday, February 10, 2016 6:35 PM
    • Marked as answer by Walter Beach Wednesday, February 10, 2016 10:14 PM
    Wednesday, February 10, 2016 6:35 PM

All replies

  • Please do not post colorized code as it is broken when viewed.  Use code posting control.

    Please post exact code that produces the error.


    \_(ツ)_/

    Wednesday, February 10, 2016 6:05 PM
  • You could do this:

    PS C:\scripts> $xml.'rpc-reply'.data.show.version.__XML__OPT_Cmd_sysmgr_show_version___readonly__.__readonly__

    <rr_service/> >> </__readonly__> header_str : Cisco Nexus Operating System (NX-OS) Software TAC support: http://www.cisco.com/tac Documents: http://www.cisco.com/en/US/products/ps9372/tsd_products_support_series_home.html Copyright (c) 2002-2013, Cisco Systems, Inc. All rights reserved. The copyrights to certain works contained herein are owned by other third parties and are used and distributed under license. Some parts of this software are covered under the GNU Public License. A copy of the license is available at http://www.gnu.org/licenses/gpl.html. bios_ver_str : 3.6.0 loader_ver_str : N/A kickstart_ver_str : 5.2(1)N1(4) sys_ver_str : 5.2(1)N1(4) power_seq_ver_str : {Module 1: version v1.0, Module 2: version v1.0, Module 3: version v5.0, Module 1: v1.0.0.0} ucontroller_ver_str : v1.2.0.1 bios_cmpl_time : 05/09/2012 kick_file_name : bootflash:///n5000-uk9-kickstart.5.2.1.N1.4.bin kick_cmpl_time : 3/19/2013 3:00:00 kick_tmstmp : 03/19/2013 06:12:59 isan_file_name : bootflash:///n5000-uk9.5.2.1.N1.4.bin isan_cmpl_time : 3/19/2013 3:00:00 isan_tmstmp : 03/19/2013 08:10:47 chassis_id : Nexus5548 Chassis module_id : O2 32X10GE/Modular Universal Platform Supervisor cpu_name : Intel(R) Xeon(R) CPU memory : 8263848 mem_type : kB proc_board_id : FOC16361YBT host_name : VB200-5548A bootflash_size : 2007040 kern_uptm_days : 51 kern_uptm_hrs : 10 kern_uptm_mins : 15 kern_uptm_secs : 45 rr_reason : Unknown rr_sys_ver : 5.2(1)N1(4) rr_service :



    \_(ツ)_/


    • Edited by jrv Wednesday, February 10, 2016 6:30 PM
    Wednesday, February 10, 2016 6:08 PM
  • We can do this:

    $namespaces=@{ ns="http://www.cisco.com/nxos:1.0:nfcli";nc="urn:ietf:params:xml:ns:netconf:base:1.0"}
    
    Get-Content file.xml |
          Select-Xml -XPath '//ns:__readonly__/.' -Namespace $namespaces |
          select -expand Node
    


    \_(ツ)_/


    • Edited by jrv Wednesday, February 10, 2016 6:35 PM
    • Marked as answer by Walter Beach Wednesday, February 10, 2016 10:14 PM
    Wednesday, February 10, 2016 6:35 PM
  • Hello,

    Your code works perfectly. 

    Thank you for your help


    • Edited by Walter Beach Wednesday, February 10, 2016 10:13 PM Spelling error on my part lead me to a wrong conclusion.
    Wednesday, February 10, 2016 8:26 PM