aspose file tools*
The moose likes Product and Other Certifications and the fly likes XMLWhiz test ... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of JavaScript Promises Essentials this week in the JavaScript forum!
JavaRanch » Java Forums » Certification » Product and Other Certifications
Bookmark "XMLWhiz test ..." Watch "XMLWhiz test ..." New topic
Author

XMLWhiz test ...

Svetlana Petrova
Ranch Hand

Joined: Sep 26, 2002
Posts: 50
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>
Jayadev Pulaparty
Ranch Hand

Joined: Mar 25, 2002
Posts: 662
Your analysis above is flawless. I get 7. I'm using xalan's java xslt processor.
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
"//a[not(following::x=.)]
what does this fetch?? Can someone explain clearly.
Jayadev Pulaparty
Ranch Hand

Joined: Mar 25, 2002
Posts: 662
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

Joined: Nov 22, 2008
Posts: 18944
//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

Joined: Mar 25, 2002
Posts: 662
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.
patrick gee
Greenhorn

Joined: Sep 30, 2002
Posts: 9
Good JOB Jaydev!


The greatest risk is not taking one!
Svetlana Petrova
Ranch Hand

Joined: Sep 26, 2002
Posts: 50
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

Joined: Mar 25, 2002
Posts: 662
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 agree. Here's the link: http://aspose.com/file-tools
 
subject: XMLWhiz test ...