1
<title>
 <article_title>Land a b   c   d      Band</article_title>
</title>

using the following function

replace(article_title, '(^[^ ]+)(.+\s+)([^ ]+)$', '$1 $3')

this string in is transformed to Land Band which is exactly what i want.

but the problem is i need this solution in xslt 1.0 since the java app that i am working with can only handle xslt 1.0 parsing.

1 Answer 1

1

This XSLT 1.0 transformation (there is a nasty SO bug and the code isn't indented -- I apologize for this visual mess...):

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>

 <xsl:template match="node()|@*">
  <xsl:copy>
   <xsl:apply-templates select="node()|@*"/>
  </xsl:copy>
 </xsl:template>

 <xsl:template match="text()" name="removeSingles">
   <xsl:param name="pText" select="."/>

   <xsl:variable name="vText" select="normalize-space($pText)"/>

   <xsl:if test="string-length($vText)">
    <xsl:variable name="vLeftChars" select=
    "substring-before(concat($vText, ' '), ' ')"/>

    <xsl:if test="string-length($vLeftChars) >1">
     <xsl:value-of select="$vLeftChars"/>
     <xsl:if test=
      "not(string-length($vLeftChars)
          >=
           string-length($vText)
           )
      ">
       <xsl:text> </xsl:text>
      </xsl:if>
    </xsl:if>

    <xsl:call-template name="removeSingles">
     <xsl:with-param name="pText" select=
     "substring-after($vText, ' ')"/>
    </xsl:call-template>
   </xsl:if>
 </xsl:template>
</xsl:stylesheet>

when applied on the provided XML document:

<title>
 <article_title>Land a b   c   d      Band</article_title>
</title>

produces the wanted, correct result:

<title>
   <article_title>Land Band</article_title>
</title>
Sign up to request clarification or add additional context in comments.

8 Comments

Dimitre, thanks looks like it should work, how do i call this ? is this correct <xsl:value-of select="removeSingles"/>
@sanjay: What do you mean by "Its not working". I always test my answers before publishing them. Just run the transformation on the provided XML document and you should get the reported result. If not, then either you modified the XSLT code, or you modified the XML file, or both, or your XSLT processor is buggy / not fully compliant.
Dimitre, I really appreciate your effort and time, I have to apply this only to a particular element on my xml. This is how i am calling <xsl:value-of select="removeSingles"/> is this correct ?
@sanjay: No, you need to: <xsl:call-template name="removeSingles"> <xsl:with-param name="pText" select="yourElement"/></xsl:call-template> . Also, remove the match="text()" from the removeSingles xsl:template instruction.
Dimitri, Thank you very much.....it working. In your previous comment u asked me to remove match="text()" How do i do that and what will be the benefit ?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.