Dave Johnson
Dave Johnson

Reputation: 21

How to use a dynamic path expression in Xquery

I'm fairly new to xquery so please bear with me as I try to best ask this question.

when i use the following statement (to select a sequence) it works just fine:

let $parts := doc("soap.xml")/soapenv:Envelope/soapenv:Body/ns0:PartDetails/ns0:partName

this loads all elements (as a sequence) into the $parts variable just how i'd like but...

i want to assign the "/ns0:partName" dynamically. I went thru all the Qname functions thinking they'd somehow help but no such luck. after 2 days of struggling i'm hoping one of you can point me in the right direction.

Upvotes: 2

Views: 1220

Answers (2)

Oliver Hallam
Oliver Hallam

Reputation: 4262

As a simplication to vincent's answer you can just check that an xs:QName matches the node-name() of the node

let $elementName = QName("urn:namespace", "PartDetails")
...
let $parts := doc("soap.xml")/soapenv:Envelope/soapenv:Body/*[node-name() = $elementName]/ns0:PartName

Upvotes: 2

Vincent Biragnet
Vincent Biragnet

Reputation: 2998

In your path expression, you can replace element with * and add predicates in order to filter the results, predicates may contain dynamic variables. For example :

*[local-name()=$someName and namespace-uri()=$someURI]

Upvotes: 3

Related Questions