Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

an Xpath question

 
jim yin
Ranch Hand
Posts: 111
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1183
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Jayadev Pulaparty
Ranch Hand
Posts: 662
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 111
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, guys. This is from whizlab's sample test.
Pretty tricky, I got to get used to it.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic