Re: [xsl] manipulating text and not losing elements

Subject: Re: [xsl] manipulating text and not losing elements
From: "James A. Robinson" <jim.robinson@xxxxxxxxxxxx>
Date: Thu, 13 Oct 2005 08:17:41 -0700
> But what happens in the scenario where I want to remove the numbers
> right after the indent1 and paragraph tags, as far as I can see the
> only way to accomplish this is with substring, but using a substring
> requires outputting using <xsl:value-of> which negates all following
> children.  Ultimately I would like the line tagged as <indent1> to
> read the following:

What you need to remember is that text is a node type, like any other.
So you can match on text(), and furthermore you can qualify the match
to strings with numbers in front of them, etc.  The example below
shows a crude example of matching on text() and removing the first few
chars.  There are other ways of modifying the string, but I'm not sure
what your text model is, and this reflects your original solution.  :)

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"; version="2.0">
  
  <xsl:template match="/">
      <xsl:apply-templates/>
  </xsl:template>
  
  <xsl:template match="@*|node()">
    <xsl:copy>
      <xsl:apply-templates/>
    </xsl:copy>
  </xsl:template>
  
  <xsl:template match="indent1">
    <p class="indent1">
      <xsl:apply-templates/>
    </p>
  </xsl:template>
  
  <xsl:template match="italic">
    <em>
      <xsl:apply-templates/>
    </em>
  </xsl:template>
  
  <xsl:template match="bold">
   <strong>
     <xsl:apply-templates/>
   </strong>
  </xsl:template>
  
  <xsl:template match="text()[matches(., '\d+ .+')]">
    <xsl:value-of select="substring(., 6)"></xsl:value-of>
  </xsl:template>
  
</xsl:stylesheet>

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
James A. Robinson                       jim.robinson@xxxxxxxxxxxx
Stanford University HighWire Press      http://highwire.stanford.edu/
+1 650 7237294 (Work)                   +1 650 7259335 (Fax)

Current Thread