Loading ...
Sorry, an error occurred while loading the content.
 

manipulating strings

Expand Messages
  • tinacalla
    I have been unable to find an example of removing word spaces at the beginning of a string and converting the interior word spaces in the strings to hyphens.
    Message 1 of 3 , Nov 4, 2003
      I have been unable to find an example of removing word spaces at the
      beginning of a string and converting the interior word spaces in the
      strings to hyphens.

      Here is my xml example ...

      <partnumber>3859</partnumber>
      <partnumber> 3859</partnumber>
      <partnumber> 3859 A1L</partnumber>
      <partnumber> 3859</partnumber>
      <partnumber> 3859 3E4</partnumber>

      Here is my desired output ...
      <partnumber>3859</partnumber>
      <partnumber>3859</partnumber>
      <partnumber>3859-A1L</partnumber>
      <partnumber>3859</partnumber>
      <partnumber>3859--3E4</partnumber>

      I am unable to use normalize-space because there may be more than one
      space in the interior of the partnumber and both of these interior
      spaces need to be converted to hypens. So in other words, I am saying
      that I want to remove all leading spaces (if any), and translate all
      other spaces to hyphens.

      I would greatly appreciate any help.

      Thanks
    • Ryan Graham
      ... Try this: First declare these as global variables at the top of your stylesheet:
      Message 2 of 3 , Nov 4, 2003
        >Here is my xml example ...
        >
        ><partnumber>3859</partnumber>
        ><partnumber> 3859</partnumber>
        ><partnumber> 3859 A1L</partnumber>
        ><partnumber> 3859</partnumber>
        ><partnumber> 3859 3E4</partnumber>
        >
        >Here is my desired output ...
        ><partnumber>3859</partnumber>
        ><partnumber>3859</partnumber>
        ><partnumber>3859-A1L</partnumber>
        ><partnumber>3859</partnumber>
        ><partnumber>3859--3E4</partnumber>
        >
        >I am unable to use normalize-space because there may be more than one
        >space in the interior of the partnumber and both of these interior
        >spaces need to be converted to hypens. So in other words, I am saying
        >that I want to remove all leading spaces (if any), and translate all
        >other spaces to hyphens.

        Try this:

        First declare these as global variables at the top of your stylesheet:

        <xsl:variable name="space"><xsl:text> </xsl:text></xsl:variable>
        <xsl:variable name="hyphen">-</xsl:variable>

        Then when it comes time to process a partnumber node:

        <xsl:template match="partnumber">
        <xsl:call-template name="removeSpace">
        <xsl:with-param name="textNode" select="." />
        </xsl:call-template>
        </xsl:template>

        <xsl:template name="removeSpace">
        <xsl:param name="textNode" />
        <xsl:choose>
        <xsl:when test="starts-with($textNode, $space)">
        <xsl:call-template name="removeSpace">
        <xsl:with-param name="textNode"
        select="substring-after($textNode,$space)" />
        </xsl:call-template>
        </xsl:when>
        <xsl:otherwise>
        <xsl:call-template name="formatString">
        <xsl:with-param name="fixedNode" select="$textNode" />
        </xsl:call-template>
        </xsl:otherwise>
        </xsl:choose>
        </xsl:template>

        <xsl:template name="formatString">
        <xsl:param name="fixedNode" />
        <xsl:variable name="addHyphens"
        select="translate($fixedNode,$space,$hyphen)" />
        <xsl:element name="partNumber">
        <xsl:value-of select="$addHyphens" />
        </xsl:element>
        </xsl:template>

        This tests the first character of the text node, and if it is a space,
        removes it. When it finally finds a character that is not a space, it then
        proceeds to replace the rest of the spaces in the text node with hyphens. I
        used two named templates here for clarity, but you may be able to do it with
        one.

        Hope this helps,
        RG
      • tinacalla
        Thank you for your reply. It works very well. ... one ... saying ... all ... space, ... it then ... hyphens. I ... do it with
        Message 3 of 3 , Nov 5, 2003
          Thank you for your reply. It works very well.

          --- In XSL-FO@yahoogroups.com, Ryan Graham <ryan.graham@a...> wrote:
          >
          > >Here is my xml example ...
          > >
          > ><partnumber>3859</partnumber>
          > ><partnumber> 3859</partnumber>
          > ><partnumber> 3859 A1L</partnumber>
          > ><partnumber> 3859</partnumber>
          > ><partnumber> 3859 3E4</partnumber>
          > >
          > >Here is my desired output ...
          > ><partnumber>3859</partnumber>
          > ><partnumber>3859</partnumber>
          > ><partnumber>3859-A1L</partnumber>
          > ><partnumber>3859</partnumber>
          > ><partnumber>3859--3E4</partnumber>
          > >
          > >I am unable to use normalize-space because there may be more than
          one
          > >space in the interior of the partnumber and both of these interior
          > >spaces need to be converted to hypens. So in other words, I am
          saying
          > >that I want to remove all leading spaces (if any), and translate
          all
          > >other spaces to hyphens.
          >
          > Try this:
          >
          > First declare these as global variables at the top of your
          stylesheet:
          >
          > <xsl:variable name="space"><xsl:text> </xsl:text></xsl:variable>
          > <xsl:variable name="hyphen">-</xsl:variable>
          >
          > Then when it comes time to process a partnumber node:
          >
          > <xsl:template match="partnumber">
          > <xsl:call-template name="removeSpace">
          > <xsl:with-param name="textNode" select="." />
          > </xsl:call-template>
          > </xsl:template>
          >
          > <xsl:template name="removeSpace">
          > <xsl:param name="textNode" />
          > <xsl:choose>
          > <xsl:when test="starts-with($textNode, $space)">
          > <xsl:call-template name="removeSpace">
          > <xsl:with-param name="textNode"
          > select="substring-after($textNode,$space)" />
          > </xsl:call-template>
          > </xsl:when>
          > <xsl:otherwise>
          > <xsl:call-template name="formatString">
          > <xsl:with-param name="fixedNode" select="$textNode" />
          > </xsl:call-template>
          > </xsl:otherwise>
          > </xsl:choose>
          > </xsl:template>
          >
          > <xsl:template name="formatString">
          > <xsl:param name="fixedNode" />
          > <xsl:variable name="addHyphens"
          > select="translate($fixedNode,$space,$hyphen)" />
          > <xsl:element name="partNumber">
          > <xsl:value-of select="$addHyphens" />
          > </xsl:element>
          > </xsl:template>
          >
          > This tests the first character of the text node, and if it is a
          space,
          > removes it. When it finally finds a character that is not a space,
          it then
          > proceeds to replace the rest of the spaces in the text node with
          hyphens. I
          > used two named templates here for clarity, but you may be able to
          do it with
          > one.
          >
          > Hope this helps,
          > RG
        Your message has been successfully submitted and would be delivered to recipients shortly.