This is the mail archive of the docbook-apps@lists.oasis-open.org mailing list .


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Stylesheet for remapping/renaming elements


Nik Clayton mentioned a way to remap DocBook elements to other element
names -- that is, to take something like this:

  <helpproject status="draft" remap="article">
       ...
    <topic revisionflag="changed" remap="section">
        ...
    </topic>
  </helpproject>

and turn it into this:

  <article status="draft">
       ...
    <section revisionflag="changed">
        ...
    </section>
  </article>
      
Here's a simple stylesheet that I think will do it.

  <?xml version='1.0'?>
  <xsl:transform 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"; version="1.0">

  <!-- ********************************************************************
       Use the "attributename" parameter to specify the attribute
       that holds the names of the elements you want to rename to.    
                                                                            -->

    <xsl:param name="attributename">remap</xsl:param>


  <!-- ********************************************************************
       Look for any elements with an attribute named $attributename and
       replace the name of the element with the value of that attribute
                                                                            -->

    <xsl:template match="node()|@*">
      <xsl:choose>

        <xsl:when test="@*[local-name()=$attributename]">
          <xsl:element name="{@*[local-name()=$attributename]}">
            <xsl:apply-templates
                 select="@*[local-name()!=$attributename]|node()"/>
          </xsl:element>
        </xsl:when>

        <xsl:otherwise>
          <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
          </xsl:copy>
        </xsl:otherwise>

      </xsl:choose>
    </xsl:template>
  </xsl:transform>


To make the best use of it, you'd also want to do some steps to add
support for it in your DTD customization layer:

 1. Decide on an attribute to use throughout the customization layer
    for storing the element names you want to convert/rename to.
    Off the shelf, the stylesheet defaults to looking for "remap".

 2. Add the attribute name to the ATTLIST for each new element in your
    customization layer, as an NMTOKEN with a #FIXED value.

    If you're creating a DocBook customization layer, you'd end up
    with an ATTLIST that looked something like this:

    <!ATTLIST  helpproject
                       remap    NMTOKEN      #FIXED     "article"
                       appid    CDATA        #IMPLIED
                       %label.attrib;
                       %status.attrib;
                       %id.attrib;
                       %lang.attrib;
                       %xreflabel.attrib;
                       %revisionflag.attrib;
                       %effectivity.attrib;
                       %local.common.attrib;
                       %helpset.role.attrib;
                       %local.helpset.attrib;
    >

If you do that, you don't have to include "remap" attributes in your
document instances. But your XSLT engine will need access to your
customization layer DTD; you won't be able to correctly process a
document if it doesn't include a DOCTYPE declaration that references
your customization-layer DTD where the #FIXED attributes are declared.

This might not seem like a proper use of "remap", but I think Eve
Maler and Jeanne El Andaloussi's "Developing SGML DTDs: From Text to
Model to Markup" provides some support for using it that way.

They discuss a "remap" attribute in that book (Conversion Markup
section (8.4.2) of the Markup Model Design and Implementation chapter)
and outline a couple different things it might be used for, including
"transforming SGML documents to conform to a different DTD (or to the
same DTD but with different or augmented contents)."




----------------------------------------------------------------
To subscribe or unsubscribe from this elist use the subscription
manager: <http://lists.oasis-open.org/ob/adm.pl>


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]