for a set of tags How can this record be reduced ?

Optimizing <xsl: apply-templates / "> for a set of tags

How can this record be reduced ?

<xsl:template match="BR">
    <br/>
</xsl:template>

<xsl:template match="B">
    <strong><xsl:apply-templates /></strong>
</xsl:template>

<xsl:template match="STRONG">
    <strong><xsl:apply-templates /></strong>
</xsl:template>

<xsl:template match="I">
    <em><xsl:apply-templates /></em>
</xsl:template>

<xsl:template match="EM">
    <em><xsl:apply-templates /></em>
</xsl:template>

<xsl:template match="OL">
    <ol><xsl:apply-templates /></ol>
</xsl:template>

<xsl:template match="UL">
    <ul><xsl:apply-templates /></ul>
</xsl:template>

<xsl:template match="LI">
    <li><xsl:apply-templates /></li>
</xsl:template>

<xsl:template match="SUB">
    <sub><xsl:apply-templates /></sub>
</xsl:template>

<xsl:template match="SUP">
    <sup><xsl:apply-templates /></sup>
</xsl:template>

<xsl:template match="NOBR">
    <nobr><xsl:apply-templates /></nobr>
</xsl:template>

      

+2


a source to share


2 answers


If the elements to be created are not known in advance, and only a few known elements need to be handled in a different, more specific, more dynamic solution :

 <xsl:template match="*">
  <xsl:element name="{translate(name(), $vUpper, $vLower)}">
    <xsl:apply-templates select="node()|@*"/>
  </xsl:element>
 </xsl:template>

      

where $vUpper

and are $vLower

defined as:

<xsl:variable name="vUpper" select=
 "'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
 "/>

<xsl:variable name="vLower" select=
 "'abcdefghijklmnopqrstuvwxyz'
 "/>

      



There should be templates corresponding to several known elements that should not be processed in the above way. These more specific templates will override the more general template above. For instance:

 <xsl:template match="specificName">
   <!-- Specific processing here -->
 </xsl:template>

      

In addition to the general pattern above, matching elements must override the "identity rule" (pattern).

+1


a source


Maybe something like:

<xsl:template match="LI|SUB|...">
   <xsl:element name="{translate(name(),
          'ABCDEFGHIJKLMNOPQRSTUVWXYZ','abcdefghijklmnopqrstuvwxyz')}">
    <xsl:apply-templates/>
   </xsl:element>
</xsl:template>

      



I don't think there is a tolower function in XSLT (at least not in 1.0)

+2


a source







All Articles