1

im looking to find an element in a schema based on the value of a variable (that changes each time i iterate). the catch is the element could be anywhere inside the schema.

for instance:

<...
  <foo>
    <bar>
       <bar1>BB</bar1>
       <bar2>CC</bar2>
    </bar>
    <rab>
       <rab1>DD</rab1> 
    </rab>
  </foo>
/...>

$attribute = bar1 (then the next iteration, $attribute may equal rab1)

how would i write an expression that could find me: .../foo/bar/$attribute

the closest thing i can find is ...//*[name()=$attribute] but it doesn't work. is there any other way?

Thanks for your help!

2
  • Does your XML contain namespaces (xmlns)? Commented Aug 18, 2011 at 13:21
  • "Doesn't work" is too vague. Does it throw an error? Does it return no nodes, when there are nodes it should return? Commented Aug 18, 2011 at 14:27

1 Answer 1

3

Although the question leaves out a lot of details that may be important, you could try changing name() to local-name():

...//*[local-name()='bar1']

and see if that fixes the problem. The return value of name() includes any prefix the element name has, which could cause it not to match the value of $attribute. (@Kirill was hinting at this.)

If that doesn't solve the problem, provide more context: What is the full XPath expression? How is it being used in XSLT? How do you know it "doesn't work"? (Give expected results and actual results.)

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

3 Comments

thanks for all your help. when i did //*[local-name()=string($attribute)] it worked
@David: I wonder if the $attribute variable contains something other than a string. I don't think that would make a difference, but it's hard to say, without knowing what unexpected behavior was occurring.
@David you can "accept" this answer since it solved your problem.

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.