Attribute Handling

Subject: Attribute Handling
From: Christian Lindig <lindig@xxxxxxxxxxxxxxx>
Date: Thu, 8 Oct 1998 16:49:37 +0200
I just have started to explore XML and thus my question may be a FAQ:

(1) How do I write a template that matches optional attributes such
    that I have access to the values of the attributes? Let's look at the
    HTML IMG element:

        <!ELEMENT IMG - O EMPTY                -- Embedded image -->
        <!ATTLIST IMG
          %attrs;                              -- %coreattrs, %i18n, %events --
          src         %URI;          #REQUIRED -- URI of image to embed --
          alt         %Text;         #REQUIRED -- short description --
          longdesc    %URI;          #IMPLIED  -- link to long description
                                                  (complements alt) --
          height      %Length;       #IMPLIED  -- override height --
          width       %Length;       #IMPLIED  -- override width --
          usemap      %URI;          #IMPLIED  -- use client-side image map --
          ismap       (ismap)        #IMPLIED  -- use server-side image map --
          >

   The following template will only match elements where certain
   attributes are present. Am I forced to write a template for every
   possible combination of attributes if I want to make use of the
   present attributes?

        <xsl:template match="img[attribute(src),
                                 attribute(alt),
                                 attribute(width),
                                 attribute(height),
                                 attribute(border)]">
          <img src="{attribute(src)}"
               alt="{attribute(alt)}"
               width="{attribute(width)}"
               height="{attribute(height)}"
               border="{attribute(border)}"/>
        </xsl:template>
        
        <xsl:template match="img[attribute(src),
                                 attribute(width),
                                 attribute(height)]"/>
          <img src="{attribute(src)}"
               width="{attribute(width)}"
               height="{attribute(height)}"/>
        </xsl:template>
        

Related to the problem above I would like to make a suggestion.  

(2) The template above just passes the element together with all its
    attributes matched. It would be convenient to have something like the
    built-in template rule available for this:

        <!-- standard rule -->
        
        <xsl:template match="*">
          <xsl:process-children/>
        </xsl:template>
        
        <!-- suggested rule -->
        
        <xsl:template match="*">
          <xsl:insert-match-and-process-children/>
        </xsl:template>

   <xsl:insert-match-and-process-children/> does what
   <xsl:process-children/> does, but inserts the result of
   <xsl:process-children/> into an element that was just matched
   including all its attributes (instead of forgetting what was just
   matched like the standard rule does). This would allow to write a
   single rule for all elements that are just passed through XML and
   concentrate on the elements that need non trivial processing. This way
   XML could be used to implement new elements for existing DTDs by
   translating the new elements to the existing ones. Only rules for the
   new elements would be needed and all other ones would be matched by
   the suggested rule.

-- Christian

------------------------------------------------------------------------------
Christian Lindig                  http://www.cs.tu-bs.de/softech/people/lindig 
                                  mail:  lindig@xxxxxxxxxxxxxxx  



 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list


Current Thread