xml 如何在XQuery中选择节点的属性值?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16050023/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-06 13:59:21  来源:igfitidea点击:

how to select attribute value of a node in XQuery?

xmlxquery

提问by Itz.Irshad

In below XML:

在下面的 XML 中:

<company>
    <customers>
    <customer cno="2222">
            <cname>Charles</cname>
            <street>123 Main St.</street>
            <city>Wichita</city>
            <zip>67226</zip>
            <phone>316-636-5555</phone>
        </customer>
        <customer cno="1000">
            <cname>Bismita</cname>
            <street>Ashford Dunwoody</street>
            <city>Wichita</city>
            <zip>67226-1555</zip>
            <phone>000-000-0000</phone>
        </customer>     
    </customers>
</company>

I need to get the customer's no which is an attribute. In XPath I know it is /company/customers/customer/@cno, in XQuery I've tried below expression but didn't work for me:

我需要得到客户的否,这是一个属性。在 XPath 中,我知道它是/company/customers/customer/@cno,在 XQuery 中,我尝试了以下表达式,但对我不起作用:

for $c in /company/customers/customer
return $c/@cno

回答by Navin Rawat

You should use data to pick attribute value:-

您应该使用数据来选择属性值:-

for $c in /company/customers/customer
return data($c/@cno)

回答by Nikunj Vekariya

You can also use stringto get attribute value:

您还可以使用string来获取属性值:

for $c in /company/customers/customer
    return $c/@cno/string()