Problem with XPath with child name as a variable

T

tslettebo

Hi.

I don't know if the following is possible (I haven't found it in
examples/tutorials, or using a search, but then, it's hard to tell what
to search for), but it seems so obvious, so I'm wondering if I just
don't know the right syntax for it:

I'm using XSLT and have an input XML like this:

<root>
<name1><value>1</value></name1>
<name2><value>2</value></name2>
<name3><value>3</value></name3>
</root>

I'd like to read the contents of the right <value> node, with the
parent name given with a variable/string, something like:

/root/$name/value // This gives "1", "2" or "3", depending on whether
$value has "name1", "name2" or "name3".

However, the above doesn't work, at least on Sablotron. Any ideas how
to accomplish this?

Regards,

Terje
 
M

Martin Honnen

<root>
<name1><value>1</value></name1>
<name2><value>2</value></name2>
<name3><value>3</value></name3>
</root>

I'd like to read the contents of the right <value> node, with the
parent name given with a variable/string, something like:

/root/$name/value // This gives "1", "2" or "3", depending on whether
$value has "name1", "name2" or "name3".

You can do that like this
/root/*[local-name() = $name]/value
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,813
Latest member
lawrwtwinkle111

Latest Threads

Top