search tag attribute value based on child node value using XmlStarlet

前提是你 提交于 2019-12-12 02:46:43

问题


I have a xml file having structure something like below:

        <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>

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 this below link and got half of the solution .

XMLStarlet Return attribute based on value (Reverse lookup)

I also refered 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?


回答1:


The XPath expression you can use is //role[.//sid = 'abc' ]/@name, the command line for xmlstarlet is xmled.exe sel -t -v "//role[.//sid = 'abc']/@name" input.xml (I think it depends on your command line shell which quote character you need to use to wrap the XPath expression).




回答2:


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



来源:https://stackoverflow.com/questions/33037486/search-tag-attribute-value-based-on-child-node-value-using-xmlstarlet

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!