2

I have searched thoroughly and not found any examples or guidance on how I can query and retrieve the values of multiple XML siblings.

There are plenty of examples on how to retrieve a single sibling value.

For example given the following XML fragment:

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

If I want to retrieve the value of <AnnualSales>, I can execute the following statement in query analyzer:

SELECT Survey_untyped.query('/StoreSurvey/AnnualSales') 
FROM Stores;

The result set will properly display

<AnnualSales>800000</AnnualSales>

However, what if I want to retrieve both <AnnualSales> AND <AnnualRevenue>? How would I do that?

The query should provide a result set looking like:

<AnnualSales>800000</AnnualSales>
<AnnualRevenue>80000</AnnualRevenue>

Or what if I want three of the sibling values “BankName” in addition to those values? Result set would look like this:

  <AnnualSales>800000</AnnualSales>
  <AnnualRevenue>80000</AnnualRevenue>
  <BankName>United Security</BankName>

Does anyone know the answer to that?

1 Answer 1

2

This XPath, which uses the self:: axis,

/StoreSurvey/*[self::AnnualSales or self::AnnualRevenue or self::BankName]

will select

<AnnualSales>800000</AnnualSales>
<AnnualRevenue>80000</AnnualRevenue>
<BankName>United Security</BankName>

from your XML, as requested.

Sign up to request clarification or add additional context in comments.

4 Comments

Thank you, I have not seen any references that use that use that syntax "self" etc. Could you please post a link to that? (I have been looking at standard x-query examples, etc.)
actually, now that i look I see "self" in there, but it was described as selecting "the current node" Never saw any examples like your's but thank you very much, you saved me a lot of time. Thanks again.
@SharmaVenkati: Answer updated with self:: axis link per your request.
You're welcome. Please accept this answer if it's helped. Thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.