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 problem 2

Status
Not open for further replies.

JontyMC

Programmer
Nov 26, 2001
1,276
GB
I'm sure I don't need to use a variable here, but can't work out the XPath. How do you select an attribute of the context node when you have to navigate up and down the tree?

XML:
Code:
<?xml version="1.0" encoding="UTF-8"?>
<test>
	<row id="1">
		<item1>an item</item1>
		<item2>another item</item2>
	</row>
	<row id="2">
		<item1>an item</item1>
		<item2>another item</item2>
	</row>
	<x>
		<y id="2">
			<amount>100</amount>
		</y>
	</x>
</test>

XSL:
Code:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="[URL unfurl="true"]http://www.w3.org/1999/XSL/Transform">[/URL]
	<xsl:template match="/">
		<xsl:for-each select="test/row">
			<xsl:variable name="id" select="@id" />
			<xsl:value-of select="../x/y[@id= $id]/amount"/>
		</xsl:for-each>
	</xsl:template>
</xsl:stylesheet>

Thanks,

Jon
 
That looks ok.. The XPATH looks ok to me. And I've run it through my transformer and it prints out the right values...?


Another thing to try would be to do

Code:
 <xsl:value-of select = "//x/y[@id = $id]/amount"/>


but my guess is something else is wrong. ;)

matt

 
Yeah, I know it does work, but I was wondering if there was a way to say the same thing in a single XPath statement without using a variable.

Something along the lines of:

select " ../x/y/amount where ../x/y/@id equals ./@id "
looping through all the row elements as the context node.
 
Hmm.. re-reading your post..


Are you asking if theres anyway NOT to use a variable in there?

I dont think so. You can use relative paths in the axis, but it doesn't seem to work very well.

M

 
Thinking about why: I think its because you lose context as soon as you enter axis of a different level;

So, if you are selecting where ../x/y[@id = $something], as soon as you hit the axis[] the context is now ../x/y and not "row".

Does that make sense?

 
I figured it out:

Code:
<xsl:value-of select="self::node()[../x/y/@id = @id]/../x/y/amount"/>

Is there a neater way of writing this?
 
Why can't I write:

Code:
<xsl:value-of select=".[../x/y/@id = @id]/../x/y/amount"/>

I thought . was short for self::node()?
 
.. because it doesn't make sense?

The axis are qualifying the node that is currently in context, so although the expression may be true as soon as you leave the axis [] you loose the context again.

What this says is:

"Find me the current node (.) where the parent/x/y/@id equals the current node's (.) id and then find me the parents/x/y/amount " which makes no sense.

What you need to ask is "find me the ../x/y/amount where ../x/y/@id is equal to the current node's id". and you can't do that without going out of context.





 
.. or using a variable to store the current context nodes' id ;)

(forgot to add that bit;)

 
hang on i missed a post.. you say self::node() works?!

Wow. thats odd.

... *tap tap tap* ...

no it doesn't work!!

if you add "id=1" like this:

Code:
<?xml version="1.0" encoding="UTF-8"?>
<test>
    <row id="1">
        <item1>an item</item1>
        <item2>another item</item2>
    </row>
    <row id="2">
        <item1>an item</item1>
        <item2>another item</item2>
    </row>
    <x>
    	<y id="1">
            <amount>100</amount>
        </y>
        <y id="2">
            <amount>100</amount>
        </y>
    </x>
</test>

and then try it, you get two 100's printed out!!

matt

 
oops sorry, change the second one (y[@id = 2]) to 200 or something. you still get two 100's printed out.

m

 
Oh, bugger. You're right. Guess it can't be done. Variable it is then.

Cheers,

Jon
 
There's nothing wrong with using variables.
Anyways:
<xsl:value-of select="../x/y[@id=current()/@id]/amount"/>
 
Very good jel. I hate using variables when I know I don't need to.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top