xslt 字符串替换具体例子 [英] xslt string replace concrete example

查看:30
本文介绍了xslt 字符串替换具体例子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现 Codesling 提供的模板可以替代 XSLT 1.0 中的替换功能:XSLT 字符串替换

I've found the template Codesling has provided as a substitute to the replace-function in XSLT 1.0: XSLT string replace

我遇到的问题是我无法弄清楚如何将它调整到我的具体示例中,所以我希望在这里提供一些指示.

The problem I'm having is that I cannot figure out how to adapt it to my concrete example, so I'm hoping for some pointers here.

我有一个名为 ./bib-info 的 xml 参数,它看起来像这样(例如):

I've got a xml-parameter called ./bib-info that looks like this (for example):

<bib-info>Gäbler, René, 1971-  [(DE-588)138691134]:                           Schnell-Umstieg auf Office 2010, [2010]</bib-info>

我的目标是替换]:"和以下文本开头(在本例中为Schnell")之间的空格.顺便说一句,空格的数量总是相同的.

My goal is to replace the blanks between the "]:" and the beginning of the following text (in this case "Schnell"). The number of blanks is always the same btw.

这里有另外两个例子:

<bib-info>Decker, Karl-Heinz, 1948-2010  [(DE-588)141218622]:                           Funktion, Gestaltung und Berechnung, 1963</bib-info>

<bib-info>Decker, Karl-Heinz, 1948-2010  [(DE-588)141218622]:                           Maschinenelemente, 1963</bib-info>

谁能给我一个提示如何编写调用代码

Could anybody please give me a hint how to write the calling code

<xsl:variable name="newtext">
    <xsl:call-template name="string-replace-all">
        <xsl:with-param name="text" select="$text" />
        <xsl:with-param name="replace" select="a" />
        <xsl:with-param name="by" select="b" />
    </xsl:call-template>
</xsl:variable>

所以它符合我的问题?

提前致谢

凯特

推荐答案

我的目标是替换]:"和以下文本开头之间的空格

My goal is to replace the blanks between the "]:" and the beginning of the following text

执行此操作的示例如下:

An example doing this is the following:

<xsl:template match="bib-info">
  <xsl:variable name="newtext">
    <xsl:call-template name="string-replace-all">
      <xsl:with-param name="text" select="text()" />
      <xsl:with-param name="replace" select="']:                           '" />
      <xsl:with-param name="by" select="']: '" />
    </xsl:call-template>
  </xsl:variable>
  <bib-info><xsl:value-of select="$newtext" /></bib-info>
</xsl:template>

请注意,这只会替换真正的空格,而不是制表符或其他特殊字符.

Note that this only replaces real spaces and not tabs or other special characters.

顺便说一句,在这种情况下替换字符串可能不是最佳选择.您可以使用 XSLT-1.0 的 substring 函数实现相同的效果:

BTW replacing strings may not be the best choice in this case. You can achieve the same with the substring functions of XSLT-1.0:

<xsl:template match="bib-info">
  <xsl:variable name="newtext1" select="substring-before(text(),']:                           ')" />
  <xsl:variable name="newtext2" select="substring-after(text(),']:                           ')" />
  <xsl:variable name="newtext" select="concat($newtext1,']: ',$newtext2)" />
  <bib-info><xsl:value-of select="$newtext" /></bib-info>
</xsl:template> 

两个模板的输出相同.

这篇关于xslt 字符串替换具体例子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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