0
votes

I have XML files with text and I want to format certain words and characters. These could be within several elements, such as l, add and others. So I want to look up for characters like letter e or symbols and change its format. I want to exclude from my search text within abbr element. Thanks.

EDIT: I'm using XSLT 1.0 and I can't upgrade it.

Take this text as an example:

<l n="1" type="latin">Lorem ipsum dolor sit amet, <add type="con" xml:id="add2">consectetur</add> adipiscing elit, <abbr>se</abbr>d do eiusmod tempor incididunt ut labore et dolore magna aliqua</l>

Desired output:

Lorem ipsum dolor sit amet,consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua

XML Header

<?xml version="1.0" encoding ="UTF-8" standalone ="no" ?>

XSLT Header

<?xml version="1.0" encoding="UTF-8"?>

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xmlns:tei="http://www.tei-c.org/ns/1.0"
    exclude-result-prefixes="xs tei"
    version="1.0">
    <xsl:output method="html" encoding="UTF-8" indent="yes"/>
1
You might want to check whether you can't use an XSLT 2 or 3 processor like Saxon (there is a Java version, a .NET version, a C/C++ version with PHP and Python bindings and there is Saxon JS 2 for browsers and for Node.js) or Altova or XmlPrime to make use of xsl:analyze-string.Martin Honnen
In XSLT 1.0 you need to use a recursive named template to replace a search-string with markup - see an example here: stackoverflow.com/a/46007915/3016153.michael.hor257k
thank you! i'll try itmindolianum

1 Answers

0
votes

Here is an example using XSLT 3 and the analyze-string function:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  version="3.0"
  xmlns:xs="http://www.w3.org/2001/XMLSchema"
  exclude-result-prefixes="#all"
  expand-text="yes">

  <xsl:output method="html" indent="yes" html-version="5"/>

  <xsl:mode on-no-match="shallow-copy"/>

  <xsl:template match="/" name="xsl:initial-template">
    <html>
      <head>
        <title>Example</title>
      </head>
      <body>
        <xsl:apply-templates/>
      </body>
    </html>
  </xsl:template>
  
  <xsl:template match="*[not(self::abbr)]/text()">
    <xsl:apply-templates select="analyze-string(., 'e+')" mode="wrap"/>
  </xsl:template>
  
  <xsl:template match="*:match" mode="wrap">
    <b>
      <xsl:value-of select="."/>
    </b>
  </xsl:template>

</xsl:stylesheet>