Convert xml to key-value pair notation

前端 未结 2 1287
醉梦人生
醉梦人生 2021-02-09 09:59

I use xmlstarlet el -v to display the structure of an xml file, including all the attributes and values. I would like to convert its output to some sort of key-valu

2条回答
  •  别那么骄傲
    2021-02-09 10:08

    Since you're already using xmlstarlet, you could also use XSLT.

    XSLT 1.0 (can be run with xmlstarlet by using the tr command)

    
        
        
    
        
    
        
    
        
            
                
                    
                        
                    
                    
                        
                    
                    
                
            
            
                
                
    
            
            
                
              
            
            
        
    
        
            
                          
            
        
    
    
    

    Note: There is a parameter named sep so you can change the separator from the command line (using -p sep="/" or whatever separator you want). It's currently set to your preferred ".".

    Output

    commSyslog.descr=Syslog Service
    commSyslog.name=syslog
    commSyslog.policyOwner=local
    commSyslog.severity=critical
    commSyslog.commSyslogClient[1].adminState=disabled
    commSyslog.commSyslogClient[1].forwardingFacility=local7
    commSyslog.commSyslogClient[1].hostname=none
    commSyslog.commSyslogClient[1].name=secondary
    commSyslog.commSyslogClient[1].severity=critical
    commSyslog.commSyslogClient[2].adminState=disabled
    commSyslog.commSyslogClient[2].forwardingFacility=local7
    commSyslog.commSyslogClient[2].hostname=none
    commSyslog.commSyslogClient[2].name=tertiary
    commSyslog.commSyslogClient[2].severity=critical
    commSyslog.commSyslogClient[3].adminState=disabled
    commSyslog.commSyslogClient[3].forwardingFacility=local7
    commSyslog.commSyslogClient[3].hostname=none
    commSyslog.commSyslogClient[3].name=primary
    commSyslog.commSyslogClient[3].severity=critical
    commSyslog.commSyslogMonitor.adminState=disabled
    commSyslog.commSyslogMonitor.descr=
    commSyslog.commSyslogMonitor.name=
    commSyslog.commSyslogMonitor.severity=critical
    commSyslog.commSyslogConsole.adminState=disabled
    commSyslog.commSyslogConsole.descr=
    commSyslog.commSyslogConsole.name=
    commSyslog.commSyslogConsole.severity=critical
    commSyslog.commSyslogSource.audits=disabled
    commSyslog.commSyslogSource.descr=
    commSyslog.commSyslogSource.events=disabled
    commSyslog.commSyslogSource.faults=enabled
    commSyslog.commSyslogSource.name=
    commSyslog.commSyslogFile.adminState=enabled
    commSyslog.commSyslogFile.descr=
    commSyslog.commSyslogFile.name=messages
    commSyslog.commSyslogFile.severity=critical
    commSyslog.commSyslogFile.size=4194304
    

提交回复
热议问题