【问题标题】:Formatting a string with XML and XSLT like sptrinf() in PHP使用 XML 和 XSLT 格式化字符串,如 PHP 中的 sptrinf()
【发布时间】:2010-11-03 04:04:43
【问题描述】:

我正在编写一个将语言字符串分隔在 xml 文件中的网站。根据语言,我包括一种或另一种。没有问题。

顺便说一下,这个网站是动态的,所以会有一个字符串,例如英文的 Hello ipalaus!,如 Bienvenido ipalaus! 等等。在这种情况下,我们有相同的单词顺序,但在某些情况下可能会是 name stringstring namestring name string...

有没有办法回复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 文件中,代表 Welcome ipalaus

实际上,在我的 XSLT 文件中,我使用它来加载语言:

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

使用以下命令访问值:&lt;xsl:value-of select="$base/language/welcome" /&gt;

提前谢谢你!

编辑:向亚历杭德罗提出的关于他完全有效的回答的问题示例:

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

【问题讨论】:

  • 我完全不明白。它发布了一个示例,但我正在寻找一种将其解析为大规模的方法,因此我不必为每个变量重复,因为在大规模项目中可能非常困难。请注意,必须与不同的 XML 文件混合使用。有吗?
  • @Alejandro,你没看到我能做些什么吗?提前致谢!

标签: xml string xslt


【解决方案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>

编辑:这个样式表

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

【讨论】:

  • 谢谢亚历杭德罗!稍微复杂一点,是否会在索引中使用某种方法,例如答案中的 EDIT ?访问一个孩子?对不起,我的麻烦,再次感谢您的有用帮助和时间!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-04-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多