xmlstarlet XPath expression selects single result rather than multiple

前端 未结 2 1970
天命终不由人
天命终不由人 2021-01-23 03:03

I have an XML file having structure like this:



    
        

        
2条回答
  •  醉梦人生
    2021-01-23 03:34

    The XPath and xmlstarlet command you use,

    xmlstarlet sel -t -v "//role[.//sid = 'abc']/@name" test.xml
    

    already results in the following strings being returned,

    Employees
    test1
    test2
    test3
    

    as requested.

    Your XPath is correct; your xmlstarlet command line is correct. Recheck the filename you're using and the directory from which you're running the xmlstarlet. Recheck how you're using the results of the xmlstarlet command in your actual context. In this simplified form you've posted, the expected results are precisely what's being returned.


    Update: Sorry to hear you're still stuck. The last idea I have is to post a detailed transcript to provide proof that this should be working so that you can retrace your steps and try to see where your path may be different:

    c:\gd\usr\kjh\proj\try\xml
    > cat test.xml
    
    
        
            
                
                    
                        abc
                        bcd
                    
                
            
            
                
                    
                        abc
                        zxc
                    
                
                
                    
                    
                        abc
                        ghi
                    
                
                
                    
                    
                        abc
                    
                
            
        
    
    
    c:\gd\usr\kjh\proj\try\xml
    > xmlstarlet sel -t -v "//role[.//sid = 'abc']/@name" test.xml
    Employees
    test1
    test2
    test3
    c:\gd\usr\kjh\proj\try\xml
    > xmlstarlet --version
    1.5.0
    compiled against libxml2 2.9.1, linked with 20901
    compiled against libxslt 1.1.28, linked with 10128
    
    c:\gd\usr\kjh\proj\try\xml
    > systeminfo | findstr /B /C:"OS Name" /C:"OS Version"
    OS Name:                   Microsoft Windows 7 Professional
    OS Version:                6.1.7601 Service Pack 1 Build 7601
    c:\gd\usr\kjh\proj\try\xml
    >
    

    Final update:

    OP noticed that his version of xmlstarlet (v1.0.1) was considerably older than the one I used in the above transcript (xmlstarlet v1.5.0). Upgrading to the latest version of xmlstarlet resolved the problem.

提交回复
热议问题