2010-11-02 104 views
0

我正在編碼一個網站,該網站的語言字符串以xml文件分隔。根據不同的語言,我包括一個或另一個。這沒有問題。使用XML和XSLT格式化字符串,如PHP中的sptrinf()

順便說一下,這個網站是一個動態的,所以會有例如像字符串你好ipalaus!英文,如Bienvenido ipalaus!等等。在這種情況下,我們有詞的順序相同,但在某些情況下可能會名字符串字符串名稱字符串名稱字符串 ...

有無論如何回答什麼sprintf()確實在PHP?在PHP中,我們有這樣的:

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

echo sprintf($string, $name); 

// OUTPUTS: Welcome ipalaus 
?> 

我想在我的index.en.xml一些像:

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

在我INDEX.XML,與PHP產生的一個,將有:

<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

更多鈔票複製http://stackoverflow.com/questions/3986408/how-to-fill-text- templates-using-xslt – 2010-11-02 14:24:57

+0

我完全不理解它。它發佈了一個示例,但我正在尋找一種將其解析爲大規模的方法,因此我不必爲每個變量重複一次,因爲在大型項目中可能會非常困難。請注意,必須混合使用不同的XML文件。無論如何? – ipalaus 2010-11-02 14:48:51

+0

@Alejandro,你沒有看到我能做的事嗎?提前致謝! – ipalaus 2010-11-02 17:46:08

回答

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> 
+0

謝謝亞歷杭德羅!爲了更復雜一點,將會在索引中使用某種方式,例如** EDIT **中的答案?要訪問一個孩子?對不起,我的併發症,並再次感謝你有用的幫助和時間! – ipalaus 2010-11-02 19:05:46

+1

@Isern帕勞斯:你很好!檢查我的編輯。 – 2010-11-02 20:05:04