Peter Lange
Peter Lange

Reputation: 2886

XPATH Query Recursion

I have an XML Document with Nodes that can appear recursively within other nodes of the same type. For example:

<root>
   <Categories>
   <Category>
      <CategoryId>1</CategoryId>
      <CategoryName>Cat 1</CategoryName>
      <ChildCategories>
          <Category>
               <CategoryId>3</CategoryId>
               <CategoryName>Cat 3</CategoryName>
          </Category>
      </ChildCategories>
   </Category>
   <Category>
        <CategoryId>5</CategoryId>
        <CategoryName>Cat 5 </CategoryName>
   </Category>
   </Categories>
</root>

As such, I need to be able to query for a specific Category or Child Category (or even Child of a Child, etc) by its CategoryID value. Is this doable in XPATH?

TIA

Upvotes: 1

Views: 4556

Answers (1)

John Kugelman
John Kugelman

Reputation: 362037

No problem. Use // to search the entire XML document and square brackets to filter by category id:

//Category[CategoryId=1]

Upvotes: 9

Related Questions