Help with XSL

Discussion in 'XML' started by Kasp, Oct 9, 2003.

  1. Kasp

    Kasp Guest

    Hi,
    I have an XSL here...and though I understand XML, I am having a hard time
    understanding what this XSL does?
    Any help will be appreciated.
    TIA

    <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    version="1.0">
    <xsl:eek:utput indent="yes"/>
    <xsl:key name="pageKey" match="//HierarchyMember" use="@PageKey"/>
    <xsl:template match="/">
    <Root>
    <xsl:apply-templates select="//HierarchyMember"/>
    </Root>
    </xsl:template>
    <xsl:template match="HierarchyMember">
    <xsl:if test="count(key('pageKey', @PageKey)) > 1">
    <xsl:copy>
    <xsl:attribute
    name="PageKey"><xsl:value-of-select="@PageKey"/></xsl:attribute>
    <xsl:attribute name="Count"><xsl:value-of
    select="count(key('pageKey',@PageKey))"/></xsl:attribute>
    </xsl:copy>
    </xsl:if>
    <xsl:apply-templates select="HierarchyMember"/>
    </xsl:template>


    </xsl:stylesheet>


    --
    "Accept that some days you are the pigeon and some days the statue."
    "A pat on the back is only a few inches from a kick in the butt." - Dilbert.
    Kasp, Oct 9, 2003
    #1
    1. Advertising

  2. Kasp

    Martin Boehm Guest

    "Kasp" <> wrote in message
    news:bm3onc$3hb$

    > <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    > version="1.0">


    The XSL declaration with the namespace, this is clear, I quess.

    > <xsl:eek:utput indent="yes"/>


    Advises the XSL Processor to indent output tags to make them somewhat
    more readable.

    > <xsl:key name="pageKey" match="//HierarchyMember" use="@PageKey"/>


    Creates a key (think of an associative array) named pageKey from all
    HierarchyMember elements anywhere in the document, using their PageKey
    attribute as association.

    > <xsl:template match="/">
    > <Root>
    > <xsl:apply-templates select="//HierarchyMember"/>
    > </Root>
    > </xsl:template>


    Matches the root node, puts a Root element around the output and begins
    processing all HierarchyMember elements in the documant one by one.

    The next template is executed once per HierarchyMember element:

    > <xsl:template match="HierarchyMember">
    > <xsl:if test="count(key('pageKey', @PageKey)) > 1">

    ------------------------------------------------^
    This should be escaped with &gt;

    Tests if there is more than one HierarchyMember element with a PageKey
    attribute equal to the currently processed HierarchyMember.
    The key() function returns all node of the previously created key that
    have the value of the current PageKey attribute associated with them.

    If this is true, copy the current element into the output:
    > <xsl:copy>


    ....but add two attributes - one containing the current PageKey:

    > <xsl:attribute name="PageKey">
    > <xsl:value-of-select="@PageKey"/>
    > </xsl:attribute>


    ....and one containing the count of the HierarchyMember elements havin
    the same PageKey as the current one:

    > <xsl:attribute name="Count">
    > <xsl:value-of select="count(key 'pageKey',@PageKey))"/>
    > </xsl:attribute>
    > </xsl:copy>
    > </xsl:if>


    End if.

    > <xsl:apply-templates select="HierarchyMember"/>


    This is senseless. The template is already repeatedly called by the one
    that matches the document root.

    > </xsl:template>


    End of HierarchyMember template.

    > </xsl:stylesheet>


    End of stylesheet.

    What the styleheet does is counting how many HierarchyMember elements
    with duplicate PageKeys exist in the source .

    I don't know if it is a bug, but I guess: The xsl:if will cause
    duplicate HierarchyMember elemets to appear in the output, as may as
    there are duplicate ones in the input. That means, if there are two
    HierarchyMember's having PageKey="1", the outpot will be

    <HierarchyMember PageKey="1" Count="2" />
    <HierarchyMember PageKey="1" Count="2" />

    because there are two elements passing the "count(key('pageKey',
    @PageKey)) > 1" test, which are processed one by one, so the condition
    is true twice during the transformation process.

    Martin
    Martin Boehm, Oct 9, 2003
    #2
    1. Advertising

  3. Excellent explanation!

    Only two minor things:

    > The next template is executed once per HierarchyMember element:
    >
    > > <xsl:template match="HierarchyMember">
    > > <xsl:if test="count(key('pageKey', @PageKey)) > 1">

    > ------------------------------------------------^
    > This should be escaped with &gt;


    Not really. "<" and "&" must be escaped, not ">".
    .. . . . . . . . . . . . .

    > End if.
    >
    > > <xsl:apply-templates select="HierarchyMember"/>

    >
    > This is senseless. The template is already repeatedly called by the one
    > that matches the document root.


    Yes this is obviously an error, but depending on the source.xml this may or
    may not affect the result of the transformation.

    For example, if a "HierarchyMember" does not have children which are
    "HierarchyMember", then the above xsl:apply-templates will not be applied on
    any nodes.


    =====
    Cheers,

    Dimitre Novatchev.
    http://fxsl.sourceforge.net/ -- the home of FXSL
    Dimitre Novatchev, Oct 9, 2003
    #3
  4. Kasp

    Martin Boehm Guest

    "Dimitre Novatchev" <> wrote in message
    news:bm4cra$i1nm8$-berlin.de

    > Excellent explanation!


    Thanks! :eek:)

    >>> <xsl:if test="count(key('pageKey', @PageKey)) > 1">

    >> -----------------------------------------------^
    >> This should be escaped with &gt;

    >
    > Not really. "<" and "&" must be escaped, not ">".


    I could say "That's why I said `should`." now. ;-)
    But actually I was not aware of that (as I always escape tag brackets),
    and I did not run the code.

    >>> <xsl:apply-templates select="HierarchyMember"/>

    >>
    >> This is senseless. The template is already repeatedly called by the
    >> one that matches the document root.

    >
    > Yes this is obviously an error, but depending on the source.xml this
    > may or may not affect the result of the transformation.
    >
    > For example, if a "HierarchyMember" does not have children which are
    > "HierarchyMember", then the above xsl:apply-templates will not be
    > applied on any nodes.


    I am not sure if I get that. You say it will _not_ be applied if there
    are no matching children. That is clear. This implies it would be
    applied if there were, that is clear, too.

    But I can't see what sense it would make (at least in this context) to
    recursively apply the template, as it is already applied to each and
    every HierarchyMember by the first (root-matching) one. Would that not
    produce even more duplicate output, as any child HierarchyMember is hit
    redundantly?

    Martin
    Martin Boehm, Oct 9, 2003
    #4
  5. "Martin Boehm" <> wrote in message
    news:3f85bf37$0$6599$-online.net...
    > "Dimitre Novatchev" <> wrote in message
    > news:bm4cra$i1nm8$-berlin.de
    >
    > > Excellent explanation!

    >
    > Thanks! :eek:)
    >
    > >>> <xsl:if test="count(key('pageKey', @PageKey)) > 1">
    > >> -----------------------------------------------^
    > >> This should be escaped with &gt;

    > >
    > > Not really. "<" and "&" must be escaped, not ">".

    >
    > I could say "That's why I said `should`." now. ;-)
    > But actually I was not aware of that (as I always escape tag brackets),
    > and I did not run the code.
    >
    > >>> <xsl:apply-templates select="HierarchyMember"/>
    > >>
    > >> This is senseless. The template is already repeatedly called by the
    > >> one that matches the document root.

    > >
    > > Yes this is obviously an error, but depending on the source.xml this
    > > may or may not affect the result of the transformation.
    > >
    > > For example, if a "HierarchyMember" does not have children which are
    > > "HierarchyMember", then the above xsl:apply-templates will not be
    > > applied on any nodes.

    >
    > I am not sure if I get that. You say it will _not_ be applied if there
    > are no matching children. That is clear. This implies it would be
    > applied if there were, that is clear, too.
    >
    > But I can't see what sense it would make (at least in this context) to
    > recursively apply the template, as it is already applied to each and
    > every HierarchyMember by the first (root-matching) one. Would that not
    > produce even more duplicate output, as any child HierarchyMember is hit
    > redundantly?


    At the scope of the:

    > <xsl:apply-templates select="HierarchyMember"/>


    the current node is an "HierarchyMember" element.

    If no "HierarchyMember" element has a "HierarchyMember" child, then the
    "select" attribute above will not select any node and, therefore, no
    template will be applied.

    Therefore, in this case no additional (duplicate) output will be produce.

    Of course, the above xsl:apply-templates is an error -- we only talk here
    whether the output transformation will be affected by this error.

    Unfortunately, for some source xml documents with the structure described
    above the output of the transformation will not be affected by the error --
    thus the programmer may not notice this meaningless and erroneous xsl
    instruction.



    =====
    Cheers,

    Dimitre Novatchev.
    http://fxsl.sourceforge.net/ -- the home of FXSL
    Dimitre Novatchev, Oct 9, 2003
    #5
  6. Kasp

    Kasp Guest

    Thanks Martin for your time and explanation - which was very well explained.
    Thanks to Dimtre too.

    I understand that the <xsl:apply-templates select="HierarchyMember"/>
    in <xsl:template match="HierarchyMember"> is redundant.

    Thanks.
    --
    Kasp, Oct 10, 2003
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Kevin Flood
    Replies:
    0
    Views:
    1,009
    Kevin Flood
    Sep 8, 2004
  2. Kevin Flood
    Replies:
    1
    Views:
    2,712
    Kevin Flood
    Sep 13, 2004
  3. Klaus Friese
    Replies:
    0
    Views:
    459
    Klaus Friese
    Nov 22, 2004
  4. Replies:
    1
    Views:
    3,583
    A. Bolmarcich
    May 27, 2005
  5. =?iso-8859-1?q?Jean-Fran=E7ois_Michaud?=

    Help with XSL/XSL:FO for producing revision indicator bar in document.

    =?iso-8859-1?q?Jean-Fran=E7ois_Michaud?=, Apr 28, 2006, in forum: XML
    Replies:
    6
    Views:
    563
    =?iso-8859-1?q?Jean-Fran=E7ois_Michaud?=
    May 3, 2006
Loading...

Share This Page