wood burning stoves 2.0*
The moose likes XML and Related Technologies and the fly likes an Xpath question 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 "an Xpath question" Watch "an Xpath question" New topic
Author

an Xpath question

jim yin
Ranch Hand

Joined: Aug 15, 2002
Posts: 111
Hi, I am pretty confused about the following question:

How many elements will be output when the following XSL sheet is applied to the following XML document?
--------------------XSL Document-------------------------
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<xsl:element name='root'>
<xsl:call-template name="v3"/>
</xsl:element>
</xsl:template>
<xsl:template name="v3">
<xsl:variable name="v3List" select="//a[not(following::x=.)]|//x[not(preceding::a=.)] "/>
<xsl:for-each select="$v3List">
<a><xsl:value-of select="."/></a>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
--------------------XML Document-------------------------
<Root>
<B>
<a>1</a>
<a>2</a>
<a>1</a>
<x>3</x>
<x>4</x>
<x>2</x>
</B>
<B>
<a>3</a>
<a>1</a>
<a>2</a>
<a>3</a>
<x>5</x>
<x>4</x>
<x>1</x>
</B>
</Root>


The answer is 7.
First, is this a valid expression?

a[not(following::x=.)]

not() function will negate the argument, and return a boolean value, can a boolean value be put into a pair of square brackets following an element? And can anyone explain how the results come out like this. Thanks
Dan Drillich
Ranch Hand

Joined: Jul 09, 2001
Posts: 1167
Jim,
Good one - where did you get it from?

The square brackets hold a predicate and a predicate is a boolean.
The reason the first three elements don't show up in the output is because the following x elements hold the values 1 and 2.
the first digit in the result is 3 since there is no 3 in the preceding a's, etc.
Cheers,
Dan


William Butler Yeats: All life is a preparation for something that probably will never happen. Unless you make it happen.
Jayadev Pulaparty
Ranch Hand

Joined: Mar 25, 2002
Posts: 662
Good explanation Dan.
Jim,
The expression
preceding::a=.
compares the string value of the current context node . with that of each of the nodes of the preceding::a axis. By string value it means the text node value.
jim yin
Ranch Hand

Joined: Aug 15, 2002
Posts: 111
Thanks, guys. This is from whizlab's sample test.
Pretty tricky, I got to get used to it.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: an Xpath question
 
Similar Threads
Confused with Xpath expression! Need Help.
Need help with some sample questions...
Can Not XSL-Transform Something so Simple.
MOCK QUESTIONS !!
XMLWhiz test ...