• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

XMLWhiz test ...

 
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,
Can somebody give a good explanation on a right answer to the question below (from XMLWhiz test)?
When I tested this, I've got 9 values in output (?!!!).
Thanks a lot,
Svetlana.

Q:
====
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>
Select from:
A) 7
B) 8
C) 3
D) 5
E) error
==========
A:
==========
A is correct. It will take 3 values from first selection "a[not(following::x=.)]" (these three coming from second "B"), and 4 values from second part "x[not(preceding::a=.)]" 2 each from both "B"). All the selected elements are marked in the XML document below:
---------------------XML Document-----------------------
<Root>
<B>
<a>1</a>
<a>2</a>
<a>1</a>
<x>3</x>(selected)
<x>4</x>(selected)
<x>2</x>

</B>
<B>
<a>3</a>(selected)
<a>1</a>
<a>2</a>(selected)
<a>3</a>(selected)
<x>5</x>(selected)
<x>4</x>(selected)
<x>1</x>
</B>
</Root>
 
Ranch Hand
Posts: 662
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your analysis above is flawless. I get 7. I'm using xalan's java xslt processor.
 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"//a[not(following::x=.)]
what does this fetch?? Can someone explain clearly.
 
Jayadev Pulaparty
Ranch Hand
Posts: 662
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It gets all the "a" nodes in the tree and checks if any of the values of the "x" nodes that follow them in the tree doesn't (not) have the same value as them ".". This is the selection criterion for an "a" node to end up in the output list.
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
//a[not(following::x=.)]
does it take each value of a and compare it with each value of x
so for eg
<a>1</a>compares with all the values of x from both <B>/<B> elements or does it in it s own B element?
Please clarify...
 
Jayadev Pulaparty
Ranch Hand
Posts: 662
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can look at it this way.
//a gets all the "a" nodes in the xml document.
The stuff inside "[.....]" is called the predicate (kind of filter) i.e., the condition that any "a" should satisfy (condition should evaluate to be true) in order to be selected as part of the list. The predicate here is "not(following::x=.)". Each "a" node will be tested to see if any of the list of following "x" nodes have the same value as itself "." . If not, then the predicate conditon will evaluate to "true" and that "a" node will be selected to be part of the final list of survivors. Hope that this is clear. Please come back if you have any further doubt.
 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Good JOB Jaydev!
 
Svetlana Petrova
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot, guys!
Eventually, I also logically figured that out and got 7 values in the output. Though running this example with xalan XSLT processor still returns 9 values.
Take care,
Svetlana.
 
Jayadev Pulaparty
Ranch Hand
Posts: 662
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Svetlana,
Can you please run the following stylesheet on the xml data file you have and see what the count returns. I'm a little suspicious about the content of ur xml document
================================================
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<xsl:call-template name="v3"/>
</xsl:template>
<xsl:template name="v3">
<xsl:value-of
select="count(//a[not(following::x=.)]|//x[not(preceding::a=.)] )"/>
</xsl:template>
</xsl:stylesheet>
================================================
The count also returns 7. Even i get 7 values as per the previous stylesheet you mentioned here.
 
I am not young enough to know everything. - Oscar Wilde This tiny ad thinks it knows more than Oscar:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic