0

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
    **1.** You want to replace the blanks with what? -- **2.** For a concrete example, we need to se where `` is relative to your current context. So please show us a [mcve]. – michael.hor257k Aug 04 '17 at 08:54
  • You're so right of course! I want to replace the multitude of blanks with a single one so that the example `code` Decker, Karl-Heinz, 1948-2010 [(DE-588)141218622]: Funktion, Gestaltung und Berechnung, 1963 `code` – Katharina Wolkwitz Aug 07 '17 at 05:54
  • Sorry - wasn't fast enough editing my comment... :-( I want to replace the multitude of blanks with a single one so that the ugly gap after the "]:" is closed. Excuse me for forgetting to mentioning that. – Katharina Wolkwitz Aug 07 '17 at 06:02
  • 1
    Could you not simply apply `normalize-space()` to the `bib-info` elements? Note that this would reduce *any* group of whitespace characters to a single space - in your example, it would change `1971- [` to `1971- [`. – michael.hor257k Aug 07 '17 at 06:03
  • I'll have a look at the normalize-space() function - perhaps it'll solve my problem. Thanks for the tip! :-) – Katharina Wolkwitz Aug 07 '17 at 06:07
  • I've had a look at the normalize-space()-function and found that it does too much for my liking. I need the line-breaks to stay in the -construct so I cannot use this function. :-( – Katharina Wolkwitz Aug 07 '17 at 08:14
  • I don't see any line breaks in the examples you have provided. – michael.hor257k Aug 07 '17 at 08:30
  • You're right again, of course. But since the parameter is supposed to contain more or less formatted bibliographical information from a library catalog, I cannot rule out for certain that there won't be intended line breaks inside the -parameter. So using the normalize-space()-function would be nice to get rid of the excess blanks but would also kill off possible intended line-breaks in the process which would be bad. – Katharina Wolkwitz Aug 07 '17 at 09:10
  • Okay, so use a replace template like you started to (this is assuming the number of spaces in the block you want to remove is constant). -- P.S. I think you mean an *element*, not a *parameter*. – michael.hor257k Aug 07 '17 at 09:21

1 Answers1

0

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.

zx485
  • 28,498
  • 28
  • 50
  • 59