Home > Mobile >  How make two transformations for an element XSL
How make two transformations for an element XSL

Time:12-21

I am learning XSL and I would like for an element to do 2 transformations if it's possible.

Here is the element :

<xsl:template match="//ns:text[@type='TEXT']">
    <xsl:call-template name="break"/>
  </xsl:template>

Here is the 1st transformation and the piece of code :

<xsl:template name="break">

    <xsl:param name="text" select="string(.)"/>
    <xsl:choose>
      <xsl:when test="contains($text, '&#xa;')">
        <xsl:if test="substring-before($text, '&#xa;') != ''">
          <p>
            <xsl:value-of select="substring-before($text, '&#xa;')"/>
          </p>
        </xsl:if>
        <xsl:call-template name="break">
          <xsl:with-param name="text" select="substring-after($text, '&#xa;')"/>
        </xsl:call-template>
      </xsl:when>
      <xsl:otherwise>
        <p>
          <xsl:value-of select="$text"/>
        </p>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

And for the 2nd transformation, I want to replace some character with translate() method.

But, please, how can I do for to leave the first transformation with the break call-template and at the same time put the translate ?

Thanks in advance for your help,

Best Regards.

CodePudding user response:

I believe the most efficient point to perform the translation would be when calling the tokenizing template - so that the call would look something like:

<xsl:template match="ns:text[@type='TEXT']">
    <xsl:call-template name="break">
        <xsl:with-param name="text" select="translate(., 'abc', 'xyz')"/>
    </xsl:call-template>
</xsl:template>

Then, in the called template, you would want to change:

<xsl:param name="text" select="string(.)"/>

to:

<xsl:param name="text"/>

although it should work just as well if you don't.

Alternatively, you could modify the tokenizing template so that it writes to the output in one place only (see an example here). Then perform the translation at this point.


Untested, because no example to test with was provided.

  • Related