xslt string replace concrete example

限于喜欢 提交于 2019-12-08 13:41:31

问题


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.

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>

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.

Here are two other examples:

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

so that it matches my problem?

Thanks in advance

Kate


回答1:


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.


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> 

The output is the same for both templates.



来源:https://stackoverflow.com/questions/45500871/xslt-string-replace-concrete-example

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!