views:

174

answers:

2

How it is possible to reduce this record?

<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: 

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's a tolower function in XSLT (at least not in 1.0)

Chris Lercher
@chris_l: Just to confirm - no, there isn't.
Tomalak
@chris_l, Perfectly! Works, thanks.
Kalinin
+1  A: 

If the elements to be created are not known in advance and only a few known elements needs to be processed in another, more specific way, here's a 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 $vLower are defined as:

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

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

There must be templates matching the few known elements that should not be processed in the above way. These more specific templates will override the more general template above. For example:

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

Also, the generic template above, matching elements should be overriding the "identity rule" (template).

Dimitre Novatchev
@Dimitre Novatchev, Well, I will know. The only thing that to me is not clear in your concrete code is what for there variables are used. The set of letters remains to constants.
Kalinin
@kalininew: The two xsl:variables must be globally defined -- then they can be used anywhere in the code (more than once). This will save a lot of keystrokes and makes the code a lot more readable.
Dimitre Novatchev
How about using `<xsl:apply-templates select="node()|@*"/>` instead, so that attributes will not be stripped? If the source is HTML, as it would appear to be, there may be attributes worth keeping on `ul`, `ol`, and `li` elements, in particular.
markusk
@markusk: Thanks, I reflected your comment in my answer.
Dimitre Novatchev
@Dimitre Novatchev, I have added your new line `<xsl:apply-templates select="node()|@*"/>` in a code. On an exit I have received: if to set at an element a class at transformation it passes not in attribute of an element, but in text knot. For example `<STRONG CLASS="aaa">lorem ipsum</STRONG>` passes in `<strong>aaa lorem ipsum</strong>`. How it can be corrected?
Kalinin
@kalininew, you need to have the "identity rule" template, preferrably as the first one in your stylesheet.
Dimitre Novatchev
@Dimitre Novatchev, I have read about "identity rule". I used it in our project. It has very much reduced a code. Thanks big.
Kalinin