File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes XML and Related Technologies and the fly likes XPath with variables Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "XPath with variables" Watch "XPath with variables" New topic

XPath with variables

Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Hi there,
I have a XML file that looks like this:

I have a stylesheet, that looks like that:

I would like to change the XPath Expression to point the language that is in curLanguage. I tried

but without success. How does the correct XPath look like?
;-) stw

Disabled smiles.
- m
[ June 29, 2004: Message edited by: Madhav Lakkapragada ]
Nimchi Yung
Ranch Hand

Joined: Jan 27, 2004
Posts: 71
<xsl:template match="priorities"><xsl aram name="curLanguage"/><xsl aram name="curPrio"/><xsl:value-ofselect="priority-key[level=$curPrio]/English"/></xsl:template>

I think your XPath expr should be like this:

Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Hi Nimchi,
thx for taking the time to reply. From your reply I see, that I didn't phrase my question clearly. The xpath
actually does work well. What I want to achieve is to replace English by the content that is in $curLanguage.
This is a very fundamental problem for me: how to construct an xPath expression where the name of a node is a variable. I tried to poke around with the name() function but no luck. The w3school tutorials didn't answer this question too (or I missed it).
;-) stw
Porky Liu

Joined: Jan 30, 2002
Posts: 22
maybe you can get all the element under priority-key, and then get the correct element with xsl:if test current() = $whatever, just like this

It's not easy, but I do think it can work
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Hi Porky Liu,

thx for your reply. I did a little reevaluating of XLST concepts. You actually can't compare a node to a string. So the solution is to look for something that is a string inside the node. The riddle is solved when you use the name() function. to the answer is:

;-) stw
[ July 07, 2004: Message edited by: Stephan H. Wissel ]
I agree. Here's the link:
subject: XPath with variables
It's not a secret anymore!