[xsl] node() function

Subject: [xsl] node() function
From: "Mailing Lists Mail daktapaal@xxxxxxxxx" <xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx>
Date: Mon, 23 Jun 2014 20:17:14 -0000
Need to know the behavior of the node() functionb& This mail contains
two parts. Part 2 is kind of a corollary of part 1.


I have the following XML

<Comp>

            <a>Universal</a>

            <b>HSBC</b>

            <c>Disney </c>

            <d>Barclays</d>

</Comp>



PART1



I wanted to test some stylesheet behaviors .. I used three stylesheets :



Stylesheet 1



<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
xmlns:fo="http://www.w3.org/1999/XSL/Format"xmlns:xs="http://www.w3.org/2001/
XMLSchema"
xmlns:fn="http://www.w3.org/2005/xpath-functions";>

            <xsl:template match="node()|@*">

                        <xsl:apply-templates/>

            </xsl:template>

</xsl:stylesheet>





Stylesheet 2





<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
xmlns:fo="http://www.w3.org/1999/XSL/Format"xmlns:xs="http://www.w3.org/2001/
XMLSchema"
xmlns:fn="http://www.w3.org/2005/xpath-functions";>

            <xsl:template match="*">

                        <xsl:apply-templates/>

            </xsl:template>

</xsl:stylesheet>





Stylesheet 3

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
xmlns:fo="http://www.w3.org/1999/XSL/Format"xmlns:xs="http://www.w3.org/2001/
XMLSchema"
xmlns:fn="http://www.w3.org/2005/xpath-functions";>

            <xsl:template match="/">

                        <xsl:apply-templates/>

            </xsl:template>

</xsl:stylesheet>

I was expecting all the three stylesheets to copy the text nodes to the
target.

While the stylesheet2 and stylesheet 3 did that, the stylesheet 1 did
not output anything ( wondered why?? )..

What I was thinking the stylesheet1 will do is :



1.       Match any node() or the attribute node .

2.       Apply template to the children and self

3.       Default template rule will kick in  as I havenbt mentioned
any node. This will :

a.       Do value-of select for text nodes

b.      Do apply-templates for the element nodes ( * )



With that, I was expecting

            Universal

            HSBC

            Disney

            Barclays



This is what the Stylesheet 2 and Stylesheet 3 produces



SO the answer was in the fact that node() does not match text()??

So I added





<xsl:template match="text()">

<xsl:value-of select="."/>

</xsl:template>



This came with what I wanted.. ( both happy and disappointed )

Happy as it brought me to a logical end, and disappointed as it dint
work like I initially thought it would.



STOPPING HERE : Needed some insights into what I just wrote before
going furtherb&





PART2



Further, This  leads me   to a (dangerous) way of saying : Select only
node c and nothing else.



I could do :



1.       The normal intuitive way  ( Approach A)



            <xsl:template match="/">

            <xsl:apply-templates select = bcb/>

           </xsl:template>

<xsl:template match="c">

<xsl:value-of select="."/>

</xsl:template>



2.       The somewhat dangerous way (based on the observation in PART1
 ) ( Approach B )



                        <xsl:template match="node()|@*">

                                   <xsl:apply-templates/>

                       </xsl:template>

<xsl:template match="c">

<result>

<xsl:value-of select="."/>

</result>

</xsl:template>



<!bOther nodes will not be cared for or other nodes does nothing (but
why??)

I would have thought, the other nodes will be matched, and text nodes
be printed, but did not, as in PART1.  (= reasons for calling Part 2
as corollary to part 1)

C 





Approach B is not intuitive for me. But somehow doing the same thing
as approach A.  Although I will NEVER use the approach B.



Any Idea why this is so.. are there situations where approach B wont
work? I want to think approach B is Wrong and will fail some how..





Dak/

Current Thread