String formatting with XML and XSLT, like sptrinf () in PHP

I am coding a website where the language string is split in xml files. Depending on the language, I include one or the other. No problem with that.

By the way, this site is dynamic, so it will have a string like Hello ipalaus! in English, e.g. Bienvenido ipalaus !, etc. In this case, we have the same word order, but in some cases there will probably be a name string, string name, string name string ...

In any case, you can answer what sprintf()does in PHP? In PHP, we have this:

<?php
$name = "ipalaus";
$string = "Welcome %s";

echo sprintf($string, $name);

// OUTPUTS: Welcome ipalaus
?>

I would like to have in my index.en.xml some like:

<language>
    <welcome>Welcome %s</welcome>
</language>

In my index.xml generated with PHP will be:

<index>
    <locale>en</locale>

    <welcome>ipalaus</welcome>
</index>

XSLT ipalaus.

, XSLT :

<xsl:param name="language" select="document(concat('../lang/', $locale, '/index.xml'))" />

: <xsl:value-of select="$base/language/welcome" />.

!

: :

<index>
    <video>
        <author>ipalaus</author>
    </video>
    <video>
        <author>Alejandro</author>
    </video>
</index>

:

<language>
    <video>
        <made>This videos is made by <author/></made>
        <random>Another string</random>
    </video>
</language>
0
1

, index.xml:

<index> 
    <locale>en</locale> 
    <name>ipalaus</name> 
</index> 

index.en.xml:

<language>
    <welcome>Welcome <name/></welcome>
</language>

:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:param name="pLayoutURI" select="'index.en.xml'"/>
    <xsl:variable name="vData" select="/index"/>
    <xsl:variable name="vLayout" select="document($pLayoutURI,/)/language"/>
    <xsl:template match="/">
        <html>
            <h1><xsl:apply-templates select="$vLayout/welcome"/></h1>
        </html>
    </xsl:template>
    <xsl:template match="language/*/*[not(node())]">
        <xsl:value-of select="$vData/*[name()=name(current())]"/>
    </xsl:template>
</xsl:stylesheet>

:

<html>
    <h1>Welcome ipalaus</h1>
</html>

EDIT:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:param name="pLayoutURI" select="'index.en.xml'"/>
    <xsl:variable name="vLayout" select="document($pLayoutURI,/)/language"/>
    <xsl:template match="/index">
        <ul>
            <xsl:apply-templates/>
        </ul>
    </xsl:template>
    <xsl:template match="video">
        <li>
            <video src="{url}">
                <xsl:attribute name="title">
                    <xsl:apply-templates select="$vLayout/video/made/node()"
                                         mode="populate">
                        <xsl:with-param name="pContext" select="."/>
                    </xsl:apply-templates>
                </xsl:attribute>
            </video>
        </li>
    </xsl:template>
    <xsl:template match="language//*[not(node())]" mode="populate">
        <xsl:param name="pContext" select="/.."/>
        <xsl:value-of select="$pContext/*[name()=name(current())]"/>
    </xsl:template>
</xsl:stylesheet>

:

<index>
    <video>
        <author>ipalaus</author>
        <url>ipalaus.mpg</url>
    </video>
    <video>
        <author>Alejandro</author>
        <url>Alejandro.mpg</url>
    </video>
</index>

index.en.xml:

<language>
    <video>
        <made>This videos is made by <author/></made>
        <random>Another string</random>
    </video>
</language>

:

<ul>
   <li>
      <video src="ipalaus.mpg" title="This videos is made by ipalaus"/>
   </li>
   <li>
      <video src="Alejandro.mpg" title="This videos is made by Alejandro"/>
   </li>
</ul>
+1

All Articles