是否可以在 XSLT 中使用 sprintf() 函数? [英] Is it possible to use the sprintf() function in XSLT?

查看:51
本文介绍了是否可以在 XSLT 中使用 sprintf() 函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想做类似Hello %s"的事情,并在另一个变量中添加World".

I'd like to do something like "Hello %s" and have "World" in another variable.

当然,我可以使用字符串替换来完成这个简单的案例,但如果可能的话,我想要所有 sprintf() 功能,例如参数重新排序,这对我自己来说会非常复杂.

Of course I could do this simple case using string replacement but if possible I'd like all sprintf() features like argument reordering, which would be very complex to do myself.

推荐答案

Hello World 的 XML/XSLT 等价物是这样的:

我们有这个 XML 文档:

We have this XML document:

<t>Hello <rep/>!</t>

并在其上使用此转换:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>

 <xsl:template match="node()|@*">
     <xsl:copy>
       <xsl:apply-templates select="node()|@*"/>
     </xsl:copy>
 </xsl:template>

 <xsl:template match="rep">
  <xsl:value-of select="'World'"/>
 </xsl:template>
</xsl:stylesheet>

想要的结果产生:

<t>Hello World!</t>

请注意:

  1. 我们使用并覆盖身份规则.这是最基本的 XSLT 设计模式.

  1. We use and override the identity rule. This is the most fundamental XSLT design pattern.

我们可以用许多不同的替换元素散布一个恒定的内容,从而有一种填空"技术.

We can intersperse a constant content with many different replacement elements and thus have a "fill-in the blanks" technique.

使用这种技术可以实现内容和处理逻辑之间的完全分离.XSLT 代码不知道它正在处理什么源 XML 文档(文档 URL 甚至可以作为参数传递).相同的 XSLT 样式表可以用于任何填空文档,任何填空文档都可以由不同的 XSLT 样式表处理.这种分离实现了最大的灵活性和可维护性.

Using this technique a full separation between content and processing logic can be achieved. The XSLT code doesn't know what source XML document it is processing (the document URL can even be passed as parameter). The same XSLT stylesheet can be used any fill-in the blanks document and any fill-in the blanks document can be processed by different XSLT stylesheets. This separation achieves maximum flexibility and maintainability.

如果您只想只生成文本,这也可以轻松完成 -- 例如使用 <xsl:output method="text"/>

If you only want just text to be produced, this can also be done easily -- for example using <xsl:output method="text"/>

这篇关于是否可以在 XSLT 中使用 sprintf() 函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆