Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

XPath question?????

Status
Not open for further replies.

dako

Programmer
Jan 7, 2005
3
US
Hi, I need some help here. I have the following

...
<page>
<objectives>
<objective>Objective 1</objective>
<objective>Objective 2</objective>
</objective>
<page>
<page>
<objref num="2"/>
...
</page>
<page>
<objref num="1"/>
...
</page>
...

I need an XPATH expression (1.0 or 2.0) that automatically retrives the contents of the 'objective[n]' element from the 'objref' element for an id="n" ('n' is an integer)

for example if <objref num="1"/> I need the contents inside of the first objective element

Is that possible?
I tried something like this from the 'objref' element:

(../../page[1]/objectives/objective)[@num]

but this does't return anything.

an expression like:
(../../page[1]/objectives/objective)[1]

returns without problems the value of the first objective "Objective 1"

Any ideas?

 
<xsl:for-each select="//page">
<xsl:value-of select="//page[1]/objectives/objective[number(current()/objref/@num)]"/>
</xsl:for-each>
 
btw...
If you cut and pasted that xml, it is broken...
the objectives closing tag needs an s on the end...
Code:
<objectives>
  <objective>Objective 1</objective>
  <objective>Objective 2</objective>
</objective[COLOR=red]s[/color]>

If you just typed this as an example, please disregard...


PROGRAMMER: (n) Red-eyed, mumbling mammal capable of conversing with inanimate objects.
 
Many Thanks guys for your help. But I still can't make it work. The current() function would be the perfect solution but that function is not a XPATH function but a XSLT function and I'm not using XSLT. I'm using Altova's Stylevision Stylesheets (Authentic) and those stylesheets supports only XPATH 1.0 or 2.0 functions.

I really appreciate your help with this one!!! Other ideas to make it work?

Thanks for your help.
 
instead of:
../../page[1]/objectives/objective)[@num]

try:
../../page[1]/objectives/objective)[./@num]

reference...
(under "Location Path Abbreviated Syntax")

How are you looping through the <objref> tags, anyway?

It woud be alot easier if you can access msxml dom objects... can you?

and just out of curiosity, is there any reason you are using...
<objref num="1"/>
instead of...
<objref>1</objref>

I can understand if you are using xml from someone else, though it would be easier to access if you used the tag value instead...


PROGRAMMER: (n) Red-eyed, mumbling mammal capable of conversing with inanimate objects.
 
Ok, after hours and hours of much pain and frustration I found a solution to my problem. I dont know if it is the best solution but it works.

First of all special thanks to CubeE101 and Jal for your help.

CubeE101 giving a little bit of context to my situation. I'm using Altova's StyleVision Stylesheets to generate a GUI that will allow non-programmers to modify a XML file for the company I work for. Altova has its own way to apply their style sheets(sps) but they just accept XPATH expressions and there is no way to use any of the XSLT features like variables, other functions or MSDOM and so on...

ok the solution is as follows:
The following XPATH expression returns a sequence of all the contents of the "objective" elements

(../../page[1]/objectives/objective)

using the "subsequence" function I can specify the element I want doing as follows:

subsequence ((../../page[1]/objectives/objective),@num,@num)

the first @num specifies the start element of the subsequence and the second @num the ending element of the subsequence so if I have @num="1" that expression will return a subsequence starting with the first element and ending with the first element and so on and so forth...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top