本文介绍了xmlstarlet XPath的前pression选择一个结果而不是多个的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有结构,具有这样的XML文件:

I have an XML file having structure like this:

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

previously,我并没有给有问题的整体结构和我的解决方案得到了XPath的,没给,结果我的预期(我的系统上,但它给了谁回答我问题的人)在下面的链接search使用基于子节点值标记属性值XmlStarlet

如该链接所提到的,我想基于SID标签的价值发现作用的标记名称属性。对于例如:如果我搜索ABC,查询必须返回员工,为test1,test2的和TEST3

AS mentioned in that link, 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 Employees, test1, test2 and test3.

下面是我使用的脚本:

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

但它给了我唯一的员工。

but it gave me only 'Employees'.

我使用LINUX / bash的。请告诉我,我在XPATH前pression很想念。

I am using LINUX /bash. Please tell me, what I am missing in XPATH expression.

推荐答案

您使用的XPath和xmlstarlet命令,

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

的要求。

您XPath是正确的;您xmlstarlet命令行是正确的。重新检查你使用的文件名以及您正在运行的xmlstarlet的目录。重新检查您如何使用您的实际背景下xmlstarlet命令的结果。在你已经张贴了这个简单的形式,预期的结果是precisely的返回什么。

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注意到他xmlstarlet(V1.0.1)的版本,比我在上面的成绩单(xmlstarlet V1.5.0)所使用的年纪大。 升级到xmlstarlet最新版本解决了这个问题。

这篇关于xmlstarlet XPath的前pression选择一个结果而不是多个的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-26 08:07