1

I have a xml file having structure something like below input.xml:

<myxml>
    <role name="test" pattern=".*">
        <assignedSIDs>
            <sid>abc</sid>
            <sid>cde</sid>
            <sid>def</sid>
        </assignedSIDs>
    </role>

    <role name="test1" pattern=".*">
        <assignedSIDs>
            <sid>abc</sid>
            <sid>zxc</sid>
            <sid>vbn</sid>
        </assignedSIDs>
    </role>

    <role name="test2" pattern=".*">
        <assignedSIDs>
            <sid>abc</sid>
            <sid>hex</sid>
            <sid>oct</sid>
        </assignedSIDs>
    </role>
</myxml>

I want to find role tag name attribute based on value of sid tag. for eg: if I search for abc, the query must return test, test1 and test2

I referred to this below link and got half of the solution: XMLStarlet Return attribute based on value (Reverse lookup)

I also referred to this too: http://xmlstar.sourceforge.net/doc/UG/xmlstarlet-ug.html

but I didn't got any example that would support my requirement.

Is there something I can do?

StackzOfZtuff
  • 2,534
  • 1
  • 28
  • 25

2 Answers2

4

The XPath expression you can use is:
//role[.//sid = 'abc']/@name

The command line for xmlstarlet is:

C:\> xmled.exe sel -t -v "//role[.//sid = 'abc']/@name" input.xml
test
test1
test2

(I think it depends on your command line shell which quote character you need to use to wrap the XPath expression).

StackzOfZtuff
  • 2,534
  • 1
  • 28
  • 25
Martin Honnen
  • 160,499
  • 6
  • 90
  • 110
  • Thanks @Martin for your answer. It tried your xpath expression.. but it gave me only "test" and not "test1" and "test2" . Could you please confirm. – srikanth jagdhane Oct 09 '15 at 13:51
  • When I try the XPath I posted with xmlstarlet on Windows with the command line I posted it outputs `test test1 test2`. – Martin Honnen Oct 09 '15 at 13:55
  • Please copy the above xml lines. This is the structure of my xml file. there are two types of tag, one is of global role and another is of project role. i want to find the user in both tags and want the role tag attribute name. – srikanth jagdhane Oct 09 '15 at 14:15
  • If you have a new question then start a new one. If that code in the comment is supposed to be added to your original question then edit the question and add the sample there with proper formatting. But I have posted my XPath suggestion together with a command line for xmlstarlet, I am not sure what else to do. Maybe someone else can help you further if you explain which problem you still have. – Martin Honnen Oct 09 '15 at 14:21
  • Thanks Martin. I'll remember that. Could you please give me some reference from where i can get some examples on XPATH expression using XmlStarlet – srikanth jagdhane Oct 09 '15 at 14:26
0

Even though the XPATH given by @martin-honnen was correct, i was not getting the expected output. This was due to old version of xmlstarlet 1.0.1 that was installed in my system. As I upgraded to 1.6.1, problem was solved.
here is the link, where @kjhughes helped me to find the solution xmlstarlet XPath expression selects single result rather than multiple

Community
  • 1
  • 1