subclassing in XSLT

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Andy Fish

    subclassing in XSLT

    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




  • Martin Honnen

    #2
    Re: subclassing in XSLT



    Andy Fish wrote:

    [color=blue]
    > 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.[/color]

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


    --

    Martin Honnen

    Comment

    • Patrick TJ McPhee

      #3
      Re: subclassing in XSLT

      In article <dHY%c.3626$1O1 .36431928@news-text.cableinet. net>,
      Andy Fish <ajfish@blueyon der.co.uk> 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:styleshe et version='1.0'
      xmlns:xsl='http ://www.w3.org/1999/XSL/Transform'>

      <xsl:output method='text'/>

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


      <!-- stylesheet B.xsl -->
      <xsl:styleshe et 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>Speci alised 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
      ptjm@interlog.c om

      Comment

      • Andy Fish

        #4
        Re: subclassing in XSLT

        >[color=blue]
        > xsl:import does this implicitly.
        >[/color]

        D'oh!

        Thanks guys - I hadn't noticed that one before.

        Comment

        • Dimitre Novatchev [MVP]

          #5
          Re: subclassing in XSLT

          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:



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

          Cheers,

          Dimitre Novatchev.



          "Patrick TJ McPhee" <ptjm@interlog. com> wrote in message
          news:2qcg8oFu09 lbU1@uni-berlin.de...[color=blue]
          > In article <dHY%c.3626$1O1 .36431928@news-text.cableinet. net>,
          > Andy Fish <ajfish@blueyon der.co.uk> 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:styleshe et version='1.0'
          > xmlns:xsl='http ://www.w3.org/1999/XSL/Transform'>
          >
          > <xsl:output method='text'/>
          >
          > <xsl:template match='A'>
          > <xsl:text>stand ard processing of A.</xsl:text>
          > </xsl:template>
          > </xsl:stylesheet>
          >
          >
          > <!-- stylesheet B.xsl -->
          > <xsl:styleshe et 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>Speci alised 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
          > ptjm@interlog.c om[/color]


          Comment

          • David Carlisle

            #6
            Re: subclassing in XSLT

            [color=blue]
            > I guess the latter remains true in XSLT 2.0, too.[/color]

            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

            Comment

            Working...