I have an XML file having structure like this:
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
<?xml version="1.0" encoding="UTF-8"?>
<hudson>
<authorizationStrategy>
<roleMap type="globalRoles">
<role name="Employees">
<assignedSIDs>
<sid>abc</sid>
<sid>bcd</sid>
</assignedSIDs>
</role>
</roleMap>
<roleMap type="projectRoles">
<role name="test1" pattern=".*">
<assignedSIDs>
<sid>abc</sid>
<sid>zxc</sid>
</assignedSIDs>
</role>
<role name="test2" pattern=".*">
<permissions/>
<assignedSIDs>
<sid>abc</sid>
<sid>ghi</sid>
</assignedSIDs>
</role>
<role name="test3" pattern=".*">
<permissions/>
<assignedSIDs>
<sid>abc</sid>
</assignedSIDs>
</role>
</roleMap>
</authorizationStrategy>
</hudson>
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
>
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.
if you are not bound to xmlstarlet try to use xmllint otherwise adjustment of the xpath like shown below could help.
$ xmllint --xpath '//role//sid/../../@name' roles.xml
name="Employees" name="Others"
The xml file for this example looked like
<?xml version="1.0" ?>
<hudson>
<authorizationStrategy>
<roleMap>
<role name="Employees">
<assignedSIDs>
<sid>abc</sid>
<sid>bcd</sid>
</assignedSIDs>
</role>
<role name="Others">
<assignedSIDs>
<sid>abc</sid>
<sid>zxc</sid>
</assignedSIDs>
</role>
</roleMap>
</authorizationStrategy>
</hudson>
In short you can address parent childs attribute by addressing them with ../@*
.