xsl-list
[Top] [All Lists]

Re: [xsl] node() function

2014-06-23 22:01:55
There is an XSLT training course at Pluralsight, watching which should
help you find the correct answers to these questions:

http://www.pluralsight.com/training/Courses/TableOfContents/xslt-foundations-part1

On Mon, Jun 23, 2014 at 1:17 PM, Mailing Lists Mail
daktapaal(_at_)gmail(_dot_)com 
<xsl-list-service(_at_)lists(_dot_)mulberrytech(_dot_)com> wrote:
Need to know the behavior of the node() function… 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 haven’t 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 further…





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 = “c”/>

           </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>



<!—Other 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)

à





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/




-- 
Cheers,
Dimitre Novatchev
---------------------------------------
Truly great madness cannot be achieved without significant intelligence.
---------------------------------------
To invent, you need a good imagination and a pile of junk
-------------------------------------
Never fight an inanimate object
-------------------------------------
To avoid situations in which you might make mistakes may be the
biggest mistake of all
------------------------------------
Quality means doing it right when no one is looking.
-------------------------------------
You've achieved success in your field when you don't know whether what
you're doing is work or play
-------------------------------------
To achieve the impossible dream, try going to sleep.
-------------------------------------
Facts do not cease to exist because they are ignored.
-------------------------------------
Typing monkeys will write all Shakespeare's works in 200yrs.Will they
write all patents, too? :)
-------------------------------------
I finally figured out the only reason to be alive is to enjoy it.
--~----------------------------------------------------------------
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list
EasyUnsubscribe: http://lists.mulberrytech.com/unsub/xsl-list/1167547
or by email: xsl-list-unsub(_at_)lists(_dot_)mulberrytech(_dot_)com
--~--

<Prev in Thread] Current Thread [Next in Thread>