我进行了彻底的搜索,但没有找到任何有关如何查询和检索多个 XML 同级的值的示例或指导。
有很多关于如何检索单个同级值的示例。
例如,给出以下 XML 片段:
<StoreSurvey>
<AnnualSales>800000</AnnualSales>
<AnnualRevenue>80000</AnnualRevenue>
<BankName>United Security</BankName>
<BusinessType>BM</BusinessType>
<YearOpened>1996</YearOpened>
<Specialty>Mountain</Specialty>
<SquareFeet>21000</SquareFeet>
<Brands>2</Brands>
<Internet>ISDN</Internet>
<NumberEmployees>13</NumberEmployees>
<Products Type="Bikes">
<Product>Mountain</Product>
<Product>Road</Product>
<Product>Racing</Product>
</Products>
<Products Type="Clothes">
<Product>Jerseys</Product>
<Product>Jackets</Product>
<Product>Shorts</Product>
</Products>
</StoreSurvey>
如果我想检索 <AnnualSales>
的值,我可以在查询分析器中执行以下语句:
SELECT Survey_untyped.query('/StoreSurvey/AnnualSales')
FROM Stores;
结果集将正确显示
<AnnualSales>800000</AnnualSales>
但是,如果我想检索 <AnnualSales>
该怎么办?和 <AnnualRevenue>
?我该怎么做呢?
查询应提供如下所示的结果集:
<AnnualSales>800000</AnnualSales>
<AnnualRevenue>80000</AnnualRevenue>
或者,如果除了这些值之外我还想要三个同级值“BankName”怎么办?结果集如下所示:
<AnnualSales>800000</AnnualSales>
<AnnualRevenue>80000</AnnualRevenue>
<BankName>United Security</BankName>
有人知道答案吗?
最佳答案
此 XPath 使用 self::
axis ,
/StoreSurvey/*[self::AnnualSales or self::AnnualRevenue or self::BankName]
将选择
<AnnualSales>800000</AnnualSales>
<AnnualRevenue>80000</AnnualRevenue>
<BankName>United Security</BankName>
根据要求从您的 XML 中获取。
关于sql-server - 如何检索多个 XML 同级 - SQL Server,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42404464/