subclassing in XSLT

Discussion in 'XML' started by Andy Fish, Sep 9, 2004.

  1. Andy Fish

    Andy Fish Guest

    Hi,

    I'm trying to achieve something like subclassing in XSLT, by which I mean
    that I have stylesheet A and I want to make a more specialised "subclass"
    called stylesheet B. which inherits most of the functions but overrides some
    templates.

    the closest I can think of getting is to write template B including template
    A, and then put templates in B with the same "match" clause but with a
    higher priority than A. Unfortunately, a key feature would be to invoke the
    superclass method as part of the overridden method, and I can't see any way
    to do this.

    TIA for Any ideas

    Andy
    Andy Fish, Sep 9, 2004
    #1
    1. Advertising

  2. Andy Fish wrote:


    > the closest I can think of getting is to write template B including template
    > A, and then put templates in B with the same "match" clause but with a
    > higher priority than A. Unfortunately, a key feature would be to invoke the
    > superclass method as part of the overridden method, and I can't see any way
    > to do this.


    Doesn't
    <xsl:apply-imports />
    do what you want?


    --

    Martin Honnen
    http://JavaScript.FAQTs.com/
    Martin Honnen, Sep 9, 2004
    #2
    1. Advertising

  3. In article <dHY%c.3626$>,
    Andy Fish <> wrote:

    % the closest I can think of getting is to write template B including template
    % A, and then put templates in B with the same "match" clause but with a
    % higher priority than A. Unfortunately, a key feature would be to invoke the

    xsl:import does this implicitly.


    % superclass method as part of the overridden method, and I can't see any way
    % to do this.

    Once you've used xsl:import, you can invoke the template from the imported
    stylesheet using xsl:apply-imports.

    <!-- stylesheet A.xsl -->
    <xsl:stylesheet version='1.0'
    xmlns:xsl='http://www.w3.org/1999/XSL/Transform'>

    <xsl:eek:utput method='text'/>

    <xsl:template match='A'>
    <xsl:text>standard processing of A.</xsl:text>
    </xsl:template>
    </xsl:stylesheet>


    <!-- stylesheet B.xsl -->
    <xsl:stylesheet version='1.0'
    xmlns:xsl='http://www.w3.org/1999/XSL/Transform'>

    <!-- this must come first! -->
    <xsl:import href='A.xsl'/>

    <xsl:template match='A'>
    <xsl:text>Specialised processing of A, followed by </xsl:text>
    <xsl:apply-imports/>
    </xsl:template>
    </xsl:stylesheet>

    <!-- A.xml -->
    <A/>

    of course in the real world I would use names like x.xsl and y.xsl.
    Anyway, if you process A.xml using A.xsl, you get this output

    standard processing of A.

    while if you process A.xml using B.xsl, you get this

    Specialised processing of A, followed by standard processing of A.


    --

    Patrick TJ McPhee
    East York Canada
    Patrick TJ McPhee, Sep 10, 2004
    #3
  4. Andy Fish

    Andy Fish Guest

    >
    > xsl:import does this implicitly.
    >


    D'oh!

    Thanks guys - I hadn't noticed that one before.
    Andy Fish, Sep 10, 2004
    #4
  5. Just a note that in XSLT 1.0 xsl:apply-imports cannot be passed any
    parameters.

    There are also some nasty surprises with import precedence -- e.g. see what
    Jeni Tennison had to say at:

    http://www.biglist.com/lists/xsl-list/archives/200102/msg01135.html

    I guess the latter remains true in XSLT 2.0, too.

    Cheers,

    Dimitre Novatchev.



    "Patrick TJ McPhee" <> wrote in message
    news:...
    > In article <dHY%c.3626$>,
    > Andy Fish <> wrote:
    >
    > % the closest I can think of getting is to write template B including
    > template
    > % A, and then put templates in B with the same "match" clause but with a
    > % higher priority than A. Unfortunately, a key feature would be to invoke
    > the
    >
    > xsl:import does this implicitly.
    >
    >
    > % superclass method as part of the overridden method, and I can't see any
    > way
    > % to do this.
    >
    > Once you've used xsl:import, you can invoke the template from the imported
    > stylesheet using xsl:apply-imports.
    >
    > <!-- stylesheet A.xsl -->
    > <xsl:stylesheet version='1.0'
    > xmlns:xsl='http://www.w3.org/1999/XSL/Transform'>
    >
    > <xsl:eek:utput method='text'/>
    >
    > <xsl:template match='A'>
    > <xsl:text>standard processing of A.</xsl:text>
    > </xsl:template>
    > </xsl:stylesheet>
    >
    >
    > <!-- stylesheet B.xsl -->
    > <xsl:stylesheet version='1.0'
    > xmlns:xsl='http://www.w3.org/1999/XSL/Transform'>
    >
    > <!-- this must come first! -->
    > <xsl:import href='A.xsl'/>
    >
    > <xsl:template match='A'>
    > <xsl:text>Specialised processing of A, followed by </xsl:text>
    > <xsl:apply-imports/>
    > </xsl:template>
    > </xsl:stylesheet>
    >
    > <!-- A.xml -->
    > <A/>
    >
    > of course in the real world I would use names like x.xsl and y.xsl.
    > Anyway, if you process A.xml using A.xsl, you get this output
    >
    > standard processing of A.
    >
    > while if you process A.xml using B.xsl, you get this
    >
    > Specialised processing of A, followed by standard processing of A.
    >
    >
    > --
    >
    > Patrick TJ McPhee
    > East York Canada
    >
    Dimitre Novatchev [MVP], Sep 10, 2004
    #5

  6. > I guess the latter remains true in XSLT 2.0, too.


    yes although in 2 you can use next-match rather than apply-imports (with
    or without using xsl:import) this has rather different behaviour on edge
    cases.

    David
    David Carlisle, Sep 10, 2004
    #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. Stylus Studio
    Replies:
    0
    Views:
    640
    Stylus Studio
    Aug 3, 2004
  2. Benjamin Hillsley
    Replies:
    3
    Views:
    1,662
    Dimitre Novatchev
    Sep 25, 2003
  3. ted
    Replies:
    1
    Views:
    615
    Laurens
    Jan 26, 2004
  4. Replies:
    2
    Views:
    708
    Henry S. Thompson
    Oct 19, 2005
  5. Replies:
    4
    Views:
    659
Loading...

Share This Page